Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
private void EnterPressed( object sender, KeyEventArgs e )
        {
            if ( e.KeyCode == Keys.Enter )
            {
                if ( this.SignButton.Enabled == true &&
                   ( sender is TextBox )
                   )
                {
                    this.SignButton_Click( sender, e );
                } // if
            } // if
        }

My Form consists of many TextBox Controls, how it will distinguish between the sender.

If i pass a particular textbox name, it is throwing error as "Is a field but used as type"
Posted
Comments
Shreyas Tg 8-Oct-15 5:49am    
You should pass the ID right?
BillWoodruff 8-Oct-15 22:42pm    
Does each TextBox have a different Button "associated" with it ?

So there might be 'TextBox5 and a Button 'Button5 which you will want to test the 'Enabled state ?

Is this a calculator project ?

1 solution

All you need to do here is to type cast the sender object to a TextBox Control and check to see is the control Id. Something like this..

C#
TextBox ctrl = (TextBox) sender;

if (ctrl != null)
{
// Do something
// Check for condition based on control Id
if (ctrl.Id.equals("txt1"))
{
  //do some thing
}
else if (ctrl.Id.equals("txt2")
{
 //  do something else..
}
}
 
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