473,221 Members | 2,135 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,221 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 1395
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Chaduke | last post by:
I wrote a simple client/server chat program and the client runs as an activex control within an asp page. I have the users logging into the page using widows authentication and I'm using...
3
by: Joe Bloggs | last post by:
Does anyone know if its possible to pass parameters or the values of Request.QueryString from a web page to a custom control class? I'm using a C# Web Application. For Example I have Web Page1...
6
by: n33470 | last post by:
Hi all, We have a user control that contains a DropDownList. This user control appears on a web page. Suppose the name of the userControl on the web page is called "ucLookup". Suppose the...
5
by: Len Weltman | last post by:
I am trying to pass a NumericUpDown object into a class method using Visual Studio 2005, but the control type is not found in Intellisense and the type declaration is flagged as an error. Here...
4
by: MohanDasGandhi | last post by:
Hi all, How the class name for windows standard controls(Ex:Button) are generated in VS2005? InVS 2005, I have created one button conrol and compiled. I got a class name as given below: ...
2
by: ad | last post by:
I use login control of VS2005 in my web application. The web applicaiton is developed in my notebook. It run ok in my notebook, but when I login in from another PC with the user ID and password,...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
7
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I'm new to Visual Studio 2005. I'm creating a windows application using Visual Basic. After I added a control to a form and added some codings to the control, I want to rename the control. ...
9
by: Chris | last post by:
Ok, so I have this sub I wrote, and I create a new instance of a UserControl: ctrlAPs tempctrl = new ctrlAPs(); Now, I would like to be able to use this sub I wrote for more than one...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.