0% found this document useful (0 votes)
4 views10 pages

Button Properties

The document provides an overview of button properties, events, and methods in C#, including examples of how to set properties like text, color, and size, as well as handle events such as Click and MouseEnter. It also demonstrates how to use tooltips with buttons and manage event handlers, including attaching and removing them programmatically. Additionally, it includes code snippets for practical implementation within a Windows Forms application.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views10 pages

Button Properties

The document provides an overview of button properties, events, and methods in C#, including examples of how to set properties like text, color, and size, as well as handle events such as Click and MouseEnter. It also demonstrates how to use tooltips with buttons and manage event handlers, including attaching and removing them programmatically. Additionally, it includes code snippets for practical implementation within a Windows Forms application.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Handout Two: Button Properties, Events, and Methods

Here are some examples of button properties, events, and methods in C#:

Properties:

Text: Gets or sets the text displayed on the button.

Button button = new Button();

button.Text = "Click Me";

Enabled: Gets or sets a value indicating whether the button is enabled.

Button button = new Button();

button.Enabled = false;

BackColor: Gets or sets the background color of the button.

Button button = new Button();

button.BackColor = Color.Red;

ForeColor: Gets or sets the foreground color (text color) of the button.

Button button = new Button();

button.ForeColor = Color.White;

Size: Gets or sets the size of the button.

Button button = new Button();

button.Size = new Size(100, 30);

Events:

Click: Occurs when the button is clicked.

Button button = new Button();

button.Click += Button_Click;

private void Button_Click(object sender, EventArgs e)

// Button click event handler

MouseEnter: Occurs when the mouse pointer enters the button.

Dr. Ahmed Alnasheri 1/10


Button button = new Button();

button.MouseEnter += Button_MouseEnter;

private void Button_MouseEnter(object sender, EventArgs e)

// Mouse enter event handler

MouseLeave: Occurs when the mouse pointer leaves the button.

Button button = new Button();

button.MouseLeave += Button_MouseLeave;

private void Button_MouseLeave(object sender, EventArgs e)

// Mouse leave event handler

DoubleClick: Occurs when the button is double-clicked.

Button button = new Button();

button.DoubleClick += Button_DoubleClick;

private void Button_DoubleClick(object sender, EventArgs e)

// Double click event handler

Methods:

PerformClick(): Programmatically performs a click on the button.

Button button = new Button();

button.PerformClick();

Focus(): Gives the button the input focus.

Button button = new Button();

button.Focus();

Dr. Ahmed Alnasheri 2/10


Capture: Captures mouse input to the button.

Button button = new Button();

button.Capture = true;

ToString(): Returns a string representation of the button.

Button button = new Button();

string buttonString = button.ToString();

Here's an example of how to use tooltips with buttons in C# using the ToolTip control in Windows
Forms:

using System;

using System.Windows.Forms;

public class MainForm : Form

private Button button1;

private Button button2;

private ToolTip toolTip;

public MainForm()

// Create the ToolTip control

toolTip = new ToolTip();

// Create the buttons

button1 = new Button();

button1.Text = "Button 1";

button1.Location = new Point(50, 50);

button2 = new Button();

button2.Text = "Button 2";

button2.Location = new Point(150, 50);

Dr. Ahmed Alnasheri 3/10


// Set tooltip text for each button

toolTip.SetToolTip(button1, "This is Button 1");

toolTip.SetToolTip(button2, "This is Button 2");

// Add the buttons to the form

Controls.Add(button1);

Controls.Add(button2);

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.Run(new MainForm());

In this example, we first create an instance of the ToolTip control, which will be used to display the
tooltips. We then create two buttons (button1 and button2) and set their Text properties to specify the
text displayed on the buttons.

Next, we use the SetToolTip method of the ToolTip control to associate tooltip text with each button. In
this case, we set the tooltip for button1 to "This is Button 1" and the tooltip for button2 to "This is
Button 2".

Finally, we add the buttons to the form using the Controls.Add method.

When you run the application and hover the mouse pointer over each button, the associated tooltip text
will be displayed.

Note: Make sure to include the using System.Windows.Forms; namespace at the beginning of your code
file to access the necessary classes and controls for working with Windows Forms.

Dr. Ahmed Alnasheri 4/10


Here's an example of how you can handle the Button.Enter event programmatically:

using System;

using System.Windows.Forms;

public class MainForm : Form

private Button myButton;

public MainForm()

// Create the Button control

myButton = new Button();

myButton.Text = "My Button";

myButton.Location = new System.Drawing.Point(50, 50);

// Attach the Enter event handler

myButton.Enter += MyButton_Enter;

// Add the Button control to the form

Controls.Add(myButton);

private void MyButton_Enter(object sender, EventArgs e)

// Handle the Enter event

MessageBox.Show("Button has received focus!");

Dr. Ahmed Alnasheri 5/10


[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.Run(new MainForm());

In this example, we create a Button control named myButton and attach an event handler to the Enter
event using the += operator. The event handler method MyButton_Enter is then defined to handle the
Enter event. In this case, when the button receives focus, a message box is displayed with the message
"Button has received focus!"

You can customize the event handler method MyButton_Enter to perform any specific actions or logic
you require when the button receives focus. The example above demonstrates showing a message box,
but you can modify it to suit your application's needs.

By handling the Button.Enter event, you can respond to focus changes programmatically and implement
custom behavior based on the button gaining focus.

In addition, it is possible to remove an event handler after it has been attached. To remove an event
handler, you use the -= operator to detach it from the event.

Here's an example that demonstrates how to attach and remove an event handler for the Button.Click
event:

using System;

using System.Windows.Forms;

public class MainForm : Form

private Button myButton;

public MainForm()

// Create the Button control

Dr. Ahmed Alnasheri 6/10


myButton = new Button();

myButton.Text = "Click Me";

myButton.Location = new System.Drawing.Point(50, 50);

// Attach the Click event handler

myButton.Click += MyButton_Click;

// Add the Button control to the form

Controls.Add(myButton);

// Attach a timer to remove the event handler after 3 seconds

Timer timer = new Timer();

timer.Interval = 3000;

timer.Tick += Timer_Tick;

timer.Start();

private void MyButton_Click(object sender, EventArgs e)

// Handle the Click event

MessageBox.Show("Button clicked!");

private void Timer_Tick(object sender, EventArgs e)

// Remove the event handler after 3 seconds

myButton.Click -= MyButton_Click;

Dr. Ahmed Alnasheri 7/10


[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.Run(new MainForm());

In this example, we attach the MyButton_Click event handler to the Click event using the += operator as
before. However, we also add a Timer control that ticks after 3 seconds. In the Timer_Tick event
handler, we use the -= operator to remove the MyButton_Click event handler from the Click event of
the button.

By removing the event handler, the button will no longer trigger the associated logic when clicked. This
can be useful in scenarios where you want to dynamically control when an event handler is active or
when you want to remove it after a certain condition or time period.

Here's an example that demonstrates how to remove an event handler using the -= operator:

using System;

using System.Windows.Forms;

public class MainForm : Form

private Button myButton;

private EventHandler myButtonClickHandler;

public MainForm()

// Create the Button control

myButton = new Button();

myButton.Text = "Click Me";

myButton.Location = new System.Drawing.Point(50, 50);

Dr. Ahmed Alnasheri 8/10


// Define the event handler

myButtonClickHandler = MyButton_Click;

// Attach the event handler

myButton.Click += myButtonClickHandler;

// Add the Button control to the form

Controls.Add(myButton);

// Simulate removing the event handler after a delay

Timer timer = new Timer();

timer.Interval = 3000;

timer.Tick += Timer_Tick;

timer.Start();

private void MyButton_Click(object sender, EventArgs e)

// Handle the Click event

MessageBox.Show("Button clicked!");

private void Timer_Tick(object sender, EventArgs e)

// Remove the event handler

myButton.Click -= myButtonClickHandler;

// Show a message indicating the event handler was removed

Dr. Ahmed Alnasheri 9/10


MessageBox.Show("Event handler removed!");

// Stop the timer

((Timer)sender).Stop();

[STAThread]

static void Main()

Application.EnableVisualStyles();

Application.Run(new MainForm());

In this example, we define an event handler method MyButton_Click and store it in the
myButtonClickHandler field. We attach the event handler to the Click event of myButton using the +=
operator.

Later, we use a Timer control to simulate removing the event handler after a delay of 3 seconds. In the
Timer_Tick event handler, we remove the event handler by using the -= operator with the
myButtonClickHandler reference.

When the event handler is removed, a message box is displayed with the message "Event handler
removed!".

By using the -= operator and providing the same event handler instance that was previously attached,
you can successfully remove the event handler from the event.

Dr. Ahmed Alnasheri 10/10

You might also like