Use a control from a class function
Can someone give me a simple but working exemple of the method to use a control from a external class function ?
I mean by exemple, I have a textbox on my form and a button, bwhen I click the button it call a function from another class and that function change de textbox.text.
Thanks in advance.
Re: Use a control from a class function
You can a property of type TextBox that returns the instance of your TextBox or make this public and pass a reference of the instance of your Form or TextBox (anyway you want). Something like this.
VB Code:
private void Form1_Load(object sender, System.EventArgs e)
{
new Class1(this).ChangeTextBoxTextTo("Some text");
}
public TextBox MyTextBox
{
get{ return textBox1;}
set{ textBox1=value;}
}
and Class1 is something like this
VB Code:
using System;
using System.Windows.Forms;
namespace test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
Form1 caller;
public Class1(Form1 caller)
{
this.caller=caller;
}
public void ChangeTextBoxTextTo(string text)
{
caller.MyTextBox.Text=text;
}
}
}
Or there is an alternative making your TextBox a static which is not a member of the instance of your Form1 but as said (not a member of the instance of the Form), you can see it on the designer mode of your IDE. If you're interested, it's like this
VB Code:
public static TextBox txt=new TextBox();
private void Form1_Load(object sender, System.EventArgs e)
{
this.Controls.Add(txt);
new Class1().ChangeTextBoxTextTo("Some text");
}
and the Class1 is something like
VB Code:
using System;
using System.Windows.Forms;
namespace test
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
public Class1()
{
//
// TODO: Add constructor logic here
//
}
public void ChangeTextBoxTextTo(string text)
{
Form1.txt.Text=text;
}
}
}
Is this what you want, or I am missing something?
Re: Use a control from a class function
Thanks you, it's working ;)