Answered by:
How do I access MainForm textbox from another class?

Question
-
Hi,
To make things easy for me to learn, I have coded my windows form program on MainForm.cs. Now, I want to move the code to another class within a 2nd project called Functions. I have added the correct References and using statements. I copied the code from the Mainform to the new class, and I need to pull a string from a textbox on the MainForm, but I cannot see the textbox variable when updating the code. What changes to do I need to make so the texbox variable will appear via intellisense?
Thanks in advance,
John- Moved by OmegaMan Friday, March 5, 2010 2:40 AM (From:Visual C# General)
Sunday, February 28, 2010 4:26 PM
Answers
-
Hi
You need to declare that variable as static like as follows. Then you can access it from any class. Don't hesitate to post back if anything unclear
Code in MainForm.cs
public static string MyString=textBox1.Text;
Please mark the post as answered if my post is the answer for your question, and mark other helpful posts as helpful. Thanks Vijay- Marked as answer by CsharpLerner Tuesday, March 2, 2010 11:40 PM
Sunday, February 28, 2010 5:25 PM -
Please read:
Passing Values Between Forms
This discusses the difference between parent to child, sibling to sibling relationships and how to pass data around.
For passing of info back to MainForm from sibling forms, you would use events to do that.
public class TextValueEventArgs : EventArgs
{
public String MyText { get; private set; }
public TextValueEventArgs (String text)
{
MyText = text;
}
}
Then on Form2, you would create an event:
public event EventHandler<TextValueEventArgs> AdviseParent;
Then to activate the push, you would do something like this:
EventHandler<TextValueEventArgs> handler = this.AdviseParent;
if (handler != null)
{
handler(this, new TextValueEventArgs("My value to push"));
}
this.Close();
//MainForm
Form2 form2 = new Form2();
form2.AdviseParent += new EventHandler<TextValueEventArgs>(form2_AdviseParent);
form2.Show();
//somewhere in MainForm
private void form2_AdviseParent(object sender, TextValueEventArgs e)
{
textBox2.Text = e.MyText;
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by Rudedog2 Wednesday, March 3, 2010 1:47 PM
Sunday, February 28, 2010 5:37 PM
All replies
-
Hi
You need to declare that variable as static like as follows. Then you can access it from any class. Don't hesitate to post back if anything unclear
Code in MainForm.cs
public static string MyString=textBox1.Text;
Please mark the post as answered if my post is the answer for your question, and mark other helpful posts as helpful. Thanks Vijay- Marked as answer by CsharpLerner Tuesday, March 2, 2010 11:40 PM
Sunday, February 28, 2010 5:25 PM -
Please read:
Passing Values Between Forms
This discusses the difference between parent to child, sibling to sibling relationships and how to pass data around.
For passing of info back to MainForm from sibling forms, you would use events to do that.
public class TextValueEventArgs : EventArgs
{
public String MyText { get; private set; }
public TextValueEventArgs (String text)
{
MyText = text;
}
}
Then on Form2, you would create an event:
public event EventHandler<TextValueEventArgs> AdviseParent;
Then to activate the push, you would do something like this:
EventHandler<TextValueEventArgs> handler = this.AdviseParent;
if (handler != null)
{
handler(this, new TextValueEventArgs("My value to push"));
}
this.Close();
//MainForm
Form2 form2 = new Form2();
form2.AdviseParent += new EventHandler<TextValueEventArgs>(form2_AdviseParent);
form2.Show();
//somewhere in MainForm
private void form2_AdviseParent(object sender, TextValueEventArgs e)
{
textBox2.Text = e.MyText;
}
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.com- Marked as answer by Rudedog2 Wednesday, March 3, 2010 1:47 PM
Sunday, February 28, 2010 5:37 PM -
Did this answer your question or do you have any more questions?
John Grove - TFD Group, Senior Software Engineer, EI Division, http://www.tfdg.comMonday, March 1, 2010 9:03 PM -
Maybe I can guess what is wrong.
If you copy&paste the code to other project maybe you forgot to do for it's designer code(MainForm.Designer.cs) too.
Nevertheless if you didn't forget, maybe the designer code(MainForm.Designer.cs) and your code(MainForm.cs) aren't in same namespace.
My Blog - MSDN Complement by providing Visual C# Walkthroughs and Sample Codes - Founded In February 24, 2010Monday, March 1, 2010 9:10 PM -
Hi John,
Your post/link was my next question to this board. Thanks for the links and info it will be extremely helpful to me.
Take care,
JohnTuesday, March 2, 2010 11:43 PM