N
N
Nonpacie2021-02-25 17:38:22
WPF
Nonpacie, 2021-02-25 17:38:22

WPF application crashes on button click, what's wrong?

I'm making a randomizer with an interface that describes itself.
6037b5a08d56d568589453.png

When you click the Randomize button, the program simply closes. I have no idea what's wrong :( I just started learning WPF a couple of days ago, so don't rush :)

MainWindow.xaml:

<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>

MainWindow.xaml.cs
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();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sumor, 2021-02-25
@Sumor

CalculateRandomNumber - it's ugly to work with text blocks, it's better to pass a string or a number.
private string CalculateRandomNumber(string min, string max)
and call CalculateRandomNumber(minValue.Text, maxValue.Text)
minValue and maxValue cannot be null - they are existing elements on the form.
You need to check for string.IsNullOrWhiteSpace(minValue.Text) instead of checking for null.
Instead of Convert.ToInt32, it is better to use the Int32.TryParse construct. Immediately get processing for incorrect input.
All processing can be framed in a try-catch block and immediately get an error:

try
{
/// ваш код

}
catch(Exception err)
{
  MessageBox.Show(err.Message);
}

A
Alexander Ananiev, 2021-02-25
@SaNNy32

Put a breakpoint on the method that handles the button click and see what line the application crashes on.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question