Writing to TextBox from Seperate Class

J

JP

I'm sure this is a simple question and I'm overlooking the obvious. But, I
have two seperate classes (in seperate files) under the same namespace. I
am calling a method in one class that contains
public System.Windows.Forms.TextBox textBox1;

When I try to write to this textbox it never publishes anything. If I call
the same method from inside the same class it does. How can I publish to
this textbox from an external class. Simple example would be:

FILE: classone.cs



namespace DemoApp

{

public class classone : System.Windows.Forms.Form

{

public System.Windows.Forms.TextBox textBox1;

private System.ComponentModel.Container components = null;

public classone()

{

InitializeComponent();

}

private void InitializeComponent()

{



this.textBox1 = new System.Windows.Forms.TextBox();

((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit();

//

// textBox1

//

this.textBox1.Location = new System.Drawing.Point(0, 32);

this.textBox1.Multiline = true;

this.textBox1.Name = "textBox1";

this.textBox1.ReadOnly = true;

this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;

this.textBox1.Size = new System.Drawing.Size(568, 344);

this.textBox1.TabIndex = 4;

this.textBox1.Text = "textBox1";

//

// classone

//

this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

this.ClientSize = new System.Drawing.Size(568, 373);

this.Controls.Add(this.textBox1);



this.Load += new System.EventHandler(this.Form1_Load);

((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit();

this.ResumeLayout(false);

}

public void addstatus(string status)

{

MessageBox.Show(status);

this.textBox1.AppendText("HI");

this.textBox1.AppendText(status + "\r\n\r\n");


}

}

}

FILE classtwo.cs

namespace WxAlert

{

public void addToreport(string rep, int debugDetail)

{

classone obj = new classone();

obj.addstatus(rep);

}

}



When calling the addToreport method, everything compiles and runs - just
nothing gets written to the textbox window. Not even the "HI" - the
information does show correctly in the MessageBox.
 
G

Guest

You only create the classone form but you never show it. This is what
addToreport should look like addToreport should look like

public void addToreport(string rep, int debugDetail)
{
Form2 obj = new Form2();
obj.addstatus(rep);
obj.Show();
}

/Patric Johansson
My C# blog: http://spaces.msn.com/members/pjsson
 
J

JP

Hmm.. Tried that and it just opened a lot of new windows without any change
to the textbox.

What the addToreport method is intending to do is send a string to the other
class which will then print that string in the textbox. I am using this for
reporting. so each time I call that method I want a string to be added to
the text box. Works perfectly if all my methods are in the class that
creates the form, but if I move things to a seperate class I can't seem to
create the same behavior.
 
A

Alex Passos

Ok, add this to your main form where the text box is

If let's say your main form class is called Form1 then:

public static Form1 MainForm = null;

Then on your form initialization set it to itself that way a reference to
your form is maintained. In your form you can setup some accessor
attributes like this:

public string ValueForTextBox
{
set
{
TextBox1.Text = value;
}
get
{
return TextBox1.Text;
}
}

on your other classes, if they are in the same namespace you can use
something like:

Form1.MainForm.ValueForTextBox = somevalue;
Try it and see if it will help you.
 
J

JP

Ok - think we're getting closer, so now I get an error:

'System.Windows.Forms.Control.text' is inaccessible due to its protection
level

in:

set

{

this.textBox1.text += value;

}
 
L

Ludwig Stuyck

Ok - think we're getting closer, so now I get an error:

'System.Windows.Forms.Control.text' is inaccessible due to its protection
level

in:

set

{

this.textBox1.text += value;

}

this.textBox1.Text += value;

with capital T
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top