displaying text from a different class into Form1.textBox1

P

polarz

I'm having trouble getting items from different classes to display in
my textBox.

e.g.

private void button2_Click(object sender, System.EventArgs e)
{
someData sd = new someData();
sd.showData();
}

class someData : Form1
{
public void showData()
{
textBox1.Text="Some Stuff Here";
}
}

nothing shows up in the textBox when I press the button

I also tried

class someData : SomeOtherClassHere
{
public void showData()
{
Form1 f = new Form1();
f.textBox1.Text="Some Complete BS Here";
}
}

which is what I'd like to do, use a form that uses a class other than
Form1

I'm a pretty new programmer and don't have much experience with
windows forms, any help is much appreciated. Thanks
 
M

Max Guernsey

polarz,

I'm not sure I understand your problem completely. I'm going to do the best
I can...

.... are you looking for something like the following?

#region Example code
public class SomeData : SomeOtherClassHere {
public void ShowData(Form1 form) {
Form1.textBox1.Text = "Some Stuff Here";
}
}

public class Form1 {
....
private void button2_Click(object sender, System.EventArgs e) {
SomeData sd = new SomeData();
Form1 theForm = (Form1) sender;
sd.ShowData(theForm);
}
#endregion

That will create a class named SomeData whose instances have a method that
can effect an instance of the Form1 class in such a way as to modify the
Text property of their textBox1 field.

Is that what you wanted? If not, please clarify.

Thank you,
Max Guernsey
http://www.harbingersoftware.com
 
P

polarz

Hello Max, thanks for the reply. When I try to use your code I get an
unspecified cast error. I see the logic of your code but I can't get
it to work. I have the class in a different file named Class2.cs, but
it's in the same namespace. Here is how I have my code.

class someData : System.ApplicationException //this is in Class2.cs
{
public void ShowMyData(NewsMyWay.Form1 form)
{
form.textBox1.Text = "Some Stuff Here";
}
}

private void button2_Click(object sender, System.EventArgs e)
{
someData sd = new someData();
Form1 theForm = (Form1) sender;
sd.ShowMyData(theForm);

}

Basicly I'm trying to alter a textBox and a DataGrid from another
class and am having trouble with both. I'm sure this is a simple
solution but I can't see it :/

On Fri, 30 Jan 2004 03:53:01 -0800, "Max Guernsey"

I'm not sure I understand your problem completely. I'm going to do
the best
I can...

.... are you looking for something like the following?

#region Example code
public class SomeData : SomeOtherClassHere {
public void ShowData(Form1 form) {
Form1.textBox1.Text = "Some Stuff Here";
}
}

public class Form1 {
....
private void button2_Click(object sender, System.EventArgs e) {
SomeData sd = new SomeData();
Form1 theForm = (Form1) sender;
sd.ShowData(theForm);
}
#endregion

That will create a class named SomeData whose instances have a method
that
can effect an instance of the Form1 class in such a way as to modify
the
Text property of their textBox1 field.

Is that what you wanted? If not, please clarify.

Thank you,
Max Guernsey
http://www.harbingersoftware.com
 
P

polarz

here is a solution to my problem if anyone is interested

((Form1)NewsMyWay.Form1.ActiveForm).textBox1.Text="Some text here";
 

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