jhony3 jhony3 - 3 months ago 23
C# Question

How to Change a Control of a Form from Another Form?

I would like to set comboBox.SelectedValue when I select the row in my dataGridView on first form to populate comboBox with that value on another form, on second form in my load event I have `comboBox.DataSource, DisplayMemeber, ValueMemeber set it correctly but nothing is happening when I set selectedValue on first,everything works great when I do it on one form ?

Answer

Form in Windows Forms is a class like other C# classes. The way of communicating between forms are the same as classes. You can consider this options when communicating between classes:

Pass data to second Form when creating

  • You can add suitable parameters to the constructor of the second form. Then you can pass values to the constructor when creating an instance of the second form. In the second form store parameters in member fields and use them when you nees.

  • You can create public properties for the second form and set those properties after creating an instance of the second form. This way you can use them when you need in the second form.

Manipulate second Form after showing

  • You can create a public method or public property in second form and call it from first form. It's useful for both setting a value in second form and also for getting a value from it.

  • As another option you can make the control which you want to manipulate it public and this way you can access to it from other form. Using a method is a more recommended way of doing this.

Manipulate first Form from second form

  • You can create a public method or property in first form and pass an instance of the first form to second form. Then using that method/property on the passed instance, you can manipulate the first form.

  • You can create an event in second form and after creating an instance of second form subscribe for it in first form and put the code for changing the form in the handler. Then it's enough to raise the event in second form.

  • You can define a public property of type Action or some other delegate type in second form and then after creating an instance of second form, assign the property using a custom action. Then in second form, it's enough to invoke the action when you need to manipulate first form.

  • Also here you can make a control of first form to be public and then if you pass an instance of the first form to the second form, you can manipulate the control. It's recommended to use other solutions. It's like creating public property or method, but a method which performs specific task on the control is better that exposing the whole control. But you may need this solution some times.

Example

Here is an example based on first option. You can pass the value to constructor of your new form. Then you can store it in a member field and use it in Load event.

public partial class Form2 : Form
{
    int selectedValue;
    public Form2(int value)
    {
        InitializeComponent();
        selectedValue = value;
    }
    private void Form2_Load(object sender, EventArgs e)
    {
        //Load data
        this.comboBox1.DataSource = new MyDbContext().Categories.ToList();
        this.comboBox1.DisplayMember = "Name";
        this.comboBox1.ValueMember = "Id";
        this.comboBox1.SelectedValue = selectedValue;
    }
}

Then in your first form, it's enough to pass the value to Form2 when you create a new instance of it:

var value = 2; // Or get it from grid
var f = new Form2(value);
f.ShowDialog();