473,406 Members | 2,343 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,406 software developers and data experts.

Pass a Control name to an Event Handler

I have the following code in vs2005; line number is added for convenience:

1 private void Form1_Load(object sender, EventArgs e)
2 {
3 txtMyField.Enabled = true;
4 Form2 f = new Form2();
5 f.DoUpdate += new Form2.UpdateHandler(MyForm2_ButtonClicked);
6 }

7 private void MyForm2_ButtonClicked(object sender, ValuesUpdateEventArgs3 e)
8 {
9 string sValues = "";
10 sValues = e.Val1;
11 txtMyField.Text = sValues;
12 }

Currently, my field name txtMyField.text is hardcoded and gets a value from
sValues (line #10).
Instead of hardcoding a fieldname as in Line #10; how do I pass the control
name on line #5 to event in Line #7?
Something like:

5 f.DoUpdate += new
Form2.UpdateHandler(MyForm2_ButtonClicked(txtMyFie ld.Text));
Thanks in advance.
Sep 8 '08 #1
2 1712
"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.com...
>I have the following code in vs2005; line number is added for convenience:

1 private void Form1_Load(object sender, EventArgs e)
2 {
3 txtMyField.Enabled = true;
4 Form2 f = new Form2();
5 f.DoUpdate += new Form2.UpdateHandler(MyForm2_ButtonClicked);
6 }

7 private void MyForm2_ButtonClicked(object sender,
ValuesUpdateEventArgs3 e)
8 {
9 string sValues = "";
10 sValues = e.Val1;
11 txtMyField.Text = sValues;
12 }

Currently, my field name txtMyField.text is hardcoded and gets a value
from
sValues (line #10).
Instead of hardcoding a fieldname as in Line #10; how do I pass the
control
name on line #5 to event in Line #7?
Something like:

5 f.DoUpdate += new
Form2.UpdateHandler(MyForm2_ButtonClicked(txtMyFie ld.Text));
If you are using C# 2.0 or above, you can use an anonymous delegate to
"capture" the passed control. Something similar to the following (typed from
memry, untested):

5 f.DoUpdate += delegate(object sender, ValuesUpdateEventArgs3 e)
{ string sValues = ""; sValues = e.Val1; txtMyField.Text = sValues; };

Note that here you are "passing" txtMyField in Line 5 as you wanted. If
you need to reuse it in several places, you can move the code to a separate
metod and pass to it a reference to txtMyField:

5 f.DoUpdate += delegate(object sender, ValuesUpdateEventArgs3 e)
{ MyFunc(txtMyField,e); };

private void MyFunc(TextBox t, ValuesUpdateEventArgs3 e)
{
string sValues = "";
sValues = e.Val1;
t.Text = sValues;
}
Sep 8 '08 #2
I have read similiar code on Developerdex.com but couldn't accomplish what I
was looking for. But, somehow, I was able to understand your language;
thanks for your help.

"Alberto Poblacion" wrote:
"Ryan" <Ry**@discussions.microsoft.comwrote in message
news:61**********************************@microsof t.com...
I have the following code in vs2005; line number is added for convenience:

1 private void Form1_Load(object sender, EventArgs e)
2 {
3 txtMyField.Enabled = true;
4 Form2 f = new Form2();
5 f.DoUpdate += new Form2.UpdateHandler(MyForm2_ButtonClicked);
6 }

7 private void MyForm2_ButtonClicked(object sender,
ValuesUpdateEventArgs3 e)
8 {
9 string sValues = "";
10 sValues = e.Val1;
11 txtMyField.Text = sValues;
12 }

Currently, my field name txtMyField.text is hardcoded and gets a value
from
sValues (line #10).
Instead of hardcoding a fieldname as in Line #10; how do I pass the
control
name on line #5 to event in Line #7?
Something like:

5 f.DoUpdate += new
Form2.UpdateHandler(MyForm2_ButtonClicked(txtMyFie ld.Text));

If you are using C# 2.0 or above, you can use an anonymous delegate to
"capture" the passed control. Something similar to the following (typed from
memry, untested):

5 f.DoUpdate += delegate(object sender, ValuesUpdateEventArgs3 e)
{ string sValues = ""; sValues = e.Val1; txtMyField.Text = sValues; };

Note that here you are "passing" txtMyField in Line 5 as you wanted. If
you need to reuse it in several places, you can move the code to a separate
metod and pass to it a reference to txtMyField:

5 f.DoUpdate += delegate(object sender, ValuesUpdateEventArgs3 e)
{ MyFunc(txtMyField,e); };

private void MyFunc(TextBox t, ValuesUpdateEventArgs3 e)
{
string sValues = "";
sValues = e.Val1;
t.Text = sValues;
}
Sep 8 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Todd Schinell | last post by:
Back in July, Jeffery Tan posted this: http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&selm=OWOTdf0VDHA.2296%40cpmsftngxa06.phx.gbl In response as to how to get click events from a...
2
by: Ronnie Smith | last post by:
I am trying to pass a function (method) address to a user control to associate with an event. The small user control which sends events is contained in a larger user control group which is in...
4
by: The Alchemist | last post by:
I am having a problem with a dynamically-generated Datagrid. It is important to point out that this problem does not exist with a design-time created Datagrid, but only with a dynamically generated...
4
by: thomson | last post by:
Hi all, i do have a user control with 4 buttons, and all the events are firing properly, My problem is that i need to right an event handler in the user control, which gets fired after a...
0
by: Matthew | last post by:
All, I have searched google and the newsgroups but can't find anything the same as what I am experiencing (though I may have missed something). I have controls (textboxes) within UserControls...
5
by: Mike Hoff | last post by:
Hello, I am wondering if it is possible to pass a procedure name to another proc. I have been looking into delegates, but cannot seem to get the code correct. Basically what I have is a proc to...
2
by: Mike | last post by:
Hi, I am strugling with a simple problem which I can't seem to resolve. I have an asp.net page which contains a server-control (flytreeview, which is a kind of a tree to be exact). The tree is...
3
by: weird0 | last post by:
i am trying to add nodes to a node dynamically ... and assign its proerties to a node objects stored inside the array.... when the ShapeAdded event is fired... ...inside the if block... ...
24
by: =?Utf-8?B?U3dhcHB5?= | last post by:
Can anyone suggest me to pass more parameters other than two parameter for events like the following? Event: Onbutton_click(object sender, EventArgs e)" Event handler: button.Click += new...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.