471,330 Members | 1,674 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,330 software developers and data experts.

Passing control name to a method in vs2005

How do I pass a control name to a method?
I have a Textbox named txt1; in the form load event I want to pass txt1 to a
method as:

private void MyForm_Load(object sender, EventArgs e)
{
MyMethod(txt1)
}

private Void MyMethod(txt1)
}
txt1.text = "My Value";
}
Sep 8 '08 #1
4 1314
On Sep 8, 11:27*am, Ryan <R...@discussions.microsoft.comwrote:
How do I pass a control name to a method?
I have a Textbox named txt1;
How it's named? notice that is not the same set the ID="XXXX" in the
aspx.

also note in your code that you use everywere the same name txt1. This
can lead to error or confusion.
If txt1 is a member of the class, then you do not need to pass it as a
parameter to MyMethod
in the form load event I want to pass txt1 to a
method as:

private void MyForm_Load(object sender, EventArgs e)
* * {
* * * * MyMethod(txt1)
* * }

private Void MyMethod(txt1)}

* *txt1.text = "My Value";

}- Hide quoted text -

- Show quoted text -
Sep 8 '08 #2

"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.com...
How do I pass a control name to a method?
I have a Textbox named txt1; in the form load event I want to pass txt1 to
a
method as:

private void MyForm_Load(object sender, EventArgs e)
{
MyMethod(txt1)
}

private Void MyMethod(txt1)
}
txt1.text = "My Value";
}

you could do the below
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
MyMethod(this.txt1,null);
}

private void MyMethod(object Sender,EventArgs e)
{
Control oControl;
if (Sender is TextBox)
{
oControl = (TextBox)Sender;
Console.WriteLine(oControl.Text);
// Result Hello world

}
}

}
}
Sep 8 '08 #3
Thanks, it worked.

"DaveL" wrote:
>
"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:76**********************************@microsof t.com...
How do I pass a control name to a method?
I have a Textbox named txt1; in the form load event I want to pass txt1 to
a
method as:

private void MyForm_Load(object sender, EventArgs e)
{
MyMethod(txt1)
}

private Void MyMethod(txt1)
}
txt1.text = "My Value";
}


you could do the below
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
MyMethod(this.txt1,null);
}

private void MyMethod(object Sender,EventArgs e)
{
Control oControl;
if (Sender is TextBox)
{
oControl = (TextBox)Sender;
Console.WriteLine(oControl.Text);
// Result Hello world

}
}

}
}
Sep 8 '08 #4
you could do the below
namespace TestForm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
MyMethod(this.txt1,null);
Better to use EventArgs.Empty instead of null.
}

private void MyMethod(object Sender,EventArgs e)
Second parameter isn't needed, but ok if this function needs to also be
wired to events
{
Control oControl;
if (Sender is TextBox)
{
oControl = (TextBox)Sender;
Console.WriteLine(oControl.Text);
// Result Hello world

}
}

}
}

Sep 8 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by ad | last post: by
14 posts views Thread by joey.powell | last post: by
7 posts views Thread by =?Utf-8?B?UGV0ZXI=?= | last post: by
9 posts views Thread by Chris | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.