user15769474
user15769474

Reputation:

Increment textbox value on button click WPF C#

What I'm trying to do is I have a textbox, and a '-' and a '+' button, and on button click I need to decrease/increase it's value. I have close to 0 experience with WPF, we only learnt console apps in school and now we got this task and I have no idea why this code sample is not working:

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>

Any help is appreciated!

Upvotes: 0

Views: 1277

Answers (2)

Mukiev Mukhammad
Mukiev Mukhammad

Reputation: 1

You can use prefix increment

textBox1.Text = $"{++ertek}";

Upvotes: 0

ASh
ASh

Reputation: 35646

ertek++ is a postfix increment operator. The result of ertek++ is the value of ertek before the operation.

use simple + operator:

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

Upvotes: 2

Related Questions