c# Winforms: How to read a value form another form

Hi

New to c# Winforms.  I have a menu form that has a combox control on it. When I open up a form from that menu form, how can I read the value in the Menu Form on the form I just open up?

So I have in the menu form
MenuDesigner.cs
       this.RecordType = new System.Windows.Forms.ComboBox();

Menu.cs
      getContributions frm = new getContributions();
      frm.ShowDialog();

Thanks




then in my
Charles BaldoSoftware DeveloperAsked:
Who is Participating?
 
anarki_jimbelConnect With a Mentor Commented:
The common approach is to set "relations" between forms. In other words forms should "know" each other to pass the information. In some occasions it's enough if parent form knows about child form, in some - vice versa. Quite often - both parent and child should know each other.

For the sake of example: I have two forms. Parent has two buttons, one is to create and show a child, second one is to get info from the child. The child form has one button - to get info from the parent. See the snippets below:
    public partial class MyParentForm : Form
    {
        private MyChildForm child;
        public MyParentForm()
        {
            InitializeComponent();
        }
        // button to show a child
        private void button1_Click(object sender, EventArgs e)
        {
 
            if (child == null  || child.IsDisposed)
            {
                // create new child passing parent form into constructor
                child = new MyChildForm(this);
                child.Show();
            }
        }
        // method that can be infoked by child to get info from parent
        public string GetMessageFromParent()
        {
            return "Hello from parent";
        }
        // button to get info from child
        private void button2_Click(object sender, EventArgs e)
        {
            if (child != null && !child.IsDisposed)
            {
                MessageBox.Show(child.GetMessageFromChild());
            }
        }
    }
 
=========================================================
 
    public partial class MyChildForm : Form
    {
        // reference to parent form
        private MyParentForm _parent;
        // constructor
        public MyChildForm(MyParentForm parent)
        {
            InitializeComponent();
            _parent = parent;
        }
        // some public method that can be invoked by parent
        public string GetMessageFromChild()
        {
            return "Hello from child";
        }
        // button handler to get info from parent
        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show(_parent.GetMessageFromParent());
        }
    }

Open in new window

0
 
jbachmanNETConnect With a Mentor Commented:
Hey,

You could change the constructor in the getContributions  form as follows:
private MenuChoice;
public getContributions(string menuChoice)
{
  MenuChoice = menuChoice;
}

and then run the form like this:
getContributions frm = new getContributions(combobox.SelectedItem.Text);
frm.ShowDialog();

now withing the getContributions form you can access the selected menu choice by accessing this.MenuChoice



0
 
CebikConnect With a Mentor Commented:
or make this combo modifier as public..
and other methodes and values witch you want to use and put your whole form into constructor..

prForm.menuChoice.SlectedItem....

or make in your new form public fields..
and fill them before starting form (frm.ShowDialog());
frm.menuChoice = menuChoice.Text;

getContributions frm = new getContributions(this);
frm.ShowDialog();

Open in new window

0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

 
anarki_jimbelCommented:
NEVER make controls public! Create a special method to access values in a combobox, like in my code above:

        public string GetSelectedItemValueFromComboOnParent()
        {
            return mycombo.SelectedItem.ToString();
        }


Sometimes  it does make sense to set some values on a child form before displaying it (and not passing parent's reference to constructor). However, don't make child's controls public - create set properties or methods instead.
0
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Another option is to pass the "owner" form when you call ShowDialog().  Then you can use the Owner() property in the dialog to access the owner form:

    public partial class Form1 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog(this);
        }
    }

    public partial class Form2 : Form
    {
        private void button1_Click(object sender, EventArgs e)
        {
            Form1 f1 = (Form1)this.Owner;
            f1.label1.Text = "Hello from Form2!";
        }
    }

(I made the Modifiers() Property of label1 Public for simplicity in the example.  As the others have noted, this is a bad practice.)
0
 
Charles BaldoSoftware DeveloperAuthor Commented:
Thanks Everyone learned a bunch and used anarkis solution
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.