Reputation: 13273
My question is simple.
I have a method within a class and I want this method to change the text in a TextBox
.
The TextBox
might change during runtime so I'm trying to find a way to pass the TextBox
control as parameter when I call the method.
Is this even possible ?
Thanks in advance.
Upvotes: 2
Views: 11975
Reputation: 2189
If you want to use the controls as an argument in the class you need to use their namespaces on the top such as ;
using System.Windows.Forms;
namespace myprojectnamespace
{
class myClass
{
public void myMethod(TextBox mytex, RichTextBox searchStr)
{
// ..Codes Here
}
}
Upvotes: 0
Reputation: 3793
Just to add little more to this :
You can pass any class , Interface, delegate , struct to a method as parameter. In your scenario TextBox is a class so you can pass it to method as parameter.
When you pass any reference type (except string) to a Method , no cloning on the passed object is done and changes to passed object will reflect sent object. e,g :
void Method1 ()
{
DataSet ds = new DataSet();
..do some opeartion on ds.....
Method2(ds);
..print details of ds
}
Method2(DataSet myds)
{
..do something to ds
}
You will notice that in in Method1 after calling Method2 the dataset object ds is changed.
For case number 2 above to apply for value types, pass the parameters as ref :
void Method2(ref int count)
{
count = count++;
}
Here if you pass any integer to this method then passed integer will result in change.
Upvotes: 2
Reputation: 292375
Yes of course, it's absolutely possible... a control is an object like any other, so it can be passed as a parameter or stored in a variable
void SayHello(TextBox textBox)
{
textBox.Text = "Hello world";
}
...
SayHello(textBox1);
Upvotes: 6