malben
asked on
Setting GUI object from another class other than GUI class. How do you do it.
Hi,
I've created an GUI form in C# on .NET (called form1). I also have another class (class1) with no GUI stuff in it, what I want to do is for a function in Class1 to set a label on form1.
But, Im not sure how you do it? I've tried to inherit form1 into Class1, but with no luck.
Would anyone know how I should do it?
Thanks in advance for your help!
I've created an GUI form in C# on .NET (called form1). I also have another class (class1) with no GUI stuff in it, what I want to do is for a function in Class1 to set a label on form1.
But, Im not sure how you do it? I've tried to inherit form1 into Class1, but with no luck.
Would anyone know how I should do it?
Thanks in advance for your help!
ASKER
I'm think passing a reference would be the best option.
I'm new to programming so I wonder if you could help with the syntax i.e. what should I write below?
Thanks for your help
using System;
namespace WindowsApplication1
{
public class Class1()
{
public Class1()
}
}
}
I'm new to programming so I wonder if you could help with the syntax i.e. what should I write below?
Thanks for your help
using System;
namespace WindowsApplication1
{
public class Class1()
{
public Class1()
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Many thanks bob!
Is that really what you wanted?
In Bobs example, Class1 is creating a new instance of Form1.
I was thinking that Form1 was creating an instance of Class1 and so we needed to pass a reference to Form1 into Class1 so that the class could manipulate the form...
In Bobs example, Class1 is creating a new instance of Form1.
I was thinking that Form1 was creating an instance of Class1 and so we needed to pass a reference to Form1 into Class1 so that the class could manipulate the form...
ASKER
Idle Mind. how would that look like as code?
Thanks
Thanks
At it's simplest... (this assumes that on Form1, the Modifiers() property of textBox1 has been changed to Internal)
public partial class Form1 : Form
{
private void button1_Click_1(object sender, EventArgs e)
{
Class1 c1 = new Class1();
c1.frm1 = this;
c1.Foo();
}
}
public class Class1
{
public Form1 frm1;
public void Foo()
{
if (frm1 != null)
{
frm1.textBox1.Text = "Hello from Class1";
}
}
}
public partial class Form1 : Form
{
private void button1_Click_1(object sender, EventArgs e)
{
Class1 c1 = new Class1();
c1.frm1 = this;
c1.Foo();
}
}
public class Class1
{
public Form1 frm1;
public void Foo()
{
if (frm1 != null)
{
frm1.textBox1.Text = "Hello from Class1";
}
}
}
ASKER
Many thanks Idle Mind your help was much appreciated!
How about:
(1) Pass a reference to either Form1 or your Label into your Class so you can directly manipulate them from within the Class
(2) Make your Class raise a custom event that Form1 subscribes to. When it receives this event, it uses the parameters to update the appropriate label.
Do you have a preference between these two methods?