c# Changing Textbox.Text from different class

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2009
Posts: 12
Reputation: mastermosley is an unknown quantity at this point 
Solved Threads: 0
mastermosley mastermosley is offline Offline
Newbie Poster

c# Changing Textbox.Text from different class

 
0
  #1
Mar 10th, 2009
I have the function AddToText which is located in the Form1 Partial Class

  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:
  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?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: mastermosley is an unknown quantity at this point 
Solved Threads: 0
mastermosley mastermosley is offline Offline
Newbie Poster

Re: c# Changing Textbox.Text from different class

 
0
  #2
Mar 10th, 2009
The top code is actually:
  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
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: mastermosley is an unknown quantity at this point 
Solved Threads: 0
mastermosley mastermosley is offline Offline
Newbie Poster

Re: c# Changing Textbox.Text from different class

 
0
  #3
Mar 10th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 44
Reputation: hieuuk is an unknown quantity at this point 
Solved Threads: 4
hieuuk hieuuk is offline Offline
Light Poster

Re: c# Changing Textbox.Text from different class

 
0
  #4
Mar 10th, 2009
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");
.Net Developer - 3D Game Designer
My Portfolio/Blog: http://www.hieu.co.uk
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,160
Reputation: dickersonka will become famous soon enough dickersonka will become famous soon enough 
Solved Threads: 137
dickersonka dickersonka is offline Offline
Veteran Poster

Re: c# Changing Textbox.Text from different class

 
0
  #5
Mar 10th, 2009
Just to be sure and check all sides of the problem

I noticed:
  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
  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.
Custom Application & Software Development
www.houseshark.net
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 12
Reputation: mastermosley is an unknown quantity at this point 
Solved Threads: 0
mastermosley mastermosley is offline Offline
Newbie Poster

Re: c# Changing Textbox.Text from different class

 
0
  #6
Mar 10th, 2009
I figured it out: in the Program.cs class i added

  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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC