如何将按钮中的某些值发送到文本框中?

时间:2016-06-24 08:48:38

标签: c#

我累了试试, 谁成了我的问题是,我拥有钥匙,第一把钥匙和加1按钮再次减去点击加号按钮的方式然后出现在文本框中的数字1,如果按下将再次增加按钮更少。 这是我的代码:

private void BtnTmbhCola_Click(object sender, EventArgs e)
{
   Convert.ToInt32(txtCola.Text + 1);
   txtdvr8chanel.Text = Val(txtdvr8chanel.Text) + 1;
   txttotaldvr.Text = Format(Val(txttotaldvr.Text), "Rp,#,###,###,0.000.000") + 530000;

   If (txtdvr8chanel.Text = 1 )
        {
            btntmbh8ch.Enabled = True;
        }
}

我感到很恐怖

2 个答案:

答案 0 :(得分:1)

您只需在按钮中添加简单代码,关注验证和事项。

    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();
        }
    }

答案 1 :(得分:0)

我能想象的最简单的版本就是这个:

的.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";
}

看到你正在建立一个商店,你可能想深入了解WPF。

考虑使用MVVM意义上的产品模型,并创建一个视图来显示它并使用视图模型连接它们。

将CartItem设为模型,...将购物车作为模型,......

这是一个更复杂的例子,指出了你的方向。 的.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();
    }
}