如何在 RichTextBox C# 中显示行号

How to show number of a line in a RichTextBox C#(如何在 RichTextBox C# 中显示行号)
本文介绍了如何在 RichTextBox C# 中显示行号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个带有代码突出显示的简单文本和脚本编辑器.为此,我使用 RichTextBox.但我不知道如何让它在左侧显示行号,就像在 VS 或 Notepad++ 中一样.有什么解决办法吗?

I am making a simple text and script editor with code highlighting. For that I use a RichTextBox. But I don't know how to make it show the lines' numbers on the left side, like in VS or Notepad++. Is there any solution?

推荐答案

我尝试重新使用其他地方引用的 codeproject 文章中的代码,但是我查看的每个选项都显得有点太笨拙了.

I tried re-using the code from the codeproject articles referenced elsewhere, but every option I looked at, seemed a bit too kludgy.

所以我构建了另一个显示行号的 RichTextBoxEx.

So I built another RichTextBoxEx that displays line numbers.

可以打开或关闭行号.它很快.它滚动干净.您可以选择数字的颜色、渐变的背景颜色、边框粗细、字体、是否使用前导零.您可以选择按显示"或根据 RTB 中的硬换行对行进行编号.

The line numbering can be turned on or off. It's fast. It scrolls cleanly. You can select the color of the numbers, the background colors for a gradient, the border thickness, the font, whether to use leading zeros. You can choose to number lines "as displayed" or according to the hard newlines in the RTB.

例子:

它有局限性:它只在左侧显示数字.如果你愿意的话,你可以毫不费力地改变它.

It has limitations: it displays numbers only on the left. You could change that without too much effort if you cared.

代码设计为 C# 项目.尽管它是一个更大的解决方案"(一个 XPath 可视化工具)的一部分,但自定义 RichTextBox 被打包为一个可分离的程序集,并且可以在您的新项目中使用.在 Visual Studio 中,只需添加对 DLL 的引用,即可将其拖放到设计图面上.您可以从更大的解决方案中丢弃其他代码.

The code is designed as C# project. Though it's part of a larger "solution" (an XPath Visualization tool), the custom RichTextBox is packaged as a separable assembly, and is ready to use in your new projects. In Visual Studio, just add a reference to the DLL, and you can drag-and-drop it onto your design surface. You can just discard the other code from the larger solution.

查看代码

这篇关于如何在 RichTextBox C# 中显示行号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)