| | |
c# Changing Textbox.Text from different class
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2009
Posts: 12
Reputation:
Solved Threads: 0
I have the function AddToText which is located in the Form1 Partial Class
I want to call this from a different class so I do:
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?
C# Syntax (Toggle Plain Text)
public void AddToText(string text) { this.txtStatus.Text = this.txtStatus.Text + "\r\n"; }
C# Syntax (Toggle Plain Text)
Form1 frm = new Form1(); frm.AddToText("Test");
WTF?
•
•
Join Date: Mar 2009
Posts: 12
Reputation:
Solved Threads: 0
The top code is actually:
And nothing is displayed at all in the text box
C# Syntax (Toggle Plain Text)
public void AddToText(string text) { this.txtStatus.Text = this.txtStatus.Text + "\r\n" + text; }
•
•
Join Date: Nov 2008
Posts: 44
Reputation:
Solved Threads: 4
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");
Form1 frm = new Form1();
frm.show();
frm.AddToText("Test");
.Net Developer - 3D Game Designer
My Portfolio/Blog: http://www.hieu.co.uk
My Portfolio/Blog: http://www.hieu.co.uk
•
•
Join Date: Aug 2008
Posts: 1,160
Reputation:
Solved Threads: 137
Just to be sure and check all sides of the problem
I noticed:
if its a one line textbox you won't be able to see the output, for testing purposes, try changing it to this
also be sure that in form shown you aren't setting the value of the textbox
I noticed:
C# Syntax (Toggle Plain Text)
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)
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
www.houseshark.net
•
•
Join Date: Mar 2009
Posts: 12
Reputation:
Solved Threads: 0
I figured it out: in the Program.cs class i added
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!
C# Syntax (Toggle Plain Text)
public static Form1 mainForm; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); mainForm = new Form1(); Application.Run(mainForm) }
Program.mainForm.AddToText("test"); and it works. Thanks for all the help!
![]() |
Similar Threads
- Tutorial: Automating Office applications (Visual Basic 4 / 5 / 6)
- how to get value of all selected checkbox? (ASP.NET)
- Newbie Having Trouble (ASP.NET)
- Need help with SMS program (VB.NET)
- help with radio buttons and check boxes ASAP (C++)
- Help me to develop Messenger Project (IT Professionals' Lounge)
Other Threads in the C# Forum
- Previous Thread: Excel and C#
- Next Thread: Assign a variable from a combo box
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client combobox connection console control conversion csharp custom database datagrid datagridview dataset datetime degrees deployment developer development draganddrop drawing editing encryption enum event excel file form format forms function gdi+ hospitalmanagementinformationsystem httpwebrequest image imageprocessing index input install java label list listbox mandelbrot math mouseclick mysql operator oracle path photoshop picturebox pixelinversion post priviallages. programming radians regex remote remoting richtextbox rows serialization server sleep socket sql statistics stream string table temperature text textbox thread time timer txt update uploadatextfile usercontrol validation visualstudio webbrowser windows windowsformsapplication winforms wpf xml






