Calling Event Handler

N

Nitin

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
P

Peter Duniho

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
 
N

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.

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.
 
P

Peter Duniho

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? :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top