【问题标题】:How to remove a character at the beginning of each line in Richtextbox如何删除Richtextbox中每行开头的字符
【发布时间】:2015-05-07 21:11:04
【问题描述】:

我正在创建一个应用程序,如果您单击按钮,它将为每个选定的行删除特定字符。

例如:我在RichTextbox的每一行中都有“//”并将文本着色为红色,这就像Visual Studio中的注释和取消注释功能。

问题是如何删除每一行中的“//”并以其默认颜色返回颜色?

此代码用于添加“//”并将其着色为红色:

private void toolStripButton1_Click(object sender, EventArgs e)
{
if (richTextBox1.Text.Length > 0 && richTextBox1.SelectionLength >= 0)
{
    string[] lines = richTextBox1.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
    Color normalColor = Color.Black, commentColor = Color.Red;
    int selStart = richTextBox1.SelectionStart, selEnd = selStart + richTextBox1.SelectionLength,
    startLine = -1, endLine = -1, lineSum = 0, k = 0;

    for (k = 0; k < lines.Length; k++)
        if (startLine == -1)
        {
            if ((lineSum += lines[k].Length + 1) > selStart)
            {
                startLine = k;
                if (selEnd <= lineSum) endLine = k;
            }
        }
        else if (endLine == -1)
        {
            if ((lineSum += lines[k].Length + 1) >= selEnd)
                endLine = k;
        }
        else break;

    for (int i = 0; i < lines.Length; i++)
        lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];

    richTextBox1.Text = "";
    richTextBox1.SelectionStart = 0;
    for (int i = 0; i < lines.Length; i++)
    {
        richTextBox1.SelectionStart = richTextBox1.Text.Length;
        richTextBox1.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
        richTextBox1.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
    }

    int selectStarIndx = richTextBox1.GetFirstCharIndexFromLine(startLine), selectEndIndx = richTextBox1.GetFirstCharIndexFromLine(endLine + 1);
    if (selectEndIndx == -1) selectEndIndx = richTextBox1.Text.Length;

    richTextBox1.Select(selectStarIndx, selectEndIndx - selectStarIndx);
    richTextBox1.Focus();
}
}

请帮助我如何使用新按钮取消注释行。

【问题讨论】:

  • 使用方法并摆脱事件处理程序和主窗体中的大量代码。否则你以后不能在这个项目中重新引用任何东西
  • Stackoverflow 是一种学习很多东西的好方法。同时,如果你每件事都依赖它,那么你将永远是一个用户,而不是成为一名优秀的开发人员。为什么我的意思是,如果你得到一些要评论的东西,那么你应该尝试自己去获得未评论的东西。否则stackoverflow不是帮助,而是上瘾..
  • 好的先生,我明白你在说什么.. 但感谢你回答我,即使我问得太多并且不是我自己学习的。再次感谢您,请与我交谈,因为明天我的论文有截止日期,我将在第二天为我的教授辩护代码
  • 如果你好好研究过my post,你会发现它也包含了这个问题的答案..

标签: c# winforms richtextbox


【解决方案1】:
private void commentOrUnComment(RichTextBox rtb, bool isUnComment)
{
    if (rtb.Text.Length > 0 && rtb.SelectionLength >= 0)
    {
        string[] lines = rtb.Text.Split(new string[] { Environment.NewLine, "\n", "\r", "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
        Color normalColor = Color.Black, commentColor = Color.Red;
        int selStart = rtb.SelectionStart, selEnd = selStart + rtb.SelectionLength,
        startLine = -1, endLine = -1, lineSum = 0, k = 0;

        for (k = 0; k < lines.Length; k++)
            if (startLine == -1)
            {
                if ((lineSum += lines[k].Length + 1) > selStart)
                {
                    startLine = k;
                    if (selEnd <= lineSum) endLine = k;
                }
            }
            else if (endLine == -1)
            {
                if ((lineSum += lines[k].Length + 1) >= selEnd)
                    endLine = k;
            }
            else break;

        for (int i = 0; i < lines.Length; i++)
            if (isUnComment)
                lines[i] = (i >= startLine && i <= endLine ? (lines[i].TrimStart().StartsWith("//") ? lines[i].Substring(0, lines[i].IndexOf("//"))
                        + lines[i].Substring(lines[i].IndexOf("//") + 2) : lines[i]) : lines[i]);
            else
                lines[i] = (i >= startLine && i <= endLine ? "//" : "") + lines[i];

        rtb.Text = "";
        rtb.SelectionStart = 0;
        for (int i = 0; i < lines.Length; i++)
        {
            rtb.SelectionStart = rtb.Text.Length;
            rtb.SelectionColor = lines[i].TrimStart().StartsWith("//") ? commentColor : normalColor;
            rtb.SelectedText = lines[i] += (i == lines.Length - 1 ? "" : "\r\n");
        }

        int selectStarIndx = rtb.GetFirstCharIndexFromLine(startLine), selectEndIndx = rtb.GetFirstCharIndexFromLine(endLine + 1);
        if (selectEndIndx == -1) selectEndIndx = rtb.Text.Length;

        rtb.Select(selectStarIndx, selectEndIndx - selectStarIndx);
        rtb.Focus();
    }
}

private void btnComment_Click(object sender, EventArgs e)
{
    commentOrUnComment(richTextBox1, false);
}

private void btnUncomment_Click(object sender, EventArgs e)
{
    commentOrUnComment(richTextBox1, true);
}

【讨论】:

  • 嗨先生..你的回答真的帮助了我..你太棒了..谢谢,上帝保佑你
  • 随时欢迎您。不要忘记标记为答案,并在问题中阅读我对 cme​​ts 的建议
  • 如果它对您有帮助,您可以通过点击“upvote”按钮为那些对您有帮助的答案投票
  • 好的,先生,我会的。我还有最后一个问题,我如何以编程方式执行此操作示例:/*sample*/ 这是 VS 中的组注释,我该怎么做
猜你喜欢
  • 1970-01-01
  • 2013-10-30
  • 2016-09-09
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多