Hi just add simple code to your buttons, Concern about validations and things.
private void plusButton_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(textBox2.Text))
{
int count = 0;
textBox2.Text = count.ToString();
}
else
{
int count = Convert.ToInt16(textBox2.Text);
++count;
textBox2.Text = count.ToString();
}
}
private void minusButton_Click(object sender, EventArgs e)
{
if (textBox2.Text == "0" || string.IsNullOrEmpty(textBox2.Text))
{
//do nothing
}
else
{
int count = Convert.ToInt16(textBox2.Text);
--count;
textBox2.Text = count.ToString();
}
}
The simplest version I can imagine would be this one:
.xaml
<StackPanel>
<TextBox x:Name="SomeTextBox" Text="Old Text"/>
<Button Content="Sender" Click="Button_Click" />
</StackPanel>
.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
SomeTextBox.Text = "NewText";
}
Seeing that you are building a shop you may want to dive a bit deeper into WPF.
Consider making you Product a Model in the MVVM sense of the word and create a View to display it and connect them using a viewmodel.
Make a CartItem as a Model,… make a cart as a model,…
Here is a more complex example that points you into that direction.
.xaml
<StackPanel>
<Button Content="+" Command="{Binding ProductInc}" CommandParameter="{Binding Cola}" />
<TextBlock Text="{Binding Cola.Count, Mode=OneWay}"/>
<Button Content="-" Command="{Binding ProductDec}" CommandParameter="{Binding Cola}" />
</StackPanel>
.xaml.cs
public class Product : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private int count = 0;
public int Count
{
get { return count; }
set
{
if (value == count) return;
count = value;
if (null == PropertyChanged) return;
PropertyChanged(this, new PropertyChangedEventArgs("Count"));
}
}
}
class SimpleCommand<T> : ICommand
{
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) { return true; }
private Action<T> execute;
public SimpleCommand(Action<T> execute) { this.execute = execute; }
public void Execute(object parameter) { execute((T)parameter); }
}
public partial class MainWindow : Window
{
public Product Cola { get; set; }
public ICommand ProductInc { get { return new SimpleCommand<Product>(OnProductInc); } }
public ICommand ProductDec { get { return new SimpleCommand<Product>(OnProductDec); } }
private void OnProductInc(Product product) { ++product.Count; }
private void OnProductDec(Product product) { --product.Count; }
public MainWindow()
{
Cola = new Product { Count = 0 };
this.DataContext = this;
InitializeComponent();
}
}