我试图将编号列表功能添加到文本编辑器。 RichTextbox已经提供SelectionBullet
属性来将选项更改为项目符号列表。但我无法找到类似的属性来生成编号列表。是否有任何标准方式在Richtextbox上创建编号列表。 如果不是,我将不得不自己实现,所以代码剪辑可以帮助我做,这将有助于,谢谢。Richtextbox上的编号列表
4
A
回答
2
我知道一个链接并没有被普遍接受为一个好的答案,但是CodeProject上的文章RichTextBox with Search Line Numbering, Bulleting, Printing, Searching Support可能会帮助你找到你想要的东西。
在本文中,作者将RichTextBox控件扩展为可以做你正在问的事情(以及更多),再加上代码发布给所有人看。
1
那么,我执行它如下。
private void btnNumbers_Click(object sender, EventArgs e)
{
string temptext = rtbMain.SelectedText;
int SelectionStart = rtbMain.SelectionStart;
int SelectionLength = rtbMain.SelectionLength;
rtbMain.SelectionStart = rtbMain.GetFirstCharIndexOfCurrentLine();
rtbMain.SelectionLength = 0;
rtbMain.SelectedText = "1. ";
int j = 2;
for(int i = SelectionStart; i < SelectionStart + SelectionLength; i++)
if (rtbMain.Text[i] == '\n')
{
rtbMain.SelectionStart = i + 1;
rtbMain.SelectionLength = 0;
rtbMain.SelectedText = j.ToString() + ". ";
j++;
SelectionLength += 3;
}
}
private void rtbMain_KeyDown(object sender, KeyEventArgs e)
{//this piece of code automatically increments the bulleted list when user //presses Enter key
int tempNum;
if (e.KeyCode == Keys.Enter)
try
{
if (char.IsDigit(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine()]))
{
if (char.IsDigit(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 1]) && rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 2] == '.')
tempNum = int.Parse(rtbMain.Text.Substring(rtbMain.GetFirstCharIndexOfCurrentLine(),2));
else tempNum = int.Parse(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine()].ToString());
if (rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 1] == '.' || (char.IsDigit(rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 1]) && rtbMain.Text[rtbMain.GetFirstCharIndexOfCurrentLine() + 2] == '.'))
{
tempNum++;
rtbMain.SelectedText = "\r\n" + tempNum.ToString() + ". ";
e.SuppressKeyPress = true;
}
}
}
catch{}
}
0
这是我的答案...它易于阅读和优化。我采取了一种非常不同的方法,但添加了删除选择内的编号列表(如果它已存在)的功能。请注意,到目前为止,我只是轻微地测试它,它似乎运作良好......但它可能需要进一步改进。
private void btnOrdered_Click(object sender, EventArgs e)
{
string[] splitSelection = null;
// If selection split selection else split everything
if (this.txtCaptionEditor.SelectionLength > 0)
{
splitSelection = this.txtCaptionEditor.SelectedText.Replace("\r\n", "\n").Split("\n".ToCharArray());
}
else
{
splitSelection = this.txtCaptionEditor.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());
}
bool Exists = false;
for (int i = 0; i < splitSelection.GetLength(0); i++)
{
// If Ordered List Allready exists in selection then remove else add
if (!string.IsNullOrEmpty(splitSelection[i]))
{
if (splitSelection[i].Substring(0, 2) == "1.") { Exists = true; }
}
}
for (int i = 0; i < splitSelection.GetLength(0); i++)
{
int lineCount = (i + 1);
if (Exists)
{
this.txtCaptionEditor.Text = this.txtCaptionEditor.Text.Replace(Convert.ToString(lineCount) + ". ", "");
}
else
{
if(!string.IsNullOrEmpty(splitSelection[i]))
{
this.txtCaptionEditor.Text = this.txtCaptionEditor.Text.Replace(splitSelection[i], Convert.ToString(lineCount) + ". " + splitSelection[i]);
}
}
}
}
private void txtCaptionEditor_KeyDown(object sender, KeyEventArgs e)
{
string[] splitSelection = this.txtCaptionEditor.Text.Replace("\r\n", "\n").Split("\n".ToCharArray());
if (e.KeyCode == Keys.Enter)
{
// Get Current Line Position
int currentLine = this.txtCaptionEditor.GetLineFromCharIndex(this.txtCaptionEditor.SelectionStart);
// Only Run if the previous line is greater than zero
if ((currentLine) >= 0)
{
// Loop through 100 possible numbers for match you can go higher
// If you think your numbered list could go above 100
for (int i = 0; i < 100; i++)
{
if (splitSelection[(currentLine)].Substring(0, 2) == Convert.ToString((i + 1)) + ".")
{
// If the substring of the current line equals a numbered list value.. enumerate next line
this.txtCaptionEditor.SelectedText = "\n" + (i + 2) + ". ";
e.SuppressKeyPress = true;
}
}
}
}
}
相关问题
- 1. richtextbox行号列
- 2. 将RichTextBox上的项目符号列表保存为XML
- 3. 编号表列
- 4. 在Swing上强制编号的列表
- 5. 更改有序列表上的编号?
- 6. 变换的非编号列表编号列表使用XSLT
- 7. 编号列表框
- 8. 子列表编号
- 9. UL列表编号
- 10. 从计划中编号和编号或列表和编号构建列表
- 11. 的Python:在列表编号
- 12. WPF中的编号列表
- 13. 项目符号或编号在WPF的RichTextBox
- 14. c#WPF行和列号从RichTextBox
- 15. 遗留列表编号到我的表
- 16. 跨度编号列表
- 17. 计划编号列表
- 18. 唯一编号列表
- 19. 在reStructuredText中编制编号列表
- 20. 文档编号请求列表的自动编号
- 21. 如何在表中编号列表
- 22. richtextbox高级编辑
- 23. 编号列表视图的内容
- 24. SharePoint列表中的自动编号
- 25. Microsoft Word中的编号列表
- 26. 不同的对象列表编号
- 27. Haskell:在列表中编号的项目
- 28. 返回列表编号的阶乘
- 29. 如何从menuMenu上的menuInfo获取列表项目编号
- 30. 如何bullet或编号为richtextbox silverlight的文本?
该链接很有帮助,谢谢。 – Yohannes
@Yohannes耶,接受?我不会在这里重新发布别人的代码并声明它是我的,这就是为什么我只提供链接。 – iMortalitySX
当然,我只想让更多的人参与,然后我接受 – Yohannes