c# 重复一个私有无效按钮点击

c# repeating a private void button click

如何重复单击私人无效按钮?

代码:

 private void button3_Click(object sender, EventArgs e)
    {
        if (textBox4.Text == "BTC")
        {
            WebClient c = new WebClient();
            var data = c.DownloadString("https://api.cryptonator.com/api/ticker/btc-usd");
            JObject o = JObject.Parse(data);
            richTextBox1.Text = ("BTC Price: " + o["ticker"]["price"]);

如果文本框上的数据是"BTC",则btc价格显示在richtextbox上。

但是如何让它重复,以便每次价格变化都显示在 richtextbox 上?

您不需要重复该事件,而是将您想要的代码片段移动到一个单独的方法中,并在需要时调用它。

private void button3_Click(object sender, EventArgs e)
{
    ValidateData();
}

private void ValidateData()
{
    if (textBox4.Text == "BTC")
    {
        WebClient c = new WebClient();
        var data = c.DownloadString("https://api.cryptonator.com/api/ticker/btc-usd");
        JObject o = JObject.Parse(data);
        richTextBox1.AppendText("BTC Price: " + o["ticker"]["price"]);
    }
}

然后您可以将 OnChange 事件附加到您的控件并在每次用户更改其输入时执行 ValidateData。

I click the button, and 00 is displayed in the richtextbox. I click it again, now 00 is displayed in the texbox just below the 00.

然后您应该使用 richTextBox.AppendText 而不是 richTextBox.Text

...
if (!string.IsNullOrWhiteSpace(richTextBox1.Text) 
    this.richTextBox1.AppendText(Environment.NewLine);

richTextBox1.AppendText("BTC Price: " + o["ticker"]["price"]);