?
Solved

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

Posted on 2009-04-15
6
Medium Priority
?
2,530 Views
Last Modified: 2012-05-06
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
0
Comment
Question by:Charles Baldo
6 Comments
 
LVL 1

Assisted Solution

by:jbachmanNET
jbachmanNET earned 132 total points
ID: 24152724
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
 
LVL 30

Accepted Solution

by:
anarki_jimbel earned 1600 total points
ID: 24152884
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
 
LVL 6

Assisted Solution

by:Cebik
Cebik earned 132 total points
ID: 24152924
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 30

Expert Comment

by:anarki_jimbel
ID: 24153010
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 136 total points
ID: 24153410
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
 

Author Closing Comment

by:Charles Baldo
ID: 31570716
Thanks Everyone learned a bunch and used anarkis solution
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

750 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question