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;
}
}
(source: ruchitsurati.net)
(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
How to Create an Excel (.Xls and .Xlsx) File in C# Without Installing Microsoft Office
How to Use Linq to Select Object With Minimum or Maximum Property Value
How to Copy the Contents of One Stream to Another
How to Write Unicode Characters to the Console
Difference Between Task and Thread
Why Does Property Set Throw Stackoverflow Exception
How to Save a Stream to a File in C#
When Correctly Use Task.Run and When Just Async-Await
Why Is Thread.Sleep So Harmful
How to Force My .Net Application to Run as Administrator
Convert a String to an Enum in C#
What Is the C# Using Block and Why Should I Use It
Why Would You Use Expression≪Func≪T≫≫ Rather Than Func≪T≫
Is There a Difference Between "Throw" and "Throw Ex"