<Window x:Class="Randomizer.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"
xmlns:local="clr-namespace:Randomizer"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="390">
<Grid ShowGridLines="False" Background="DarkGray">
<Grid.RowDefinitions>
<RowDefinition Height="0.5*"></RowDefinition>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="0.3*"></RowDefinition>
<RowDefinition Height="0.7*"></RowDefinition>
<RowDefinition Height="0.7*"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"></ColumnDefinition>
<ColumnDefinition Width="Auto"></ColumnDefinition>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" VerticalAlignment="Bottom" HorizontalAlignment="Center">Random number</TextBlock>
<TextBlock x:Name="result" FontSize="16" FontWeight="Bold" TextAlignment="Center" Text="ResultHere" Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" Margin="15 5 15 15" Width="260" IsEnabled="False"/>
<TextBlock x:Name="minValue" Grid.Row="2" Grid.Column="0" HorizontalAlignment="Center" VerticalAlignment="Bottom">Min Value </TextBlock>
<TextBlock x:Name="maxValue" Grid.Row="2" Grid.Column="1" HorizontalAlignment="Center">Max Value</TextBlock>
<TextBox Grid.Row="3" MaxLength="4" Grid.Column="0" Margin="10 0 10 10" Width="75"/>
<TextBox Grid.Row="3" MaxLength="4" Grid.Column="1" Margin="10 0 10 10" Width="75"/>
<Button x:Name="randomizeButton" Grid.Row="4" Grid.Column="0" Grid.ColumnSpan="2" Margin="10 0 10 10" Width="355" Content="Randomize"/>
</Grid>
</Window>
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
namespace Randomizer
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow()
{
InitializeComponent();
randomizeButton.Click += RandomizeButton_Click;
}
private void RandomizeButton_Click(object sender, RoutedEventArgs e)
{
result.Text = CalculateRandomNumber(minValue, maxValue);
}
private string CalculateRandomNumber(TextBlock min, TextBlock max)
{
Random random = new Random();
int randomNumber;
if (min is null && max is null)
randomNumber = random.Next();
else if(min is null)
randomNumber = random.Next(Convert.ToInt32(max.Text));
else if (max is null)
randomNumber = random.Next(Convert.ToInt32(min.Text), Int32.MaxValue);
else
randomNumber = random.Next(Convert.ToInt32(min.Text), Convert.ToInt32(max.Text));
return randomNumber.ToString();
}
}
}
try
{
/// ваш код
}
catch(Exception err)
{
MessageBox.Show(err.Message);
}