Click here to Skip to main content
15,905,323 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have several buttons emulating a keyboard...well..everytime you click on a key, the program must redirect to the page of its key, ok?...Is there a better way of doing this than putting this on every click() event?

C#
private void btnA_Click(object sender, EventArgs e)
        {
            ObtenerResultados(((Button) sender).Text);
        }

C#
private void btnB_Click(object sender, EventArgs e)
        {
            ObtenerResultados(((Button) sender).Text);
            Application.DoEvents();
        }


Is the same call for all buttons...maybe there is a faster way?

Thanks
Posted
Updated 21-Dec-10 7:44am
v2

I don't really follow you, but your buttons can all share the same click handler (you only need a single "btn_Click" rather than a "btnA_Click", "btnB_Click", and so on). I'm not sure if you're using ASP.Net or Windows Forms (or WPF), so I don't really want to provide you with sample code that might confuse you. Basically, go to where the click handler is assigned and choose a different one.
 
Share this answer
 
Why are you calling DoEvents from an event handler?

Just create one button click handler and do what you're already doing:


C#
private void btnLetters_Click(object sender, EventArgs e)        
{
    ObtenerResultados(((Button) sender).Text);        
}


And then attach that event to all of the applicable buttons.

 
Share this answer
 
v5
Comments
Sergey Alexandrovich Kryukov 21-Dec-10 23:08pm    
DoEvents skips event cycle of the UI thread until all pending message are processed. In almost all cases, it's a bad idea to use it.
Ok, more exactly, you need one handler for all buttons representing a keyboard, but you still need information on what exactly key was pressed. Also, you need to use better syntax -- the auto-generated syntax is gravely obsolete (you're interested in v.4.0, huh?) and inferior relative the lambda syntax.

The code will be something like this:

C#
for (Button button in KeyboardButtons)
   //in the following line, you're sure sender can be cast to Button:
   button.Click += (sender, args) => { ClickHandler((Button)sender); };

//...

//you don't have to use args parameter:
void ClickHandler(Button sender) {
    string text = sender.Text; //in WPF it would be (string)sender.Content
    Debug.Assert(text.Length > 0);
    char key = text[1];
   //now you know which key is represented by the button pressed
   //...
} //ClickHandler

..//
 
Share this answer
 
v5
Comments
Espen Harlinn 26-Feb-11 11:02am    
Right, my 5
Sergey Alexandrovich Kryukov 26-Feb-11 19:35pm    
Thank you.
--SA
_Ashish 27-Feb-11 6:32am    
its simpler and thoughful +5
Some new references on Virtual Keyboard topic; I answered few related Questions, so some important aspects are summarized here: Application focus getting and losing[^] and here: Programming on BACKSPACE button[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900