Click here to Skip to main content
15,881,719 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
which button property is changed when a button is being clicked?
is that property same for the dynamic buttons also?
can any one give an example?
Posted

No button property is changed when a button is clicked: you get an event (the Button.Click event) so that you can do your thing when it happens.

Highlight your button, and look in the Properties pane of VS. Press the "lightning flash" button to show the available events, then double click on "Click" to add a handler.

The same thing happens with dynamic buttons, but you have to add the handler manually when you create them. VS can help: type your button creation code then type myButton.Click +=
Button myButton = new Button();
myButton.Click +=
Press [TAB] twice and it will be filled in for you.


"if the buttons are created by a dynamic button array,how can we identify a clicked button,among the other dynamic buttons in an array?"


Easy - it is handed to the Event handler as the "sender" parameter:

C#
private void myButton_Click(object sender, EventArgs e)
   {
   Button pressed = sender as Button;
   if (pressed != null)
      {
      // "pressed" is the button the user clicked
      }
   }
The only reason to check for null is that it is possible to call event handlers directly: not advisable, but possible. If this happens (or the event is hooked to a different control type) then it will not throw an exception.
 
Share this answer
 
v2
Comments
CS2011 24-Mar-11 4:23am    
my 5+
Member 7779792 24-Mar-11 4:33am    
if the buttons are created by a dynamic button array,how can we identify a clicked button,among the other dynamic buttons in an array?
OriginalGriff 24-Mar-11 4:46am    
Answer updated
Sharma Richa 24-Mar-11 4:51am    
my +5
Sergey Alexandrovich Kryukov 24-Mar-11 12:27pm    
So detailed! A 5.
--SA
None. Period. No examples. Read on how events works.

—SA
 
Share this answer
 
Comments
OriginalGriff 24-Mar-11 4:48am    
Who univoted that? Compensated.

Brusque? Yes. Short? Yes. Correct? Yes. No reason to down vote that I can see, SA!
Sergey Alexandrovich Kryukov 24-Mar-11 12:26pm    
Thank you, Griff. It happens all the time now, the more morons are pupulating CodeProjec these days...
--SA
hi,

when u click a button the Look and feel u can change Of Button in eventHandler function
C#
private void button1_MouseHover(object sender, EventArgs e) {
    // when we hoover the button we get this
    button1.BackColor = Color.LightGreen;
}
private void button1_MouseLeave(object sender, EventArgs e) {
    // when we again leave the button we get back original color
    button1.BackColor = Color.Aqua;
}
private void Form1_Load(object sender, EventArgs e) {
    // Lets start with this color
    button1.BackColor = Color.Aqua;
}


[edit]Spurious in line code, big, code block removed from end of reply - OriginalGriff[/edit]
 
Share this answer
 
v2
Comments
Member 7779792 24-Mar-11 5:08am    
how can we apply this for a button in a button array???

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