【问题标题】:TextBox readline with button command带有按钮命令的文本框读取行
【发布时间】:2013-10-03 23:51:34
【问题描述】:

我只是想弄清楚如何在 TextBox 中输入一个数字,然后单击一个按钮并让该数字进入另一个 TextBox。我知道我需要将价格设为双倍值并将其设为零。我只是想知道如何让按钮控制第一个 TextBox。

private void DepositTextBox_TextChanged(object sender, EventArgs e)
    {
        string deposits = Console.ReadLine();
        double deposit = double.Parse(deposits);
        deposit += balance;
    }

    private void WithdrawTextBox_TextChanged(object sender, EventArgs e)
    {
        string withdraws = Console.ReadLine();
        double withdraw = double.Parse(withdraws);
        withdraw += balance;
    }

这是我的代码,但是当我在文本框中输入数字或字母后立即运行它时,它说值不能为空,参数名称:值。

【问题讨论】:

  • 在您的按钮点击事件中,Textbox1.Text = Textbox2.Text
  • 是按钮 我将在第一个文本框中输入数字并让它在第二个文本框中主动显示它。 Textbox1.Text = Textbox2.Text 会起作用吗?
  • 我尝试了该代码,但它甚至不允许我在 TextBox 中输入值。
  • 看起来不太好。如果您正在使用 WinForms,为什么要阅读 Console 中的一行?要获取要解析的文本,您可以简单地从存款 TextBox 中获取 Text 属性,解析,然后将值放在提款 TextBox 上的 Text 属性上。
  • 我没有从存款转到提款,他们各自做不同的事情,存款箱向我的第三个文本框添加一个值,提款框从第三个文本框中取出一个值.

标签: c# winforms button textbox


【解决方案1】:

为什么您说的是按钮单击,但您的代码示例显示了 TextChanged 事件?

听起来您可能应该创建一个表单级属性来存储总余额并对其进行操作。

在您的 ButtonClick 事件中,将 textbox.Text 转换为数字类型,然后对总余额执行适当的数学运算。

然后只需在另一个文本框中显示该余额属性即可。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private double balance;

    private void btnDeposit_Click(object sender, EventArgs e)
    {
        double value = Convert.ToDouble(txtDeposit.Text);
        balance += value;

        txtBalance.Text = balance.ToString();
    }

    private void btnWithdraw_Click(object sender, EventArgs e)
    {
        double value = Convert.ToDouble(txtWithdraw.Text);
        balance -= value;

        txtBalance.Text = balance.ToString();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-26
    • 2011-02-21
    • 2013-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-11-18
    相关资源
    最近更新 更多