Hello,
Currently I'm developing simple chat client.
I want to know how to pass data from one form to another form but at the same form.
For Example I have two Form which is Form1 and Form2.
Form1 can generate Form2 many times but which different Form2 just the Form2.text (the name)

So, I Open Form1 and click the button to create two Form2. Lets say the first Form2.text = "FirstForm2" and the second Form2.text ="SecondForm2"

The problem is, how to keep sending new data (updating the data) to others form without needed to close the form and can be chosen.
My intention is to add new information into the textbox on the other form. (not to create new one, but update the FirstForm2 that has been opened unless it has been closed then create a new one).

Form1 Code:

private void btnSend_Click(object sender, System.EventArgs e)
		{
			// Through Constructor
            Form2 frm = new Form2();
            frm.loadDis(textBox1.Text);
            frm.Show();
		}

/* to create new form*/
private void button1_Click(object sender, EventArgs e)
        {
            Form2 frm = new Form2();
            frm.Text = username;
            frm.Show();
        }

Form2 Code:

internal void loadDis(String dis)
        {
            label1.Text = dis;
        }

Recommended Answers

All 7 Replies

define your form2 at namespace lavel
like for e.g.

namespace WindowsApplication2
{
    public partial class Form1 : Form
    {
	Form2 frm = new Form2();
	public Form1()
	{
	}
	private void btnbold_Click(object sender, EventArgs e)
        {
            frm.loadDis(textBox1.Text);
            f2.Show();
        }
	}
}

Dear pritesh2010, thank you for your answer
I had tried that code before but it is still create a new form and send the data to the new form

i create two button, 1 for create form2 and another one is to send data.

from the send data button, how to correctly send data to another one without opening new one if the form2 had been created.

can you show me the code for your help. so i can help you.

are both the forms going to run on the same machine and are the messages going to be passed over a network connection ? (for example are you going to send a message from form1 to the using the IP address of the machine on which form2 is currently running ?

@pritesh2010
from your code is good enough for what I'm looking for.. it can send to the form2 as much as I want..
but I cannot create another form2 anymore ?

Just like Yahoo messenger, when you chat with other friends it will open a new form right?

still need my currently code? if yes i'll upload it..

@ChrisHunter
I haven't decided that yet, probably it will be easier to send a message from form1 to the IP address of other user.

Daniweb has a search function, I typed C# sending data between forms clicked the serch button and got lots of answers.
You will surely find what you are looking for.

@ddanbe
yes i know how to search yet haven't find any suitable answer. I even googling looking for an answer. All the answer can only send to one form not multiple

Problem is I have two form. Form1 have the button to create many Form2.
Then I want to pass data from FORM1 to other selected FORM2 thas has been created.

For Example
I'm using createButton to show Form2A, Form2B, and Form2C.
Then I would like to know how Form1 can send data/value to Form2B or Form2C depends on the action the user want and vice-versa. Also how Form2B can send data to Form2C ?
That's it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.