ContextMenu WPF in VB.NET

Here we see how to create and apply contextmenu item to a input text of the textbox in xaml.
  • 6448
 

Here we see how to create and apply contextmenu item to a input text of the textbox in xaml. To do that we create a TextBox control and input some text into the TextBox after that right click on the text and apply contextmenu items to the Text.

ContextMenu

The ContextMenu element enables you to present users with a list of items that specify commands or options that are associated with a particular control. When Users right-click the control to make the menu appear. The event is a right-click on an existing control, element, or item already displayed in the page. A ContextMenu can contain one or more context menu items. Each item is associated with an action to execute client or server-side code. In addition, each context menu item may be displayed with an icon or text.

For example

Now creating a TextBox in XAML.

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="348" Width="525">

    <TextBox Name="textBox1"

         TextWrapping="Wrap"

         Margin="10, 10, 5, 5" Grid.Row="7" Height="289">

        Rohatash Kumar

        <TextBox.ContextMenu>

            <ContextMenu>

                <MenuItem Header="_Bold"

                IsCheckable="True"

                Checked="Bold_Checked"

                Unchecked="Bold_Unchecked" />

                <MenuItem Header="_Italic"

                IsCheckable="True"

                Checked="Italic_Checked"

                Unchecked="Italic_Unchecked" />

                <Separator />

                <MenuItem Header="I_ncrease Font Size"

                Click="IncreaseFont_Click" />

                <MenuItem Header="_Decrease Font Size"

                Click="DecreaseFont_Click" />

            </ContextMenu>

        </TextBox.ContextMenu>

    </TextBox>

</Window>

Now double click on the form and add the following code.

Class MainWindow

    Private Sub Bold_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        textBox1.FontWeight = FontWeights.Bold

    End Sub

    Private Sub Bold_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        textBox1.FontWeight = FontWeights.Normal

    End Sub

    Private Sub Italic_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        textBox1.FontStyle = FontStyles.Italic

    End Sub

    Private Sub Italic_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)

        textBox1.FontStyle = FontStyles.Normal

    End Sub

    Private Sub IncreaseFont_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

        If textBox1.FontSize < 18 Then

            textBox1.FontSize += 2

        End If

    End Sub

    Private Sub DecreaseFont_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)

        If textBox1.FontSize > 10 Then

            textBox1.FontSize -= 2

        End If

    End Sub

End Class

Now run the application and test it.

img1.gif

Figure1.gif

Now right click on the Text. The ContextMenu will be display.

img2.gif

Figure2.gif

Now click on the bold. The Text will be display Bold.

img3.gif

Figure3.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.