【问题标题】:C# Need help editing my line numbering codeC#需要帮助编辑我的行号代码
【发布时间】:2013-02-28 03:25:05
【问题描述】:

我得到了一个行编号代码,它非常适合以常规方式对行进行编号,但我正在寻找一些不同的东西。我希望我的代码仅在我按 Enter 键时计算换行符(程序收到返回键码),而不是文本框自动使用自动换行剪切行。这是我现在使用的代码:

//Instructions
    int maxLC = 1; //maxLineCount - should be public
    private void InstructionsSyncTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        int linecount = InstructionsSyncTextBox.GetLineFromCharIndex(InstructionsSyncTextBox.TextLength) + 1;
        if (linecount != maxLC)
        {
            InstructionsLineNumberSyncTextBox.Clear();
            for (int i = 1; i < linecount + 1; i++)
            {
                InstructionsLineNumberSyncTextBox.AppendText(Convert.ToString(i) + "\n");
            }
            maxLC = linecount;
        }
    }

我认为最简单的方法是每次有人按 Enter 键时保存行数到列表中,并且每次有人按 Enter 键时都会更新行号 texbox,其中每个行号位于列表中的位置。但我不知道如何检测退货何时被删除。有人知道怎么解决吗?

【问题讨论】:

  • 你不能只使用正则表达式并搜索 \r 的所有实例吗?

标签: c# return word-wrap line-numbers


【解决方案1】:

非常基本的示例,用于计算您发布的代码中的行数:

    class Program
    {
        static void Main(string[] args)
        {
            string stringFromTextBox = 
@"  int maxLC = 1; //maxLineCount - should be public
    private void InstructionsSyncTextBox_KeyUp(object sender, KeyEventArgs e)
    {
        int linecount = InstructionsSyncTextBox.GetLineFromCharIndex(InstructionsSyncTextBox.TextLength) + 1;
        if (linecount != maxLC)
        {
            InstructionsLineNumberSyncTextBox.Clear();
            for (int i = 1; i < linecount + 1; i++)
            {
                InstructionsLineNumberSyncTextBox.AppendText(Convert.ToString(i) + ""\n"");
            }
            maxLC = linecount;
        }
    }";
            Regex r = new Regex("\r", RegexOptions.Multiline);
            int lines = r.Matches(stringFromTextBox).Count;
            //You'll need to run this line every time the user presses a key

        }
    }

【讨论】:

  • 我不知道您是否只是看错了我写的内容,或者您​​是否在钓鱼,但这绝不是我需要帮助的地方。我需要编辑代码以仅计算返回,而不是计算我发布的代码中的行...
  • 我发布的代码正是这样做的,它计算了字符串中的所有返回值。如果您需要保留每个返回的位置,r.Matches 返回的 MatchCollection 也将包含该数据。除此之外,你必须澄清你的问题,因为那是你问的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-12
  • 1970-01-01
  • 1970-01-01
  • 2014-07-10
  • 1970-01-01
  • 2020-11-28
  • 2019-02-12
相关资源
最近更新 更多