需要创建源代码编辑器 [英] Need to create source code editor

查看:86
本文介绍了需要创建源代码编辑器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要创建源代码编辑器.
类似于Notepad ++,但功能可能更少.
主要内容是:

Need to create source code editor.
Like Notepad++, but features can be less.
Main things are:

1. count line number and display it in front of lines.
2. highlight current line.
3. max number of character in single line should not exceed 72. ( <= 72)
...
 Background vertical line
 Highlight key words etc...



我正在使用RichTextBox.如果有人知道如何在RichTextBox中完成这三件事,请告诉我.还是有其他方法可以做到?



I''m using RichTextBox. if someone knows how these 3 things are done in RichTextBox then let me know. Or is there any other way to do this?

推荐答案

1)使用RichTextBox.Lines属性,然后Length是行数.这确实将所有行作为单独的字符串检索,但前提是这不是用于处理大量数据的情况,那么您应该没事.另一种选择是使用RichTextBox.GetLineFromChar并提供文本框的总大小:
1) Use the RichTextBox.Lines property, and then the Length is the number of lines. This does retrieve all the lines as individual strings, but provided this isn''t for latrge amounts of data, you should be ok. The other alternative is to use RichTextBox.GetLineFromChar and provide the total size of the textbox:
int lines = myRichTextBox.GetLineFromCharIndex(myRichTextBox.TextLength) + 1;
labNumberOfLines.Text = lines.ToString();

但是它并不那么明显,需要很好地评论.

2)尝试:

But that it not as obvious and needs commenting well.

2)Try:

int startOfLine = myRichTextBox.GetFirstCharIndexOfCurrentLine();
int currentLine = myRichTextBox.GetLineFromCharIndex(startOfLine);
myRichTextBox.Select(startOfLine, myRichTextBox.Lines[currentLine].Length);
myRichTextBox.SelectionBackColor = Color.Yellow;



3)处理KeyPress事件:



3) Handle the KeyPress event:

private void myRichTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    int startOfLine = myRichTextBox.GetFirstCharIndexOfCurrentLine();
    int currentLine = myRichTextBox.GetLineFromCharIndex(startOfLine);
    string[] lines = myRichTextBox.Lines;
    if (lines.Length > 0)
        {
        int currentLineLength = lines[currentLine].Length;
        if (char.IsLetterOrDigit(e.KeyChar))
            {
            if (currentLineLength > 72)
                {
                e.Handled = true;
                }
            }
        }
    }

这只是一个例子:您将需要做更多检查!

This is just an example: you will need to do more checking!


这篇关于需要创建源代码编辑器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆