编程很新,我需要帮助来解决问题。启动我的程序后,它会说程序已停止响应并立即关闭。下面是代码本身:执行代码会使程序停止响应
XAML
<Window x:Class="WpfApplication6.Window2"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window2" Height="300" Width="587.615">
<Grid>
<Label Content="Programme de devinette" HorizontalAlignment="Left" Margin="144,10,0,0" VerticalAlignment="Top" Width="355" Height="56" FontSize="22" FontFamily="Segoe WP Black"/>
<TextBox HorizontalAlignment="Left" Name="BoiteChiffre" Height="23" Margin="228,103,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120" TextChanged="TextBox_TextChanged"/>
<Label Content="Entrez un chiffre" HorizontalAlignment="Left" Margin="216,66,0,0" VerticalAlignment="Top" Width="164" FontSize="18"/>
<Button Content="Nombre random" HorizontalAlignment="Left" Margin="77,160,0,0" VerticalAlignment="Top" Width="109" Click="Button_Click"/>
<Button Content="Verification" HorizontalAlignment="Left" Margin="424,160,0,0" VerticalAlignment="Top" Width="75" RenderTransformOrigin="-0.055,0.428" Click="Button_Click_2"/>
<Button Content="Quitter" HorizontalAlignment="Left" Margin="253,201,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
</Grid>
,这是与该窗口相关的代码:
namespace WpfApplication6
{
/// <summary>
/// Logique d'interaction pour Window2.xaml
/// </summary>
public partial class Window2 : Window
{
public Window2()
{
InitializeComponent();
}
int random1;
private void Button_Click(object sender, RoutedEventArgs e) //random
{
Random chiffrealeatoire = new Random();
random1 = (chiffrealeatoire.Next(0, 20));
}
private void Button_Click_1(object sender, RoutedEventArgs e) //quit
{
Application.Current.Shutdown();
}
private void Button_Click_2(object sender, RoutedEventArgs e) //veri
{
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
if (Convert.ToInt32(BoiteChiffre.Text) < random1)
{
MessageBox.Show("Too low");
}
if (Convert.ToInt32(BoiteChiffre.Text) > random1)
{
MessageBox.Show("Too high");
}
else
{
MessageBox.Show("Congratulations");
}
}
}
的代码应该有3个按钮:一个是退出程序,一个在点击时指定0-20之间隐藏的随机数字(用户必须猜测这个数字),一个用来检查用户输入的数字是否与随机生成的数字匹配编号。如果号码不匹配,程序会告诉用户他的号码是否太高而太低。正如我前面说过的,我的程序在启动后崩溃了,我找不到问题所在。所有帮助appriciated,谢谢。
PS:窗口看起来是这样的
乍一看,它看起来像一个未处理的异常。你应该尝试在你的'TextBox_TextChanged'事件处理程序周围添加一个[try-catch](https://msdn.microsoft.com/en-us/library/0yd65esw.aspx)块 –
@MatiasCicero我建议你发布后一个答案,因为这是问题。TextChanged事件将在加载表单并将“TextBox”(默认文本)写入TextBox时引发(并导致异常)。 –
Avast与这有什么关系?我在没有Avast的另一台计算机上运行这个程序,得到了相同的结果。 –