-5

I have a Main form, Form A, Form B and Form C which all include comboboxes. I want the selection in Form A or B or C to display on a text box or a label on the Main form. How do I do that? I would like to do this without buttons. Just selecting an item from the drop down menu and displaying it on the Main form.

Please help!!!

Many thanks in advance!

  • Possible duplicate of [Send values from one form to another form](https://stackoverflow.com/questions/1559770/send-values-from-one-form-to-another-form) or https://stackoverflow.com/q/20186722/1070452 also https://stackoverflow.com/q/1173973/1070452 and many others – Ňɏssa Pøngjǣrdenlarp Aug 07 '18 at 22:08
  • Without a lot more info about your project it's hard to say for sure, but if you are using an MV* (or similar) approach, then you could send out a message whenever a combobox changes. The main form could pick that up. Or, the main form could subscribe to events on the child forms. Those events would be fired when the comboboxes changed. However, this all sounds suspiciously like your design is wrong. Would be helpful if you explain why you want to do this. Maybe we can suggest a better approach. – Avrohom Yisroel Aug 07 '18 at 22:09
  • pass the label or textBox instance from main form to A,B or C form construtor when showing it and use textBox or label inside the selectedchange method to display value. – Arslan Ali Aug 07 '18 at 22:11

2 Answers2

1

Your problem essentially boils down to doing something on one form based on the events of another form.

The best approach to do this is, in my opinion:

  • Let Main do all its own actions.
  • Let FormA (and others) do all its own actions.
  • If you need to do something on Main based on an event that occurred on FormA, let FormA notify Main that something happened there, so go do your own thing.
  • If necessary, pass data from FormA to Main.

So I would make use of delegates for this purpose.

In my Main I'd declare a public delegate with a signature like this:

public delegate void NotifyComboBoxChange(string item);

That is, this delegate represents a method that takes in one string parameter, and has void return type. The idea is to let FormA 'call' a method in Main, and that method essentially notifies the combo box item was changed on FormA. So, if there was a way for us to call a method that resides in the Main from FormA, we can then notify Main of an event happening on FormA With me so far?

Now, if I write a method like this in the Main, and let it be called from FormA, that should accomplish our goal. Here, txtLabel is a TextBlock in Main.

public void ComboBoxValueChanged(string value)
{
    txtLabel.Text = value;
}

To call such a method, I would define a delegate of type NotifyComboBoxChange in Main like below, and register the ComboBoxValueChanged() method to it. Your Main code behind should look like this:

public delegate void NotifyComboBoxChange(string item);

public partial class Form1 : Window
{
    public NotifyComboBoxChange notifyDelegate;
    FormA formA = null;

    public Form1()
    {
        InitializeComponent();
        // This is 'registering' the ComboBoxValueChanged method to the delegate.
        // So, when the delegate is invoked (called), this method gets executed.
        notifyDelegate += new NotifyComboBoxChange(ComboBoxValueChanged);
    }

    public void ComboBoxValueChanged(string value)
    {
        txtLabel.Text = value;
    }

    private void btnOpen_Click(object sender, RoutedEventArgs e)
    {            
        // Passing the delegate to `FormA`
        formA = new FormA(notifyDelegate);
        formA.Show();
    }
}

Accordingly, now we need to modify our FormA. We need to tell us which delegate to invoke when the combo box selection change happens. So to do that, I'd pass the delegate in the constructor of FormA like so:

public partial class FormA : Window
{
    NotifyComboBoxChange notifyDel;

    public FormA(NotifyComboBoxChange notify)
    {
        InitializeComponent();
        notifyDel = notify;
    }

    private void cmbItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        // This gets the currently selected value in the CombBox
        var value = cmbItems.SelectedValue.ToString();
        // This invokes the delegate, which in turn calls the ComboBoxValueChanged() method in Main.
        notifyDel?.Invoke(value);
    }
}
Sach
  • 10,091
  • 8
  • 47
  • 84
1

Set your main form Control property of Modifier to public or inherit your own choice from the control window property.

Property Window Setting

Pass the Main-form object (this) to FormA,FormB,FormC when you are showing them.Now I'm showing them on button click.

private void showingAllForm_Click(object sender, EventArgs e)
    {
        FormA a = new FormA(this);
        a.Show();
        FormB b = new FormB(this);
        b.Show();
        FormC c = new FormC(this);
        c.Show();
        this.IsMdiContainer = true;
        a.MdiParent = this;
        c.MdiParent = this;
        b.MdiParent = this;
    }

Setting Constructor for FormA,FormB,FormC and add Combobox on FormA,FormB,FormC so to add following code for each combobox such as

FormA Replace for FormB,FormC and so on.

public partial class FormA : Form
{
    public FormA()
    {
        InitializeComponent();
    }
    MainForm fm;
    public FormA(MainForm fm)
    {
        InitializeComponent();
        this.fm = fm;
    }
    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        fm.textBox1.Text = comboBox1.Text;
    }
}

Output Results

Selected from FromA ComboBox

Selected from FromB ComboBox

Selected from FromC ComboBox

Arslan Ali
  • 450
  • 4
  • 12