views:

96

answers:

4

This might be a novice question. :). Consider the following scenario.

  • Suppose we have two windows forms that are already "loaded" (i.e You
    can see both the forms)
  • Form 1 contains a textbox and a "submit" button while the form 2 contains a text lable.
  • The user can enter a string in the textbox and press submit on form 1 The lable on form 2 should be updated with the new text.

What is the best way to achive this? Any formal way to do this? I don't want to increase the variable scopes unnecessarily.

Edit : Both the forms belong to same application

+3  A: 

Assuming these forms are part of the same application, you need to have a common data model where you keep your data and then your forms "bind" to this data model. Check out M-V-C or M-V-VM patterns. This would also nicely separate your UI from your data.

Muad'Dib
+1  A: 

Do some research on model view controller pattern and databinding in winforms.

Create a seperate controller class and reference this in the two forms, which implements INotifyPropertyChanged. On the controller, have a property which propagates the changed events from and to the forms.

Gerrie Schenck
A: 

Form1 triggers Form2 to open. Form2 has overloaded constructor which takes calling form as argument and provides its reference to Form2 members. This solves the communication problem. For example I've exposed Label Property as public in Form1 which is modified in Form2.

With this approach you can do communication in different ways.

Download Link for Sample Project

//Your Form1

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form2 frm = new Form2(this);
        frm.Show();
    }

    public string LabelText
    {
        get { return Lbl.Text; }
        set { Lbl.Text = value; }
    }
}

//Your Form2

public partial class Form2 : Form
{
    public Form2()
    {
        InitializeComponent();
    }

    private Form1 mainForm = null;
    public Form2(Form callingForm)
    {
        mainForm = callingForm as Form1; 
        InitializeComponent();
    }

    private void Form2_Load(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.mainForm.LabelText = txtMessage.Text;
    }
}

alt text

alt text

this. __curious_geek
-1 This is definitely not the way to do it. You want to separate the forms from each other. Why does form1 has to know about the existence of form2?
Gerrie Schenck
Of course, the forms have to be known if this needs to be accomplished genuinely. Why would you want to update a label in any arbitrary form without any purpose. So the reference is kept for the designated purpose.
this. __curious_geek
though i would not downvote, i share this opinion!
Andreas Niedermair
no - not needed. you could also make a contract .. an interface with a set-method, which would be nicer!
Andreas Niedermair
You can also do this with Remoting, If you need to. The questioner is a novice guy!
this. __curious_geek
What Andreas said...
Gerrie Schenck
Remoting is obsolete since WCF... and by the way this isn't cross-process.
Gerrie Schenck
Another solution would be apply to **Observer Pattern** here, yet this problem would be too small for Observer pattern.
this. __curious_geek
A: 

I think you will solve your problem with this topic: http://www.codeproject.com/KB/dotnet/passingvaluesbetweenforms.aspx

Lu Lu