Communicate Between Two Windows Forms in C#

Communicate between two windows forms in C#

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

(source: ruchitsurati.net)

alt text

(source: ruchitsurati.net)

Communicating between two Forms in C#

try this in Form1:

this.TextBoxName.Text = m_Form2.getLocation();

or in Form2:

m_form.TextBoxName.Text = this.getLocation();

Note
in Form1 when call form 2:

m_Form2 = new Form2(this);
//do anything with your code

Toggle and communicate between two Windows forms in Visual C#?

You shouldn't have 2 instances of MainForm, just use one and update as required.
In your mainform you should call the LoginForm and check the DialogResult (I will explain this later).

So here, if you get an OK result it means the Login worked, otherwise the user cancelled.

So in MainForm change the constructor to be empty and remove the u_name parameter

public Main_Form()
{
....
}


private void Loginbutton_Click(object sender, EventArgs e)
{
Form Form2 = new login_Form();
if(Form2.ShowDialog() == DialogResult.OK) //We call LoginForm and wait for a Result
{
//Login was successful. Then we can set the label to the username, which is now a property for Login Form (see below)
label1.Text = Form2.UserName;
}
else
{
//Login was cancelled
//You must now implement logic to handle this case
}

}

Now in the Login form you want to check login and return the dialog result to the MainForm. DialogResult is, as the name suggests, the result of calling the LoginForm Dialog.

You will also need another click event for the cancel button.

public partial class login_Form : Form
{
//username is changed to a property
public string Username { get; private set; }

public login_Form()
{
InitializeComponent();
}

private void Button_Login_Click(object sender, EventArgs e)
{
//The check remains as before.
if (textBox_Login.Text == "Admin" && textBox_Password.Text == "123")
{
//If successful, we set the dialogresult, username and close this form.
this.Username = textBox_Login.Text;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
//Otherwise we handle the error
MessageBox.Show("Please Enter Username and/or Password Again!");
}
}

//The new click event for the cancel button
private void Button_Login_Cancel(object sender, EventArgs e)
{
//We set the dialogresult to cancel (could be anything else) and close
this.DialogResult = DialogResult.Cancel;
this.Close();
}
}

Communication between 2 Windows Forms applications

Here is a good example using WCF to communicate two processes:

http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication

Communicating between forms

well you can use delegates to communication between windows forms.
Check http://www.c-sharpcorner.com/uploadfile/mosessaur/winformsdelegates09042006094826am/winformsdelegates.aspx

For Delegates

EDIT

Check
Delegates (C# Programming Guide)

http://msdn.microsoft.com/en-us/library/ms173171%28VS.80%29.aspx

Creating Custom Delegates and Events in C#

http://www.csharphelp.com/2007/02/creating-custom-delegates-and-events-in-c/

Introduction to Delegates and Events

http://www.csharp-station.com/Tutorials/lesson14.aspx



Related Topics



Leave a reply



Submit