Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

C# Programming

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.

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]
          - m@spam.guard.caspershouse.com

"Nitin" <nitin.84in@gmail.com> wrote in message

news:1180557200.547157.179370@o11g2000prd.googlegroups.com...

On Wed, 30 May 2007 13:50:03 -0700, Nicholas Paldino [.NET/C# MVP]  

<m@spam.guard.caspershouse.com> wrote:
>     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

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc