главная продукты downloads форум тех. поддержка

 

Главная страница

Windows Forms Button

FAQ Home

1. How do I autosize a button to fit its text?
2. How can I decorate a button face with custom drawing?
3. How can I put a bitmap or icon on a button face?
4. How can I trigger a button click event?
5. How can I get button to fire a click event at specific time intervals while the mouse is down like a scrollbar button?
 



1 How do I autosize a button to fit its text?

Get a Graphics object for the button and use its MeasureString method to compute the width you need.

     Graphics g = button1.CreateGraphics();
     float w = g.MeasureString(button1.Text, button1.Font).Width;
     g.Dispose();
     button1.Width = (int) w + 12; // 12 is for the margins
 



2 How can I decorate a button face with custom drawing?

Subclass Button and add a custom Paint event handler that does you custom drawing.

     public class MyButton : System.Windows.Forms.Button
     {
          public MyButton()
          {
               this.Paint += new System.Windows.Forms.PaintEventHandler(this.button1_Paint);
          }
          private void button1_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
          {
               //custom drawing
               Pen pen2 = new Pen(Color.Red);
               pen2.Width = 8;
               e.Graphics.DrawLine(pen2, 7, 4, 7, this.Height - 4);
               pen2.Width = 1;
               e.Graphics.DrawEllipse(pen2, this.Width - 16 , 6, 8, 8);
          }
     }
 



3 How can I put a bitmap or icon on a button face?

You can use the Button.Image property to add an image to a button face. Use the Button.ImageAlign (and possibly Button.TextAlign) to layout your button face.
You can also implement custom drawing on a button face as described in the FAQ entitled "How can I decorate a button face with custom drawing".
 



4 How can I trigger a button click event?

Use the button's public method PerformClick.button1.PerformClick();

 



5 How can I get button to fire a click event at specific time intervals while the mouse is down like a scrollbar button?

Shawn Burke posted the following solution on the microsoft.public.dotnet.framework.windowsforms newsgroup.

public class RepeatButton : Button
{
     private Timer timer1;
     public int Interval
     {
          get
          {
               return Timer.Interval;
          }
          set
          {
               Timer.Interval = value;
          }
     }

     private Timer Timer
     {
          get
          {
               if (timer1 == null)
               {
               // create and setup our timer.
timer1 = new Timer();
               timer1.Tick += new EventHandler(OnTimer);
               timer1.Enabled = false;
               }
               return timer1;
          }
     }

     protected override void OnMouseDown(MouseEventArgs me)
     {
          base.OnMouseDown(me);
          // turn on the timer
          Timer.Enabled = true;
     }

     protected override void OnMouseUp(MouseEventArgs me)
     {
          // turn off the timer
          Timer.Enabled = false;
          base.OnMouseUp(me);
     }

    private void OnTimer(object sender, EventArgs e)
     {
          // fire off a click on each timer tick
OnClick(EventArgs.Empty);
}
}

 

e-mail:

 

bousoft@mail.ru