Hello friends.
I know there are lots of topics about this subject, but i couldn't find an answer for me.
I have a Program.cs file includes main. I'm defining a variable, and i want to show that in Form1. And user will be able to change that variable's value.
So, how can i write and read TextBox1 on Form1, from main program?
Thank you for your helps.
Loading
AlanPosted Oct 5, 2008, 1:56 PM
As long as you're never going to have more than one instance of Form2 then, yes, you could still use an internal static field:
public partial class Form2 : Form
{
internal static int counter; // 0 by default
public Form2()
{
//etc
and then from Form1, you can read or write the counter using the expression Form2.counter.
From Form2, just use 'counter' without qualifying it with the class name.
emrah ustunPosted Oct 5, 2008, 2:06 PM
emrah ustunPosted Oct 5, 2008, 1:00 PM
I'm trying, but i couldn't solve this problem.
Now i have Form2. It runs Form1 every 1 minute. Before running Form1, i'm setting counter, some value.
Now i can't read or write counter from Form1.
You said declare an internal static field. How i can do that?
Thaks for your helps.
AlanPosted Oct 4, 2008, 4:47 PM
You won't be able to read or write any form properties from the Program class. As soon as you hit this line in the Main() method:
Application.Run(new Form1());
execution will switch to the Form1 class and will stay there until the form is closed.
What you can do is to declare an internal static field, Text say, in the Program class and set it to a value before Application.Run() is called. Within the form, you can then write that to textBox1's Text property with this line:
textBox1.Text = Program.Text;
You can also reset this field from the Form1 class and then print it, say, to a MessageBox from the Main() method when the form is closed.