Button Control in C#

In this article, we will learn how to use button in windows form in C#.

Let's create windows form.

Step 1
In Visual Studio select "File" = > "New" = > "Project..." then select C# Windows Forms Application then click Ok.

create windows form

Step 2
Let's create a button then you go "View" = > select "ToolBox" (Crtl+Alt+X) = > then select "All Windows Forms" = > Choose "Button".

Button
  1. First of all, we'll know about the "button".

    The button raises events when the user clicks it. We'll discuss properties and function and events of button write down.

    "BackColor" changes the background color of button control.

    button1.BackColor = Color.AliceBlue;
    

    "BackgroundImage" sets background image of button control.

    button1.BackgroundImage = new Bitmap(@"C:\Users\subra\Desktop\ChandanWebsite\blog\Image\dialog\Screenshot (51).png");
    

    "BackgroundImageLayout" changes the layout of background image in button control.

    button1.BackgroundImageLayout = ImageLayout.Center;
    

    "Click" when user mouse click on button control.

    button1.Click+=new EventHandler(button1_Click);
    private void button1_Click(object sender, EventArgs e)
    {
    }
    

    "Cursor" when user mouse hover on button control then cursor style changed.

    button1.Cursor = Cursors.Hand;
    

    "Dispose()" released all resources used by the System.ComponentModel.Component.

    button1.Dispose();
    

    "DoubleClick" when user mouse double click on button control.

    button1.DoubleClick+=new EventHandler(button1_DoubleClick);
    private void button1_DoubleClick(object sender, EventArgs e)
    {
    }
    

    "Enabled" changes the button control are enable or not.

    button1.Enabled = true;
    

    "FlatStyle" gets or sets flat style appearance of the button control.

    button1.FlatStyle = FlatStyle.Flat;
    

    "FlatAppearance" gets the appearance of the borders and color used to indicate check state and mouse state.

    button1.FlatAppearance.BorderColor = Color.AliceBlue;
    

    "Focus()" focusing the button control.

    button1.Focus();
    

    "Font" changes the style of the text of button control.

    button1.Font = new System.Drawing.Font("arial", 14.0F, FontStyle.Bold);
    

    "ForeColor" changes the font color of button control.

    button1.ForeColor = Color.AliceBlue;
    

    "Height" set the height of button control.

    button1.Height=400;
    

    "Hide()" hides the button control.

    button1.Hide();
    

    "KeyDown" events are working when user press any key from keyboard.

    button1.KeyDown+=new KeyEventHandler(button1_KeyDown);
    private void button1_KeyDown(object sender, KeyEventArgs e)
    {
      if (e.KeyCode == Keys.Enter)
      {
      }
    }
    

    "KeyPress" events are working when user press any key from keyboard while button control has focused.

    button1.KeyPress+=new KeyPressEventHandler(button1_KeyPress);
    private void button1_KeyPress(object sender, KeyPressEventArgs e)
    {
      if (e.KeyChar == 'A')
      {
                
      }
    }
    

    "MouseClick" when user click the button by mouse.

    button1.MouseClick+=new MouseEventHandler(button1_MouseClick);
    private void button1_MouseClick(object sender, MouseEventArgs e)
    {
    }
    

    "MouseDoubleClick" when user double click the by mouse.

    button1.MouseDoubleClick += new MouseEventHandler(button1_MouseClick);
    private void button1_MouseClick(object sender, MouseEventArgs e)
    {
    }
    

    "MouseEnter" when mouse pointer enters the button control.

    button1.MouseEnter+=new EventHandler(button1_MouseEnter);
    private void button1_MouseEnter(object sender, EventArgs e)
    {
    }
    

    "MouseHover" when mouse pointer rests/hover on the button control.

    button1.MouseHover+=new EventHandler(button1_MouseHover);
    private void button1_MouseHover(object sender, EventArgs e)
    {
    }
    

    "MouseLeave" when mouse pointer leaves the button control.

    button1.MouseLeave+=new EventHandler(button1_MouseLeave);
    private void button1_MouseLeave(object sender, EventArgs e)
    {
    }
    

    "MouseMove" when mouse pointer moves the button control.

    button1.MouseMove+=new MouseEventHandler(button1_MouseMove);
    private void button1_MouseMove(object sender, MouseEventArgs e)
    {
    }
    

    "Padding" sets the padding on button control.

    button1.Padding = new Padding(10, 10, 10, 10);
    

    "ResetText()" resets all properties of button to its default values.

    button1.ResetText();
    

    "Show()" displays the button control.

    button1.Show();
    

    "Text" gets or sets text on button control.

    button1.Text = "Submit";
    

    "TextAlign" gets or sets text align on button control.

    button1.TextAlign = ContentAlignment.MiddleRight;
    

    "Visible" gets or sets the button control visible or not.

    button1.Visible = true;
    


Write a comment


Name Email Message