繁体   English   中英

文本框更改时的C#自动更新图表

[英]C# auto update chart when textbox is changed

我有一个包含一系列文本框和一个按钮的Windows窗体。 用户需要在文本框中输入一些数据,然后代码使用这些输入进行一些计算。 然后,用户单击按钮,将生成一个图表,显示计算结果。 该图表使用R来完成,R通过R.Net连接到C#。

问题是:一旦用户更改了其中一个文本框的某些输入,如何使图表动态更新(因此无需先单击生成图形的按钮)?

我以为我需要一个循环来不断检查是否有任何文本框已更改,但我无法完成这项工作:

foreach (Control subctrl in this.Controls)
    {
        if (subctrl is TextBox)
        {
            ((TextBox)subctrl).TextChanged += new EventHandler();
        }

    }

TextChanged应该触发buttonClick事件,以便执行生成图形的相关代码。 什么是解决此问题的好方法? 谢谢。

<<<<编辑>>>>>

这是我的表格代码:

public partial class myInputForm : Form
{
    public myInputForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e) 
    {
       // call methods to run calculations using new inputs from textboxes
       plotGraph(); // plot the new results
    }

}

我想将计算方法和绘图函数plotGraph()保留在button1_Click事件内。

试图采纳Refus L的建议,我将以下内容添加到上述部分类myInputForm中:

private void TextBox_TextChanged(object sender, EventArgs e)
{
    TextBox textbox = sender as TextBox;
    if (IsValidText(textBox.Text)) 
    {
         textbox.TextChanged += new System.EventHandler(this.button1_Click);
    }
    else
    {
    DisplayBadTextWarning(textBox.Text);
    }
}


private void myInputForm_Load(object sender, EventArgs e)
{
    foreach (Control control in this.Controls)
    {
        var textBox = control as TextBox;
        if (textBox != null)
        {
            textBox.TextChanged += TextBox_TextChanged;
        }
    }
 } 

但这仍然行不通。 如果我插入以下内容:

this.myFirstBox.textChanged += new System.EventHandler(this.button1_Click);

直接在由表单设计器自动生成的代码中起作用,并且文本框myFirstBox中的更改将触发按钮单击,从而触发绘图。 但是我需要为每个文本框写一行,因为foreach在那里不起作用。 您能解释一下如何设置它以我的形式工作吗? 谢谢。

您可以仅指定要处理事件的现有方法。 理想情况下,您将有一个单独的方法来更新图表,可以从任何地方调用该图表。 然后,您可以从TextChanged或Button_Click事件中调用它,但是这两个控件事件没有联系在一起(在TextChanged中,您可能希望首先对文本进行一些验证)。 另外,如果要从其他位置更新图表,则可以使用一个独立的方法来执行此操作。

例如,如果您具有此方法来更新图表:

private void UpdateChart()
{
    // Do something here to update the chart
}

您可以从事件处理程序中调用它:

private void button1_Click(object sender, EventArgs e)
{
    UpdateChart();
}

private void TextBox_TextChanged(object sender, EventArgs e)
{
    // You might have some data validation on the TextBox.Text here, 
    // which you wouldn't need in the Button_Click event
    TextBox textbox = sender as TextBox;
    if (IsValidText(textBox.Text)) 
    {
        // Now update the chart
        UpdateChart();
    }
    else
    {
        DisplayBadTextWarning(textBox.Text);
    }
}

然后,您可以将所有TextBox.TextChanged事件连接到上面的自定义处理程序:

private void Form1_Load(object sender, EventArgs e)
{
    // Dynamically hook up all the TextBox TextChanged events to your custom method
    foreach (Control control in this.Controls)
    {
        var textBox = control as TextBox;
        if (textBox != null)
        {
            textBox.TextChanged += TextBox_TextChanged;
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM