Visual Studio 2019 WPF (VB.NET Source Code & XAML) How to Create Your First WPF Program (Super Hello World)


Visual Studio 2019 WPF Application (VB.NET Source Code & XAML) How to Create Your First WPF Program (Super Hello, World!).

🎓 Master Visual Basic .NET and Access Database By Building the Point Of Sale System (POS).
ðŸ“ē Enroll Now: https://bit.ly/2WcbRhX

[Screenshot : 1]


[YouTube Tutorial Part 1/2]


[Screenshot : 2]


[YouTube Tutorial Part 2/2]


[Source Code]

MainWindow.xaml

<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="Super Hello World! Ex.1 : Your First WPF Program"
        Background="CadetBlue"
        WindowStartupLocation="CenterScreen"
        WindowState="Normal"
        WindowStyle="ThreeDBorderWindow"
        Height="450" Width="800"
        Icon="iCons\Icojam-Blue-Bits-Globe.ico"
        Loaded="Window_Loaded">

    <!-- Comments in XAML -->
    <!-- Code A Minute : iBasskung Tutorial -->
   
    <!-- Use a Grid for automatic layout -->
    <Grid>
        <!-- StackPanel -->
        <!-- Arranges child elements into a single line -->
        <!-- that can be oriented horizontally or vertically. -->
   
        <!-- a StackPanel inside the Gride -->
        <!-- used to hold other controls -->
        <StackPanel>

            <!-- Set margin of controls in WPF -->
            <!-- Margin property takes 4 numbers -->
            <!-- Left, Top, Right and Buttom -->
       
            <!-- TextBox -->
            <TextBox Name="inputTextBox"
                     HorizontalAlignment="Stretch"
                     HorizontalContentAlignment="Center"
                     TextWrapping="Wrap"
                     FontSize="48"
                     Foreground="Crimson"
                     Background="AliceBlue"
                     Margin="6,6,6,0"
                     TextChanged="InputTextBox_TextChanged"/>

            <!-- Left-align TextBlock -->
            <TextBlock Name="outputTextBlockLeft"
                       Text="{Binding ElementName=inputTextBox,
                       Path=Text,
                       UpdateSourceTrigger=PropertyChanged}"
                       HorizontalAlignment="Left"
                       FontSize="48"
                       Foreground="Gold"
                       Background="Snow"
                       Margin="7,6,6,0"/>

            <!-- Center-align TextBlock -->
            <TextBlock Name="outputTextBlockCenter"
                       Text="{Binding ElementName=inputTextBox,
                       Path=Text,
                       UpdateSourceTrigger=PropertyChanged}"
                       HorizontalAlignment="Center"
                       FontSize="48"
                       Foreground="BlueViolet"
                       Background="Beige"
                       Margin="7,6,6,0"/>

            <!--  Very nice -->
            <!-- Right-align TextBlock -->
            <TextBlock Name="outputTextBlockRight"
                       Text="{Binding ElementName=inputTextBox,
                       Path=Text,
                       UpdateSourceTrigger=PropertyChanged}"
                       HorizontalAlignment="Right"
                       FontSize="48"
                       Foreground="Gold"
                       Background="Snow"
                       Margin="7,6,6,0"/>

            <!-- Add button to StackPanel -->
            <!-- used to display message to user. -->
            <Button Name="displayButton"
                    Content="Display Data"
                    FontSize="48"
                    Height="116"
                    Foreground="White"
                    Background="Crimson"
                    VerticalAlignment="Stretch"
                    Margin="6,7,5,0"
                    Padding="10"
                    Click="DisplayButton_Click"/>

        </StackPanel>

    </Grid>

</Window>

MainWindow.xaml.vb

Class MainWindow

    Private _ButtonContent As String = "Display Data"
    Dim _CaptionTitle As String = "Super Hello World! : Code A Minute : iBasskung Tutorial"

    Private Sub Window_Loaded(sender As Object, e As RoutedEventArgs)
        ' TODO :)
        inputTextBox.Focus()
    End Sub

    Private Sub InputTextBox_TextChanged(sender As Object, e As TextChangedEventArgs)
        ' TODO :)

        Try

            displayButton.Content = String.Empty

            If TypeOf sender Is TextBox Then

                Dim tb As TextBox = DirectCast(sender, TextBox)

                If Not String.IsNullOrEmpty(tb.Text) Then

                    outputTextBlockLeft.Text = tb.Text & " [" & tb.Text.Length.ToString() & "]"
                    outputTextBlockCenter.Text = tb.Text & " [" & tb.Text.Length.ToString() & "]"
                    outputTextBlockRight.Text = tb.Text & " [" & tb.Text.Length.ToString() & "]"

                    displayButton.Content = _ButtonContent & " [" & tb.Text.Length.ToString() & "]"

                Else

                    outputTextBlockLeft.Text = String.Empty
                    outputTextBlockCenter.Text = String.Empty
                    outputTextBlockRight.Text = String.Empty

                    displayButton.Content = _ButtonContent

                End If

            End If

        Catch ex As Exception

        End Try

    End Sub

    Private Sub DisplayButton_Click(sender As Object, e As RoutedEventArgs)

        ' TODO :)
        Try

            Dim buttonOk = MessageBoxButton.OK
            Dim imageInfo = MessageBoxImage.Information
            Dim imageWarn = MessageBoxImage.Warning

            If Not String.IsNullOrEmpty(inputTextBox.Text) Then
                Dim value As String = "The Value You Entered: " & Environment.NewLine
                Dim data As String = inputTextBox.Text.Trim()
                MessageBox.Show(String.Format("{0} {1}", value, data), _CaptionTitle, buttonOk, imageInfo)
            Else
                Dim warning As String = "Please fill in the required field."
                MessageBox.Show(warning, _CaptionTitle, buttonOk, imageWarn)
                inputTextBox.Focus()
            End If

        Catch ex As Exception
            MessageBox.Show("Oops! Something went wrong: " & ex.Message, MessageBoxButton.OK, MessageBoxImage.Error)
        End Try

    End Sub

End Class


Comments