943,861 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 7619
  • C# RSS
Mar 10th, 2009
0

c# Changing Textbox.Text from different class

Expand Post »
I have the function AddToText which is located in the Form1 Partial Class

C# Syntax (Toggle Plain Text)
  1. public void AddToText(string text)
  2. {
  3. this.txtStatus.Text = this.txtStatus.Text + "\r\n";
  4. }
I want to call this from a different class so I do:
C# Syntax (Toggle Plain Text)
  1. Form1 frm = new Form1();
  2. frm.AddToText("Test");
I click a button called start and it calls the procedure on a different class and runs the code above. But nothing appears in my textbox. It works when I call it from the Partial Class Though.

WTF?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mastermosley is offline Offline
13 posts
since Mar 2009
Mar 10th, 2009
0

Re: c# Changing Textbox.Text from different class

The top code is actually:
C# Syntax (Toggle Plain Text)
  1. public void AddToText(string text)
  2. {
  3. this.txtStatus.Text = this.txtStatus.Text + "\r\n" + text;
  4. }
And nothing is displayed at all in the text box
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mastermosley is offline Offline
13 posts
since Mar 2009
Mar 10th, 2009
0

Re: c# Changing Textbox.Text from different class

I just added a MessageBox.Show(txtStatus.Text); below the function
and it gives me a messagebox with whas is supposed to be in the textbox meaning that it is in there but its not displaying it.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mastermosley is offline Offline
13 posts
since Mar 2009
Mar 10th, 2009
0

Re: c# Changing Textbox.Text from different class

I guess you have a Form1 Class which running in the first place. You created another Form class and run the the method in that class. So the method will update the will be update the textbox in the new class not the current running class. Try to put the code this way see what happen:
Form1 frm = new Form1();
frm.show();
frm.AddToText("Test");
Reputation Points: 11
Solved Threads: 4
Light Poster
hieuuk is offline Offline
44 posts
since Nov 2008
Mar 10th, 2009
0

Re: c# Changing Textbox.Text from different class

Just to be sure and check all sides of the problem

I noticed:
C# Syntax (Toggle Plain Text)
  1. this.txtStatus.Text + "\r\n" + text;

if its a one line textbox you won't be able to see the output, for testing purposes, try changing it to this
C# Syntax (Toggle Plain Text)
  1. this.txtStatus.Text = text;

also be sure that in form shown you aren't setting the value of the textbox
Last edited by dickersonka; Mar 10th, 2009 at 12:52 pm.
Reputation Points: 133
Solved Threads: 141
Veteran Poster
dickersonka is offline Offline
1,162 posts
since Aug 2008
Mar 10th, 2009
0

Re: c# Changing Textbox.Text from different class

I figured it out: in the Program.cs class i added

C# Syntax (Toggle Plain Text)
  1. public static Form1 mainForm;
  2. [STAThread]
  3. static void Main()
  4. {
  5. Application.EnableVisualStyles();
  6. Application.SetCompatibleTextRenderingDefault(false);
  7. mainForm = new Form1();
  8. Application.Run(mainForm)
  9. }
This adds reference to form1 and now to change the text I go
Program.mainForm.AddToText("test"); and it works. Thanks for all the help!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mastermosley is offline Offline
13 posts
since Mar 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Excel and C#
Next Thread in C# Forum Timeline: Assign a variable from a combo box





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC