Drag 1 button:
Button name: random number
Drag a 1 TextBlock control:

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>

<Button Content="Random Number" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="Button1" VerticalAlignment="Top" Width="178" />

<TextBlock Height="23" HorizontalAlignment="Left" Margin="12,66,0,0" Name="TextBlock1" Text="" VerticalAlignment="Top" Width="404" Foreground="Crimson" />

</Grid>

</Window>

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

VB.NET code

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Button1.Click

Dim generator As New Random()

Dim randomvalue As Integer

randomvalue = generator.Next(1, 10)

TextBlock1.Text += " " + randomvalue.ToString()

End Sub


Conclusion

Hope this blog will help you to generate random number in WPF using VB.NET.