Can't pass data to another form.

B

Boki

Hi All,

I can't pass data to another form:


in form2:

private void button1_Click(object sender, EventArgs e)
{
Form1 form_copy = new Form1();
form_copy.textBox1.Text = "DSFDSFDS";
this.Close();
}

the form2 will close after button click, but the textbox1 of form1
doesn't change anything.

textbox1 of form1 already set to public.

Please advice.

Best regards,
Boki.
 
B

Boki

Hi All,

I can't pass data to another form:

in form2:

private void button1_Click(object sender, EventArgs e)
{
Form1 form_copy = new Form1();
form_copy.textBox1.Text = "DSFDSFDS";
this.Close();
}

the form2 will close after button click, but the textbox1 of form1
doesn't change anything.

textbox1 of form1 already set to public.

Please advice.

Best regards,
Boki.

I have resolved this issue.

I added the new form from the new button of toolbar.

It seems I have to add the new form from right mouse click of solution
explore.

Best regards,
Boki.
 
B

Boki

I have resolved this issue.

I added the new form from the new button of toolbar.

It seems I have to add the new form from right mouse click of solution
explore.

Best regards,
Boki.

OK, sorry, it is not the root cause.

I got strange here, when I show form2 from form1, the string pass
well, but when I pass from form2, it never works.


/* this is form 1 code to show form 2*/
public void button1_Click(object sender, EventArgs e)
{
...

Form2 frm2 = new Form2();
frm2.textBox1.Text = "testing from form1"; /* this line
works well */
frm2.Show();
...

}

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

private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.textBox1.Text = "dsfsdf"; /* this line makes nothing
happen */
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
}
}


Best regards,
Boki.
 
B

Boki

OK, sorry, it is not the root cause.

I got strange here, when I show form2 from form1, the string pass
well, but when I pass from form2, it never works.

/* this is form 1 code to show form 2*/
public void button1_Click(object sender, EventArgs e)
{
..

Form2 frm2 = new Form2();
frm2.textBox1.Text = "testing from form1"; /* this line
works well */
frm2.Show();
...

}

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

private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.textBox1.Text = "dsfsdf"; /* this line makes nothing
happen */
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
}

}

Best regards,
Boki.

OK, new test result.

the code really create a new form, not the original form I was
using...

How to let it only do reference things....


Best regards,
Boki.
 
B

Boki

OK, new test result.

the code really create a new form, not the original form I was
using...

How to let it only do reference things....

Best regards,
Boki.

Hi

I try this way in form2:

private void button1_Click(object sender, EventArgs e)
{
Form1.update_msg_string(textBox1.Text.ToString());
}


and put this in form1:

public void update_msg_string(string msg_str)
{
textBox1.Text = msg_str;
}


No luck yet, ....

Boki.
 
I

Ian Semmel

When you have one form create another that requires data to be exchanged, it
is not advisable to directly manipulate controls on the other form.

There are a number of ways to pass info between forms

eg in form1 you may have

Form2 form2 = new Form2 ();
form2.Tag = someObject;
form2.Show();
and in form2_Shown you can have
MyObject o = (MyObject) Tag;

You can also set properties in Form2 eg

public string SetMyText
{
get { myControl.Text = value; }
}
and in Form1 you would have
form2.SetMyText = "aaaa";

or you can use functions, event handlers etc
 
I

Ian Semmel

It's very early in the morning here

set { myControl.Text = value; } // not 'get'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top