WPF Button control in VB.NET

In this article we will learn how to use Button control in WPF using VB.NET.
  • 2592

In this article we will learn how to use Button control in WPF using VB.NET.

Button control

The Button control is used to display a push button.

Properties: These are the following properties of button control.

b1.gif
 

Figure 1.

Width - Width property represents the width of a Button.

Height - Height property represents the height of a Button.

Content - The text shown on the button.

Command - Gets or sets the command to invoke when this button is pressed.

CommandParameter - CommandParameter is used to pass parameter to the Command property.

ClickMode - Gets or sets when the Click event occurs.

Button event:

Private Sub Button1_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles Button1.Click

End Sub

Creating a Button in XAML
 

The Button element represents a WPF Button control in XAML.

 
<Button></Button>
 

The Width and Height attributes of the Button element represent the width and the height of a Button. The Content property of the Button element sets the text of a button. Name attribute represents the name of the control, which is a unique identifier of a control.

 

<Button Content="Button" Height="23" Name="Button1" Width="205" />

 

The form looks like this.


 

b2.gif 

Figure 2.

For example:

creating two label Number and Result. Now create two text boxes, one to input a number and other to output a number and a buttons.

The form looks like this.

b3.gif
 

Figure 3.

XAML code

 

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <Grid>

        <Label Content="Number" Height="28" HorizontalAlignment="Left" Margin="10,10,0,0"Name="Label1" VerticalAlignment="Top" />

        <Label Content="Result" Height="28" HorizontalAlignment="Left" Margin="10,48,0,0"Name="Label2" VerticalAlignment="Top" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="74,10,0,0" Name="txtNumber"VerticalAlignment="Top" Width="120" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="74,48,0,0" Name="txtResult"VerticalAlignment="Top" Width="120" />

        <Button Content="Square" Height="23" HorizontalAlignment="Left" Margin="74,96,0,0"Name="cmdSquare" VerticalAlignment="Top"
Width="141" Background="Blue"Foreground="Crimson" BorderBrush="DarkRed" />

    </Grid>

</Window>

 

Now double click on the simple button and add the following code.

Private Sub Button1_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles cmdSquare.Click

        Dim num As Double = Double.Parse(txtNumber.Text)

        Dim sq As Double = num * num

        txtResult.Text = sq.ToString()

    End Sub

Now run the application.

b4.gif
 

Figure 4.

Now enter the number and press Square (Button).

b5.gif
 

Figure 5. 

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.