A metroButton is a control allowing users to initiate actions by click events.
Buttons are found in almost all GUI frameworks since they are a core component of User Interface controls.
Users always need to provide an input in the form of action and buttons are the easiest way of doing that.
Why Buttons?
No. | Reason |
---|---|
1. | Buttons allow users to interact with an application via initiation of an action. |
2. | Buttons provide users with a sense of control over the application. They have to explicitly click a button say for example that they want to delete some data.This makes them trust the application. |
3. | Buttons are straight forward to use adn create. |
Characteristics of MetroButtons
MetroButtons belong to MetroFramework.Controls
namespace:
namespace MetroFramework.Controls
A metroButton is a class:
public class MetroButton..{}
deriving from System.Windows.Forms.Button
class:
public class MetroButton : Button..{}
and implementing IMetroControl interface:
public class MetroButton : Button, IMetroControl{..}
Creating MetroButton
The MetroButton
class provides us a default public empty contructor:
public MetroButton();
This can be used as follows:
MetroButton myButton=new MetroButton();
Setting MetroButton Text
We can then set the metroButon text as follows:
myButton.Text = "Start";
or via Object initializer syntax:
MetroButton myButton = new MetroButton {Text = "Start"};
Adding MetroButton to MetroForm
On it's own a metroButton won't be very important. For it to be useful we have to render it to users and we do that by first adding it to a parent MetroForm.
using System;
using System.Windows.Forms;
using MetroFramework.Controls;
using MetroFramework.Forms;
namespace MyMetroForm
{
static class Program
{
public static void Main()
{
MetroForm myForm = new MetroForm
{
Text = "MetroButton Example",
ClientSize = new System.Drawing.Size(564, 420),
};
MetroButton myButton = new MetroButton { Text = "Start", Location = new System.Drawing.Point(232, 210) };
myForm.Controls.Add(myButton);
Application.Run(myForm);
}
}
}
Take note we specify the button location as well using the System.Drawing.Point
class.
Result:
Handling Button click events
Buttons aren't very useful if we don't provide their event handlers. This is because buttons are primarily used for initiation of actions.
To listen to our metroButton events' do the following
- First type
myButton.Click
in your visual studio. - Then type
+
- Then
=
. - Then press your tab key twice. This will generate us an event hander method and assign it to our
myButton.Click
property.
Here's a complete example:
using System;
using System.Windows.Forms;
using MetroFramework;
using MetroFramework.Controls;
using MetroFramework.Forms;
namespace MyMetroForm
{
static class Program
{
public static void Main()
{
MetroForm myForm = new MetroForm
{
Text = "MetroButton Example",
ClientSize = new System.Drawing.Size(564, 420),
};
MetroButton myButton = new MetroButton { Text = "Start", Location = new System.Drawing.Point(232, 210) };
myButton.Click += myButton_Click;
myForm.Controls.Add(myButton);
Application.Run(myForm);
}
static void myButton_Click(object sender, EventArgs e)
{
MessageBox.Show("The coldedst place in the Universe is the Bumerang' Nebula");
}
}
}
Result: