【问题标题】:Appending & removing text from a TextBox在 TextBox 中添加和删除文本
【发布时间】:2013-02-21 06:35:07
【问题描述】:

我目前正在将文本附加到文本框。在 winform 中,我有两个复选框和一个文本框。每次选中复选框时,文本框中都会出现一个文本。但是当复选框未选中时,我很难取出文本。选中复选框时如何追加文本,未选中时如何取出文本?

 private void checkBox1_CheckedChanged(object sender, EventArgs e)
 {
     ck = sender as CheckBox;
     if (ck != null && ck.Checked)
     {
         textBox1.AppendText(" Example1 ");
     }
     else
     {
         textBox1.AppendText("  ");
     }
 }

 private void checkBox2_CheckedChanged(object sender, EventArgs e)
 {
     ck = sender as CheckBox;
     if (ck != null && ck.Checked)
     {
         textBox1.AppendText(" Example2 ");
     }
     else
     {
         textBox1.AppendText("  ");
     }
 }

【问题讨论】:

  • 如果你想清除textBox1,就用textBox1.Text = string.Empty

标签: c# winforms


【解决方案1】:

要仅取出您添加的文字,您可以使用String.Replace

textBox1.Text = textBox1.Text.Replace(" Example1 ", "");

请注意,如果用户更改了该值,则此文本可能会或可能不会仍在TextBox 中。我假设您已经意识到这一点,或者这只是一个练习。

【讨论】:

    【解决方案2】:

    假设你想显示:

    • 第一个复选框被选中时的示例 1
    • 选中第二个时的示例 2
    • 示例 1 和示例 2(如果都选中)
    • 如果两者都未选中,则为空

    最好的办法是将 UI 逻辑集中在一个反映您的规则的方法中:

    该方法与删除我不需要的文本不同。我从一个空列表开始,然后填写是否选中复选框。然后我显示它。通过这种方式,我不必处理尾随分隔符。

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        UpdateTextBox();
    }
    
    private void checkBox2_CheckedChanged(object sender, EventArgs e)
    {
        UpdateTextBox();
    }
    
    void UpdateTextBox()
    {
        var words = new List<string>();
    
        if (checkbox1.Checked)
            words.Add("Example 1");
    
        if (checkbox2.Checked)
            words.Add("Example 2");
    
        textBox1.Text = string.Join(" ", words);
    }
    

    【讨论】:

      【解决方案3】:
      if (ck != null && ck.Checked)
         textBox1.Text = "Example";
      else
         textBox1.Text = "";
      

      【讨论】:

        【解决方案4】:

        你是说

        textBox1.Text = string.Empty
        

        还是我错过了什么?

        【讨论】:

          【解决方案5】:

          试试这个

           private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                  ck = sender as CheckBox;
          
                  if (ck != null && ck.Checked)
                  {
                      textBox1.AppendText(" Example1 ");
                  }
                  else
                  {
                      textBox1.Text = textBox1.Text.Replace(" Example1 ", "");
                  }
              }
          
              private void checkBox2_CheckedChanged(object sender, EventArgs e)
              {
          
                  ck = sender as CheckBox;
          
                  if (ck != null && ck.Checked)
                  {
                      textBox1.AppendText(" Example2 ");
                  }
                  else
                  {
                      textBox1.Text = textBox1.Text.Replace(" Example2 ", "");
                  }
          }
          

          【讨论】:

            猜你喜欢
            • 2011-06-15
            • 1970-01-01
            • 2017-11-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多