473,503 Members | 3,739 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calling Event Handler

Hey Guys,
I have a problem. I need to call event handler of buttons placed
on a form on its keypress event. The Problem is i need to read a file
which includes the button name whose click event is to be called and
the KEY whose press event whould trigger the respective button's click
event. e.g. The Mapper.txt file contains these values like :

btnSubmit S
btnCancel X
btnReset M

Here this file means that if "S" is pressed on the form it should
trigger the click event of btnSubmit OR
if "X" is pressed on the form it should trigger the click event of
btnCancel.

Can this be done without using "if else" conditions, because the
"Mapper.txt" file can be changed by user.
Hope u understood the problem.

Thanks,
Nitin Agarwal.

May 30 '07 #1
4 2384
Nitin,

Why not parse the file and then assign the mappings to a
Dictionary<string, Button>.

Then, when a key is pressed, look for the key in the dictionary. If it
exists, get the button and then call the PerformClick method.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Nitin" <ni***********@gmail.comwrote in message
news:11**********************@o11g2000prd.googlegr oups.com...
Hey Guys,
I have a problem. I need to call event handler of buttons placed
on a form on its keypress event. The Problem is i need to read a file
which includes the button name whose click event is to be called and
the KEY whose press event whould trigger the respective button's click
event. e.g. The Mapper.txt file contains these values like :

btnSubmit S
btnCancel X
btnReset M

Here this file means that if "S" is pressed on the form it should
trigger the click event of btnSubmit OR
if "X" is pressed on the form it should trigger the click event of
btnCancel.

Can this be done without using "if else" conditions, because the
"Mapper.txt" file can be changed by user.
Hope u understood the problem.

Thanks,
Nitin Agarwal.

May 30 '07 #2
On Wed, 30 May 2007 13:50:03 -0700, Nicholas Paldino [.NET/C# MVP]
<mv*@spam.guard.caspershouse.comwrote:
Why not parse the file and then assign the mappings to a
Dictionary<string, Button>.

Then, when a key is pressed, look for the key in the dictionary. If
it
exists, get the button and then call the PerformClick method.
And by "get the button", I expect that Nicholas is referring to the fact
that you can look a button (or any control) up by name:

((Button)Controls[strButtonName]).PerformClick;

Where the code is in the form containing the button, and the variable
"strButtonName" is a string set to the button's name (presumably the one
you get back from the Dictionary when you look up the key character).

Depending on where the data is actually coming from and how much
validation has already been done, you may need to check the results of the
"Controls[strButtonName]" expression first, to avoid a null reference
exception. It probably makes more sense though to do that validation when
you read the file and create the dictionary, and just not include invalid
control names in the dictionary in the first place (and perhaps display an
error to the user so that they know there are invalid data in the file).

Pete
May 30 '07 #3
On May 31, 2:11 am, "Peter Duniho" <NpOeStPe...@nnowslpianmk.com>
wrote:
On Wed, 30 May 2007 13:50:03 -0700, Nicholas Paldino [.NET/C# MVP]

<m...@spam.guard.caspershouse.comwrote:
Why not parse the file and then assign the mappings to a
Dictionary<string, Button>.
Then, when a key is pressed, look for the key in the dictionary. If
it
exists, get the button and then call the PerformClick method.

And by "get the button", I expect that Nicholas is referring to the fact
that you can look a button (or any control) up by name:

((Button)Controls[strButtonName]).PerformClick;

Where the code is in the form containing the button, and the variable
"strButtonName" is a string set to the button's name (presumably the one
you get back from the Dictionary when you look up the key character).

Depending on where the data is actually coming from and how much
validation has already been done, you may need to check the results of the
"Controls[strButtonName]" expression first, to avoid a null reference
exception. It probably makes more sense though to do that validation when
you read the file and create the dictionary, and just not include invalid
control names in the dictionary in the first place (and perhaps display an
error to the user so that they know there are invalid data in the file).

Pete
Hey Pete I'm trying it over Pocket PC 2003 in which i dont get the
control like this ((Button)Controls[strButtonName]), it can be
accessed only through the control's index. So what should i do.

Jun 5 '07 #4
On Mon, 04 Jun 2007 22:17:11 -0700, Nitin <ni***********@gmail.comwrote:
Hey Pete I'm trying it over Pocket PC 2003 in which i dont get the
control like this ((Button)Controls[strButtonName]), it can be
accessed only through the control's index. So what should i do.
It should not be difficult for you to figure out how to write a loop that
searches the ControlCollection by index, comparing each control's name
with the one you're looking for. Right? :)
Jun 5 '07 #5

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

Similar topics

14
9551
by: Michael Winter | last post by:
In an attempt to answer another question in this group, I've had to resort to calling the DOM method, Node.removeChild(), using a reference to it (long story...). That is, passing Node.removeChild....
2
1674
by: Andrew Banks | last post by:
I've got the following code as part of a C# web form but am having problems calling a command. I create a dataset and put some data on the screen. This works fine. (relevant sample below) ...
0
1365
by: harmeet | last post by:
hi all, i am implementing command pattern in my project and adding event handler for tollbar buttonclick event. while i click on any button it notify multiple times due to multiple event handler...
2
8327
by: Breeto | last post by:
Can anyone please tell me why the following doesn't work... using System; using System.Web; namespace AspTests {
3
4240
by: Jason | last post by:
I have an ASP.NET application in which I would like to call my button click event (imgSubmitSearch_Click) on the page load if certain criteria are met. Is this possible? What is the correct...
3
1312
by: Cheryl | last post by:
Dear all, I have a question on implementing a callback function. Suppose I have a main class which contains a static class to communicate with a remote server. I also have an array of objects...
1
4730
by: ravikumar2007 | last post by:
I am working with .NET 3.5 framework within an WPF application. I have a windows application where for a textbox, I have defined the lost focus event. I need to call the Focus() method of any other...
1
1609
by: H F | last post by:
Hi all I created a user control that has a button and have added it to the Master page. When I click on the button, the onClick event handler in the user control is not fired. How do I reference...
22
1665
by: DL | last post by:
Hi, What I wanted to do is to call a function from a newly created element. But it stumbled me. Here's the line that references the newly created element and I used the alert function for...
9
3234
by: Pubs | last post by:
Hi all, I want to call a function with some intial parameters with in a thread. At the end of the function execution it should return a value to the caller. Caller is outside the thread. ...
0
7064
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
7261
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7315
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
7445
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
5559
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,...
1
4991
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...
0
4665
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1492
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
369
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.