Button Flat Style : Button « GUI Windows Forms « C# / CSharp Tutorial






Button Flat Style
using System;
using System.Drawing;
using System.Windows.Forms;

public class ButtonFlatStyle : Form
{
  Button btn;
  int i = 1;
  FlatStyle[] flatStyles;
  Image img;

  public ButtonFlatStyle()
  {
        Text = "Button Properties";
    Size = new Size(300,200);

    img = Image.FromFile("YourFile.bmp");

    btn = new Button();
    btn.Parent = this;
    btn.Text = btn.FlatStyle.ToString();
    btn.Location = new Point(10,10);

    btn.Click += new System.EventHandler(btn_Click);
    btn.Image = img;

    ButtonSize(btn);

    FlatStyle theEnum = new FlatStyle();
    flatStyles = (FlatStyle[])Enum.GetValues(theEnum.GetType());
  }

  static void Main() 
  {
    Application.Run(new ButtonFlatStyle());
  }

  private void btn_Click(object sender, EventArgs e)
  {
    Button btn = (Button)sender;
    btn.FlatStyle = flatStyles[i];
    btn.Text = btn.FlatStyle.ToString();
    ButtonSize(btn);

    if (i < flatStyles.Length - 1)
      i++;
    else
      i = 0;
  }

  private void ButtonSize(Button btn)
  {
    int xSize = ((int)(Font.Height * .75) * btn.Text.Length) + (img.Width * 2);
    int ySize = img.Height * 2;
    btn.Size = new Size(xSize, ySize);
  }
}








23.8.Button
23.8.1.Add a Button
23.8.2.Handle button messages
23.8.3.Relocate button after pressing button
23.8.4.Add Image in an ImageList to a ButtonAdd Image in an ImageList to a Button
23.8.5.Use delegate to add an event handler to a ButtonUse delegate to add an event handler to a Button
23.8.6.Button mouse eventButton mouse event
23.8.7.Set Label text in a button click eventSet Label text in a button click event
23.8.8.Add action handler to buttonAdd action handler to button
23.8.9.Button Style: image button, standard buttonButton Style: image button, standard button
23.8.10.Button PerformClick MethodButton PerformClick Method
23.8.11.Button Flat StyleButton Flat Style
23.8.12.Calculate Button size based on its TextCalculate Button size based on its Text
23.8.13.Button Background ColorButton Background Color
23.8.14.Button Text AlignmentButton Text Alignment
23.8.15.Button ImageButton Image
23.8.16.Auto Scale Button
23.8.17.extends Button
23.8.18.Device Independent Button