Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
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...


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?
Posted
Updated 4-Mar-11 8:04am
v3
Comments
Sergey Alexandrovich Kryukov 4-Mar-11 2:44am    
Such code editor would be miserable, why wasting time for such work? School?
--SA
CPallini 4-Mar-11 3:16am    
Yes, I guess. :-)
T-Joe 4-Mar-11 14:00pm    
If i get started with such things i believe i can make it useful. And it seems you know a lot about editor. Why won't you share your knowledge with me?
Sergey Alexandrovich Kryukov 4-Mar-11 16:49pm    
I do have an editor for an article (and developed few before, use them all the time), but it will take too much time to complete it, I have more articles still waiting in the queue. Main idea of my editor is to support key macro like in Microsoft and Borland Strl+Shift+R/Strl+Shift+P and plug-in architecture (for imports/exports and text processing). It all works but can be explained on some 10 pages by my estimate, quite beyond Quick Questions. Also, minimum functionality includes many detail like indicator of current line/position (regular notepad doesn't have is which makes using it a disaster), also, support of set of encodings (Unicode requires some 5 UTF variants + ANSI), plus encoding conversions.

Required time is the only reason I did not share it so far.
--SA

1 solution

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();
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) 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!
 
Share this answer
 
Comments
Dalek Dave 4-Mar-11 3:51am    
Excellent answer Mr Griff!
T-Joe 4-Mar-11 4:28am    
Thank you so much. It helped me a lot.
T-Joe 4-Mar-11 4:42am    
How do you change whole line's back color? current line
OriginalGriff 4-Mar-11 4:44am    
See (2) in my answer...it does exactly that
T-Joe 4-Mar-11 4:50am    
yeah. But i mean whole line from start to end. Not only background of text but also blank part of line.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900