Question C# Checkbox pass value from a another form

Joined
Jul 12, 2021
Messages
2
Programming Experience
1-3
  1. How do i do for passing value, when i mark checkbox in the win forms and automatically the value pass parameters and mark the checkbox in the form2, the four checkbox? I need the code in C#
 
I need the code in C#
No, you need to understand the principles involved so that you can write the code yourself. If you expect others to write your code for you then you'll need them to keep doing so every time you encounter something remotely difficult. What you need to do is learn various programming principles and then you can apply them in various combinations to solve all manner of problems. Of course you'll need some help along the way - we all do - and we're here to provide that, but you still need to do your part to learn how to be a software developer, not just a code writer or, worse, a code copier.

Firstly, it matters whether you need to target form to update immediately when the change is made in the source form or only when the source form closes. It would be more usual for it to be the latter, e.g. you have a main form open, you open a modal dialogue from there, make some changes in the dialogue, close the dialogue and then the main form updates based on those changes. In that case, the dialogue simply needs to expose the appropriate data via one or more properties and the main form reads those properties after the dialogue closes, e.g.
Form2:
public bool CheckBox1Checked => checkBox1.Checked;
and:
Form1:
private void button1_Click(object sender, EventArgs e)
{
    using (var dialogue = new Form2())
    {
        if (dialogue.ShowDialog() == DialogResult.OK)
        {
            checkBox1.Checked = dialogue.CheckBox1Checked;
        }
    }
}
Forms are objects just like any other, so you move data into and out of them just like you do any other objects, i.e. by calling their methods and/or by getting and/or setting their properties. You do that already for the Text of a TextBox, etc, so this is no different. The only extra step is that you have to write the property yourself, but that's one of the principles you need to understand.

If you need the first form to update in real time as changes are made in the second form, which would be unusual, then you still use a property in the same way. The difference is that you don't have the closing of the second form to notify the first form that it needs to retrieve the data. You need some other sort of notification. How do you usually get notified that something has happened in WinForms? You handle an event. Again, you already do this in the Load event of a form or the Click event of a Button. The only difference is that, in this case, you need to write the event yourself. Once the event exists in the second form, you handle it like you would any other event. I won't write any code for that here but you can check out my blog post on the subject here. That is the third part of a three-part series on passing data between forms. You might like to read the other two parts as well but the third part shows how to do things the proper way. The first part deals with default instances, which are VB-specific but can be simulated in C#. The second part uses global variables which is a quick and dirty way to move data around.
 
Moving to WinForms...
 
Back
Top Bottom