【问题标题】:What i need to add to automaticaly update textBox2 with a LineCounter for the textBox1 input?我需要添加什么来使用 textBox1 输入的行计数器自动更新 textBox2?
【发布时间】:2021-08-22 00:48:58
【问题描述】:

我的目标是将 textBox1 输入的行/行计数器获取到 textBox2 并实时显示/更新它。 到目前为止,它只有在我单击并在 textBox2 中输入内容时才有效。

private void InitializeComponent()
    {
        this.textBox1 = new System.Windows.Forms.TextBox();
        this.textBox2 = new System.Windows.Forms.TextBox();
        this.SuspendLayout();
        // 
        // textBox1
        // 
        this.textBox1.Location = new System.Drawing.Point(288, 47);
        this.textBox1.Multiline = true;
        this.textBox1.Name = "textBox1";
        this.textBox1.Size = new System.Drawing.Size(301, 193);
        this.textBox1.TabIndex = 0;
        this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
        // 
        // textBox2
        // 
        this.textBox2.Location = new System.Drawing.Point(544, 265);
        this.textBox2.Name = "textBox2";
        this.textBox2.Size = new System.Drawing.Size(45, 20);
        this.textBox2.TabIndex = 1;
        this.textBox2.TextChanged += new System.EventHandler(this.textBox2_TextChanged);

这里是 Form()

private void textBox1_TextChanged(object sender, EventArgs e)
    {
        
    }

private void textBox2_TextChanged(object sender, EventArgs e)
    {
        string strtext = textBox1.Text;
        var textArr = strtext.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
        textBox2.Text = textArr.Length.ToString();
    }

【问题讨论】:

    标签: c# visual-studio winforms


    【解决方案1】:

    这段代码:

    string strtext = textBox1.Text;
    var textArr = strtext.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
    textBox2.Text = textArr.Length.ToString();
    

    都应该在textBox1_TextChanged 函数中而不是在textBox2_TextChanged 函数中,因为您希望在 textBox1 中的文本发生更改时执行该代码。

    【讨论】:

      【解决方案2】:

      您可能不需要如此复杂的代码。文本框有一个 Lines 属性,它返回一个行数组:

      lineCountLabel.Text = editorTextBox.Lines.Count.ToString();
      

      但我记得它的性能和你在那里的一样;将内容拆分为一个数组,这是一个难以置信代价高昂的操作,需要每次用户按下一个键,尤其是在文本框很长的情况下。您的程序将执行的字符串分配次数,不断将字符串拆分为数组并将其丢弃,只是为了获得计数,这是令人难以置信的资源浪费

      相反,我建议您只计算换行符:

      lineCountLabel.Text = editorTextBox.Text.Count(c => c == '\n').ToString();
      

      如果您的文本框有很多文本,只有在它们停止输入时才执行此操作:在表单上以 1 秒的间隔设置一个计时器,禁用并启用 TextChanged 中的计时器(它会重置超时)并将计时器滴答事件中的计数代码。在滴答事件中也停止计时器


      ps:注意到你有一个逻辑来删除空条目,如果有意的话,我想这意味着你只计算句子而不是行。考虑一下,如果这是你想要的,像这样的:

      int c = 0; int p = Environment.NewLine.Length; int s = lineCountLabel.Text; int n = Environment.NewLine.Last();
      
      for(int x = p; x<s.Length-1; x++)
        if(s[x] == n && s[x-p] != n)
          c++;
      
      lineCountLabel.Text = c.ToString();
      

      这可以用 LINQ 完成,但是有点乱

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2019-08-26
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多