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

public void AddToText(string text)
{
this.txtStatus.Text = this.txtStatus.Text + "\r\n";
}

I want to call this from a different class so I do:

Form1 frm = new Form1();
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?

The top code is actually:

public void AddToText(string text)
{
this.txtStatus.Text = this.txtStatus.Text + "\r\n" + text;
}

And nothing is displayed at all in the text box

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.

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");

Just to be sure and check all sides of the problem

I noticed:

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

this.txtStatus.Text = text;

also be sure that in form shown you aren't setting the value of the textbox

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

public static Form1 mainForm;
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
mainForm = new Form1();
Application.Run(mainForm)
}

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!

This article has been dead for over six months. Start a new discussion instead.