wxy新人小白 2019-09-05 18:33 采纳率: 27.8%
浏览 893
已采纳

在C#中如何使窗体间传combobox当前选中的值,而不是初始化后的combobox的第一个数据

目的:点一个按钮,子窗口嵌在主窗口(panel)上,在主窗口选完combobox的值后,按查询按钮,子窗口根据combobox的值选择数据使chart出图,然后改变combobox的值,子窗口上chart所连接的数据发生改变,图发生改变

问题:不管我怎么选combobox的值,它都只传comboBox1.Text=CHR1,comboBox2.Text=1,comboBox3.Text=1009;都只连接同一组数。
我该怎么使窗体间传combobox当前选中的值
主窗口中部分程序

public partial class Form1 : Form
    {
        Win1 w1;
        Win2 w2;
        Win3 w3;
        Win4 w4;
        Win5 w5;
        private bool flag1;
        private bool flag2;
        private bool flag;
        public string com1
        {
            get { return comboBox1.Text; }
        }
        public string com2
        {
            get { return comboBox2.Text; }
        }
        public string com3
        {
            get { return comboBox3.Text; }
        }
        public Form1()
        {
            InitializeComponent();
            string[] chexing = new[] { "CHR1", "CHR2", "CHR3" };
            string[] tuoche = new[] { "1", "2", "3" };
            string[] cgqxh = new[] { "1009", "1002", "1666" };
            comboBox1.DataSource = chexing;
            comboBox1.Name = "comboBox1";
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox2.DataSource = tuoche;
            comboBox2.Name = "comboBox2";
            comboBox2.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox3.DataSource = cgqxh;
            comboBox3.Name = "comboBox3";
            comboBox3.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox3.Enabled = false;
            if (flag1 == true && flag2 == true)//按完combox1和combox2之后,才能选combox3
            {
                comboBox3.Enabled = true;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            w1 = new Win1();
            w2 = new Win2();
            w3 = new Win3();
            w4 = new Win4();
            w5 = new Win5();
        }
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            this.panel1.Controls.Clear();  // 清空原有的控件
            w1.TopLevel = false;  // 非顶级窗口
            w1.FormBorderStyle = FormBorderStyle.None;  // 不显示标题栏
            w1.WindowState = FormWindowState.Maximized;
            this.panel1.Controls.Add(w1);  // 添加w1窗体
            w1.Show();
            flag = true;
        }
                private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
        {
            flag1 = true;
        }

        private void comboBox2_SelectedValueChanged(object sender, EventArgs e)
        {
            flag2 = true;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (flag == true && flag1 == true && flag2 == true)
            {
                w1.Creatchart1();

            }
        }

子窗口中部分程序

public partial class Win1 : Form
    {
        private string a;
        private string b;
        private string c;
        private SQLiteConnection m;
        public Win1()
        {
            InitializeComponent();
        }
        public void Creatchart1()
        {
            chart1.Series.Clear();
            Form1 f1 = new Form1();
            a = f1.com1.ToString();
            b = f1.com2.ToString();
            c = f1.com3.ToString();
            string wax = a + b + c;
            string dbPath = @"D:\历年高考分数.db";
            string sq = @"Data Source=" + dbPath;
            m = new SQLiteConnection(sq);
            m.Open();
            SQLiteDataAdapter mAdapter = new SQLiteDataAdapter("select " + wax + ",yxlc from 表2", m);
            DataSet ds = new DataSet();
            mAdapter.Fill(ds);
            DataTable dt = ds.Tables[0];
            //设置图表的数据源
            Series Series1 = new Series();
            chart1.DataSource = dt;
            Series1.IsValueShownAsLabel = false;//是否显示图例
            chart1.Series.Add(Series1);
            chart1.Series["Series1"].ChartType = SeriesChartType.Spline;//形状
            chart1.Series[0].XValueMember = "yxlc";//X轴数据成员列
            chart1.Series[0].YValueMembers = wax;
            chart1.Series[0].IsValueShownAsLabel = true;//显示坐标
            chart1.ChartAreas[0].AxisX.Title = "运行里程(万公里)";
            chart1.ChartAreas[0].AxisY.Title = "故障率(%)";
            chart1.Series[0].MarkerStyle = MarkerStyle.Circle;
            要提//需示的信息    
            chart1.Series[0].ToolTip = "运行里程:#VALX\\n故障率:#VALY";
            //将文字移到外侧
            chart1.Series[0]["PieLabelStyle"] = "Outside";
            chart1.DataBind();
            chart1.BringToFront();
            m.Close();
        }

图片说明

  • 写回答

4条回答 默认 最新

  • wxy新人小白 2019-09-09 11:16
    关注

    private void button1_Click(object sender, EventArgs e)
    {
    if (flag == true && flag1 == true && flag2 == true)
    {
    string wax = comboBox1.Text + comboBox2.Text + comboBox3.Text;
    w1.Creatchart1(wax);

            }
        }
                 public void Creatchart1(string wax)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 VS2022 C++的相关代码问题咨询
  • ¥15 如果要做一个老年人平板有哪些需求
  • ¥15 k8s生产配置推荐配置及部署方案
  • ¥15 matlab提取运动物体的坐标
  • ¥15 人大金仓下载,有人知道怎么解决吗
  • ¥15 一个小问题,本人刚入门,哪位可以help
  • ¥30 python安卓开发
  • ¥15 使用R语言GD包一直不出结果
  • ¥15 计算机微处理器与接口技术相关问题,求解答图片的这个问题,有多少个端口,端口地址和解答问题的方法和思路,不要AI作答
  • ¥15 如何根据一个截图编写对应的HTML代码