【问题标题】:Erasing last line from multiline textbox从多行文本框中删除最后一行
【发布时间】:2014-08-11 18:09:06
【问题描述】:

我正在编写一个小的 winform 程序,它有一个多行文本框和一个“清除”按钮。我附加到此文本框的每个字符串始终以“\r\n”结尾。

textBoxScriptSteps.AppendText(ClickedButton + "\r\n");

我只想在每次单击清除按钮时删除最后一行。

在谷歌中搜索但找不到任何解决方案。 请对此提供任何帮助。

【问题讨论】:

  • 到目前为止你有这方面的尝试吗?我的猜测是您可以在字符串 textBoxScriptSteps.Text 中找到最后一次出现的 "\r\n" 并从那里开始。只是给你一个大概的想法。
  • 最简单的方法(我认为)是获取内容,根据您的换行符将它们拆分为一个数组/列表,然后在没有最后一个元素的情况下重新输出列表......但没有向我们展示你的尝试,恐怕你不会得到任何具体的解决方案......
  • 是的,我试过了,但它什么也没做。 textBoxScriptSteps.Text.Remove(textBoxScriptSteps.Text.LastIndexOf("\r\n"));
  • string[] textlines = textBoxScriptSteps.Lines; textlines = textlines.Where(x => x != textlines[textlines.Count].ToArray(); textBoxScriptSteps.Lines = textlines; 你的意思是这样的?别忘了加:using System.Linq;
  • 字符串是不可变的。如果你只是做了.Remove 而没有实际将返回的值分配回textBoxScriptSteps.Text,当然它不会做任何事情。有可能有效(假设字符串不以\r\n 结尾)但只是没有对正确的字符串做任何事情:)

标签: c# textbox


【解决方案1】:

这对我有用

HTML 标记:

<asp:TextBox ID="TextBox1" runat="server" TextMode="MultiLine"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />

代码背后:

protected void Button1_Click(object sender, EventArgs e)
{
    TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.LastIndexOf(Environment.NewLine));
}

来源:http://social.msdn.microsoft.com/Forums/en-US/29118aad-dfec-4453-a653-18fa51d63252/how-to-clear-the-last-line-in-multiline-textbox?forum=vblanguage

【讨论】:

    【解决方案2】:

    我找到了一个更简单的方法:

    TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.LastIndexOf(Environment.NewLine);
    TextBox1.Text = TextBox1.Text.Remove(TextBox1.Text.LastIndexOf(Environment.NewLine);
    TextBox1.AppendText("\r\n");
    

    【讨论】:

      【解决方案3】:

      不要这样做。 保留List&lt;string&gt;。每次单击添加时,将元素添加到列表中。 每次单击清除时,从末尾删除一个元素。

      有一个 UpdateText 方法来设置文本框中的文本。在添加和清除方法的末尾调用 UpdateText。

      将数据与显示分开。

      【讨论】:

        【解决方案4】:

        我建议这样做:

        public String RemoveLastLine(String myStr)
        {
            String result = "";
            if (myStr.Length > 2)
            {
                // Ignore very last new-line character.
                String temporary = myStr.Substring(0, myStr.Length - 2);
        
                // Get the position of the last new-line character.
                int lastNewLine = temporary.LastIndexOf("\r\n");
        
                // If we have at least two elements separated by a new-line character.
                if (lastNewLine != -1)
                {
                    // Cut the string (starting from 0, ending at the last new-line character).
                    result = myStr.Substring(0, lastNewLine);
                }
            }
            return (result);
        }
        

        您可以使用此功能获取没有最后一行的文本。
        例如,用"Hello\r\nWorld\r\n" 调用RemoveLastLine(); 将得到"Hello\r\n"。
        所以每次按下按钮都可以textBoxScriptSteps.Text = RemoveLastLine(textBoxScriptSteps.Text);。


        同样的功能更紧凑:

        public String RemoveLastLine(String myStr) {
            if (myStr.Length > 2) {
                int lastNewLine;
                if ((lastNewLine = myStr.Substring(0, myStr.Length - 2).LastIndexOf("\r\n")) != -1)
                    return (myStr.Substring(0, lastNewLine));
            }
            return ("");
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-08
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-22
          相关资源
          最近更新 更多