简体   繁体   中英

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.

This is a checkbox and load button There is a 2 graph

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

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

As @Tamas Szabo and @Danish_k12 wrote you, you need to add a public property in your second form. On Load button click initialize new 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. Here is how you can achieve this:

Form1 - with Form2 as private field

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:

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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