简体   繁体   English

如何用C#编写代码以通过Form2访问Form 1 CheckedBox并在Form2中执行一些操作

[英]How to code in C# for accessing Form 1 checkedBox by Form2 and do some action in Form2

There are two graph in Form2 and user input the tick on check box 1 in Form1 and press load button.After that program show to enable chart1 or chart2.As an example if user select check box 1 and 2 then show two graph.If user select check box 1 then only show chart1.Please help me by given coding example for this.I attached my interface with this. 在Form2中有两个图形,用户在Form1中的复选框1上打勾,然后按Load按钮。该程序显示后启用chart1或chart2。例如,如果用户选中复选框1和2然后显示两个图形。选中复选框1,然后仅显示chart1。请为此提供编码示例,为我提供帮助。我为此附加了界面。

This is a checkbox and load button There is a 2 graph 这是一个复选框和加载按钮 有一个2图

您可以将公共变量声明为布尔值,并根据复选框的值更改该值。

我认为最干净的解决方案是创建一个构造函数,该构造函数将两个bool用作参数并基于这些值显示图形。

As @Tamas Szabo and @Danish_k12 wrote you, you need to add a public property in your second form. 正如@Tamas Szabo和@ Danish_k12所写的那样,您需要在第二种形式中添加一个公共属性。 On Load button click initialize new form2. 在“加载”按钮上,单击“初始化新form2”。 Then check which checkbox is checked and set the public property you add to the second form accordingly. 然后检查选中了哪个复选框,并相应地设置添加到第二个表单的公共属性。 Then in Load event of the second form, depending on the value of the public property you added show first or second graph. 然后在第二种形式的Load事件中,根据您添加的公共属性的值显示第一幅或第二幅图。 Here is how you can achieve this: 这是您可以实现的方法:

Form1 - with Form2 as private field Form1-以Form2作为私有字段

namespace WindowsFormsApplication3
{
    using System;
    using System.Windows.Forms;

    public partial class Form1 : Form
    {
        private Form2 form2;

        public Form1()
        {
            this.InitializeComponent();
            this.button_Load.Click += Button_Load_Click;
        }

        private void Button_Load_Click(object sender, EventArgs e)
        {
            if(this.form2 != null)
                this.form2.Dispose();

            this.form2 = new Form2();
            if(this.checkBox1.Checked == true)
            {
                this.form2.IndexOfGraphToShow = 1;
            }

            if(this.checkBox2.Checked == true)
            {
                this.form2.IndexOfGraphToShow = 2;
            }

            if(this.form2.IndexOfGraphToShow == 1 || this.form2.IndexOfGraphToShow == 2)
            {
                this.form2.Show();
                return;
            }

            MessageBox.Show("Select which graph to show", "Choose graph", MessageBoxButtons.OK, MessageBoxIcon.Error);
            form2.Dispose();
        }
    }
}

Form2: 窗体2:

namespace WindowsFormsApplication3
{
    using System;
    using System.Windows.Forms;

    public partial class Form2 : Form
    {
        public Form2()
        {
            this.InitializeComponent();
            this.Load += Form2_Load;
        }

        public int IndexOfGraphToShow { get; set; }

        private void Form2_Load(object sender, EventArgs e)
        {
            if(this.IndexOfGraphToShow == 1)
            {
                //  TODO: Show first graph
            }
            else if(this.IndexOfGraphToShow == 2)
            {
                //  TODO: Show second graph
            }
        }
    }
}

One more thing. 还有一件事。 Using check boxes in your case is good option only if you can show both graphics at once, as both check boxes could be checked. 仅当您一次可以显示两个图形时,才在您的情况下使用复选框是一个不错的选择,因为可以选中两个复选框。 If you plan to show only one graph consider using option set. 如果您打算仅显示一个图形,请考虑使用选项集。

To show each time button was clicked a new form you may store the shown form in a private field. 要显示每次单击按钮的新表单,可以将显示的表单存储在私有字段中。 Check if the field is not null on button click. 单击按钮时检查该字段是否不为空。 If so dispose the old form and create a new one. 如果是这样,请处置旧表格并创建一个新表格。

You may consider also using of ShowDialog instead of Show when you show the second form like this: 当您显示第二种形式时,您也可以考虑使用ShowDialog代替Show:

this.form2.ShowDialog();

This will create a dialog window and user will not be able to reach the button of the first form as long as second form is open. 这将创建一个对话框窗口,并且只要打开第二个表单,用户将无法访问第一个表单的按钮。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM