2

C#.I have 18 buttons to select, but before I will choose a selection. How to enlarge Button with BackgroundImage when mouse point in it? It's like ToolTip, when you point the cursor it will show the Text. But in my case, it will enlarge the Button. Thanks

Button[] ButtonSelect = new Button[17];
for (i = 1; i <= 18; i++)
{
ButtonSelect[i] = new Button();
ButtonSelect[i].BackgroundImage = Properties.Resources.SelectImages[i];
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
Son Gozita
  • 67
  • 1
  • 7

1 Answers1

2

Make the size of the Button grow larger in MouseEnter event:

Button btn = (Button)sender; 
int width = btn.Size.Width;
int height = btn.Size.Height;
int larger = 10;
btn.Size = new Size(width + larger, height + larger);

Then in the MouseLeave event do the opposite by shrinking the button size.

You can hook up the events like this:

for (i = 1; i <= 18; i++)
{
ButtonSelect[i] = new Button();
ButtonSelect[i].BackgroundImage = Properties.Resources.SelectImages[i];
ButtonSelect[i].MouseEnter += new System.EventHandler(Btn_MouseEnter); 
ButtonSelect[i].MouseLeave += new System.EventHandler(Btn_MouseLeave); 
}
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321
  • I'm a newbie in C#. In what part of the code I will create this "Button btn = (Button)sender"? Is this under "private void"? Also, how will I create this "Btn_MouseEnter"? It seems easy but I don't know the right procedure? – Son Gozita Aug 09 '15 at 05:29
  • Put the top code snippet in a `private void Btn_MouseEnter(object sender, EventArgs e)` here is more info: http://stackoverflow.com/questions/8950063/using-a-mouseenter-event-to-scale-a-button I assume all 18 buttons will share the same subroutine's/void's for MouseEnter and MouseLeave. – Jeremy Thompson Aug 09 '15 at 05:29
  • I'll add "btn.Location=new Point(100,100)" under MouseEnter Event but when I point to another Button, the previous button is gone until all 18 buttons are invisible. How to fix this phenomenon? I want to fixed the position because the buttons at the right and bottom part of the Form will not fit in Form Size. Thanks in advance. – Son Gozita Aug 09 '15 at 05:57
  • The button is gone because its position is out of the bounds of the form. You're probably not using the Button cast `(Button)sender` ... thats my guess anyway – Jeremy Thompson Aug 09 '15 at 05:59