0

Fairly new to WinForms. I have a main form (Main) that contains a SplitContainer (container) with two panels (Panel1 and Panel2)

In container.Panel1, I have a combobox (selectFY), and a button. Clicking the button loads a second form in container.Panel2 like:

    private void btnButton1_Click(object sender, EventArgs e)
    {
        container.Panel2.Controls.Clear();
        Form2 form = new Form2();
        form.TopLevel = false;
        form.Dock = DockStyle.Fill;
        container.Panel2.Controls.Add(form);
        form.Show();
     }

What I am trying to do is from within the code of Form2, read the selectedText value from the selectFY combobox in container.Panel1

I have tried:

  • Accessing as Main.container.Panel1.selectFY.selectedText

  • Creating a link to Main like

    Main mainform = new Main();
    var fy = mainform.container.Panel1.selectFY.selectedText
    

But neither has worked.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Keith Clark
  • 609
  • 1
  • 7
  • 19
  • 1
    Read example 2 or example 3 in linked post. You can create a public property in the child form and access the control through it. Also you can set the control to be public and access to control directly. – Reza Aghaei Nov 06 '16 at 20:16
  • @Reza - Thanks...that is what I needed. In the examples I had read, I never saw making the individual element public. That did the trick with the second method I had tried before. – Keith Clark Nov 06 '16 at 20:22
  • Yes, you can make the control public and it will be useful in some cases. But in general creating a public method or property is recommended. – Reza Aghaei Nov 06 '16 at 20:25
  • By the way. if you find the linked post useful, it would be great to vote for the question and the answer. It's not compulsory at all, this way you make them more popular and more useful for future readers :) – Reza Aghaei Nov 10 '16 at 13:05

0 Answers0