C# - 任何人都可以帮我一个文本框吗?

时间:2016-10-23 06:49:16

标签: c# winforms

我必须在私有空文本框中放置什么才能让用户输入金额,该金额将应用于等待Connection.SendToServerAsync(2700,790);就是现在。所以让我们说用户在texbox中输入2000,8,然后(2700,790)必须更改为(2000,8)

namespace Application
{
    public partial class Form1 : ExtensionForm
    {
        public Form1()
        {
            InitializeComponent();
        }
        private async void button1_Click(object sender, EventArgs e)
        {
            int repeat = 5;

            for (int i = 0; i <= repeat; i++)
            {
                await Connection.SendToServerAsync(2700, 790);
                await Connection.SendToServerAsync(3745);
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

我得到了这个答案:

您可以使用TextBox.Text获取文本框值。 它以字符串形式出现,因此您必须转换为int。您可以使用以下方法之一: Int.Parse Convert.ToInt32 使用转换后的值,您可以在单击按钮时使用新值调用方法。

有人能通过复制我的代码向我展示它是如何完成的吗?

1 个答案:

答案 0 :(得分:0)

您不需要textBox1_TextChanged()活动

一种肮脏的方式可能是以下

   private async void button1_Click(object sender, EventArgs e)
    {
        int repeat = 5;

        for (int i = 0; i <= repeat; i++)
        {                
            await Connection.SendToServerAsync(2700, Int32.Parse(textBox1.Text); // <--|use the integer value to which textBox1 value can be cast to
            await Connection.SendToServerAsync(3745);
        }
    }

虽然更强大的方法会检查在继续之前将textBox1值实际转换为整数的可能性:

    private async void button1_Click(object sender, EventArgs e)
    {
        int repeat = 5;
        int amount;

        if (Int32.TryParse(textBox1.Text, out amount)) // <--| go on only if textBox1 input value can be cast into an integer
            for (int i = 0; i <= repeat; i++)
            {                
                await Connection.SendToServerAsync(2700, amount); // <--| use the "amount" integer value read from textBox1
                await Connection.SendToServerAsync(3745);
            }
    }
相关问题