单击"WPF C#"按钮,增加文本框值



我要做的是我有一个文本框,一个"-"和一个"+"按钮,点击按钮我需要减少/增加它的值。我对WPF有接近0的经验,我们只在学校学习了控制台应用程序,现在我们完成了这项任务,我不知道为什么这个代码示例不起作用:

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace sudokuGUI
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int ertek = Convert.ToInt32(textBox1.Text);
if (ertek > 4)
{
textBox1.Text = $"{ertek--}";
}
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
int ertek = Convert.ToInt32(textBox1.Text);
if (ertek < 9)
{
textBox1.Text = $"{ertek++}";
}
}
}
}
<Window x:Class="sudokuGUI.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:sudokuGUI"
mc:Ignorable="d"
Title="Sudoku-ellenőrző" Height="210" Width="540">
<Grid>
<TextBox x:Name="textBox2" HorizontalAlignment="Left" Height="23" TextWrapping="Wrap" VerticalAlignment="Top" Width="512" Margin="10,90,0,0"/>
<Label Content="Új feladvány mérete:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
<Button Content="-" HorizontalAlignment="Left" VerticalAlignment="Top" Width="20" Margin="144,13,0,0" Click="Button_Click"/>
<Button Content="+" HorizontalAlignment="Left" VerticalAlignment="Top" Width="20" Margin="194,13,0,0" Click="Button_Click_1"/>
<Label Content="Kezdőállapot:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,59,0,0"/>
<Label x:Name="label1" Content="Hossz: 0" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,118,0,0"/>
<Button Content="Ellenőrzés" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="447,118,0,0"/>
<TextBox x:Name="textBox1" HorizontalAlignment="Left" Height="20" TextWrapping="Wrap" Text="4" VerticalAlignment="Top" Width="20" Margin="169,13,0,0" TextAlignment="Center" IsEnabled="False"/>
</Grid>
</Window>

感谢您的帮助!

ertek++是一个后缀增量运算符。ertek++的结果是操作之前ertek的值。

使用简单的+运算符:

private void Button_Click_1(object sender, RoutedEventArgs e)
{
int ertek = Convert.ToInt32(textBox1.Text);
if (ertek < 9)
{
textBox1.Text = $"{ertek+1}";
}
}

相关内容

  • 没有找到相关文章

最新更新