Reputation: 35
I've been trying to change the text of a textbox in Form1 by clicking a button (button1 in form2 is "STARTA") in Form2 and probably spent a good 2 hours now (I'm a programming-newbie!). I have been searching around for similiar questions and found a bunch, but even after trying a lot of them I can't get it to work.
Form1[DESIGN]
Form2[DESIGN]
The method I'm trying right now is something I found here
In Form1 I wrote this:
public string STARTTID
{
get
{
return this.textBox3.Text;
}
set
{
this.textBox3.Text = value;
}
}
I know it doesn't quite make sense to get and set an empty textBox, but I've tried so many different solutions which I think should work, but the textBox's text just wont change when I click the button! In form2, when button1 is clicked, I wrote this:
string TIDEN = DateTime.Now.ToString("HH:mm:ss tt");
Form1 first = new Form1();
first.STARTTID = TIDEN;
What I'm trying to do, is that I want the text in textBox3 in form1 to change to the current time when button1 in form2 is pressed.
Sorry if this post is a bit messy, it's my first and english isn't my strongest language.
Upvotes: 3
Views: 3940
Reputation: 63105
problem is you creating new Form1
and update label on that one, not in the your initial form
Form1 first = new Form1();
first.STARTTID = TIDEN;
You don't need to create new form because you already created it. what you can do is parse the Form1 to Form2 when you create Form2 by using constructor which accept Form as parameter. or create property in Form2 for Form1 and set that when you creating Form2.
Form1
Form2 f2 = new Form2(this);
f2.Show();
Form2
public partial class Form2 : Form
{
private Form1 form1;
public Form2(Form1 form1)
{
InitializeComponent();
this.form1 = form1;
}
private void button1_Click(object sender, EventArgs e)
{
form1.STARTTID = "set by form2";
}
}
Upvotes: 1