 |
|
 |
Hi,
This project saved me HOURS of work!!
I only found a bug:
If I delete all the text in the richtextbox, the numbers disappear and don't reappear.
I fixed this bug, with this code:
Private Sub MyRichTextBox_TextChanged(sender As System.Object, e As System.EventArgs) Handles MyRichTextBox.TextChanged
If Me.MyRichTextBox.Text = "" Then
Me.MyPictureBox.Invalidate()
End If
End Sub
Anyway, it's really a good work! 5/5!!
|
|
|
|
 |
|
 |
Hi there how things
Great code!!!
Would ya mind if i were to use it in my college project? I am developing a simple IDE. Of course I will reference your work etc!
Thanks a mil
|
|
|
|
 |
|
 |
Hi there.
Thank u Michael.
I have been looking for this a long time.
Good luck
Only God
|
|
|
|
 |
|
 |
When I am clicking on the carets vscroll event is fired but when I am moving the thumb of the scroll bar vscroll event is not fired? I don't have a clue how to solve this problem!!! Please help!!!
|
|
|
|
 |
|
 |
In your code..
'calculate font heigth as the difference in Y coordinate between line 2 and line 1
'note that the RichTextBox text must have at least two lines. So the initial Text property of the RichTextBox should not be an empty string. It could be something like vbcrlf & vbcrlf & vbcrlf
Dim font_height As Single = MyRichTextBox.GetPositionFromCharIndex(MyRichTextBox.GetFirstCharIndexFromLine(2)).Y - MyRichTextBox.GetPositionFromCharIndex(MyRichTextBox.GetFirstCharIndexFromLine(1)).Y
If font_height = 0 Then Exit Sub
Instead of ensuring there are at least a couple lines of text in the control, I use this...
If font_height = 0 Then
font_height = Me.MyRichTextBox.Font.GetHeight()
End If
I also noticed that if you resize the controls, sometimes you see it printing half of a "0", so to fix this, where you are printing the variable "i", I use an if statement to check if i > 0.
These were just a couple things I noticed and thought I would offer my input.
-Jason
|
|
|
|
 |
|
 |
What I mean, is how do you compensate for the font size changing so that the number list matches that of the font size in the myrichtextbox? This is the only dilemma that i have, any help would be great. Thanks.
brottmayer
|
|
|
|
 |
|
 |
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Gutter
{
public partial class frmMain : Form
{
int total_lines = 0;
public frmMain()
{
InitializeComponent();
}
private void DrawRichTextBoxLineNumbers(Graphics g)
{
Single font_height;
if (richTextBox1.Lines.Length < 3)
{
font_height = richTextBox1.Font.Height;
}
else
{
font_height = richTextBox1.GetPositionFromCharIndex(richTextBox1.GetFirstCharIndexFromLine(2)).Y - richTextBox1.GetPositionFromCharIndex(richTextBox1.GetFirstCharIndexFromLine(1)).Y;
}
Point pos = new Point (0,(int)(g.VisibleClipBounds.Y + font_height / 3));
int first_index, first_line, first_line_y;
first_index = richTextBox1.GetCharIndexFromPosition(pos);
first_line = richTextBox1.GetLineFromCharIndex(first_index);
first_line_y = richTextBox1.GetPositionFromCharIndex(first_index).Y;
g.Clear(Control.DefaultBackColor);
int i = first_line;
int x = 0;
Single y = 0;
total_lines = richTextBox1.GetLineFromCharIndex(Int32.MaxValue) + 1;
while (y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height)
{
y = first_line_y - 1 + font_height * (i - first_line - 1);
x = panel1.Width - (int)g.MeasureString(i.ToString(), richTextBox1.Font).Width - 5;
if (i <= total_lines)
{
g.DrawString((i).ToString(), richTextBox1.Font, Brushes.DarkBlue, x, (int)y);
}
i++;
}
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
DrawRichTextBoxLineNumbers(e.Graphics);
}
private void richTextBox1_Resize(object sender, EventArgs e)
{
panel1.Invalidate();
}
private void richTextBox1_VScroll(object sender, EventArgs e)
{
panel1.Invalidate();
}
private void richTextBox1_TextChanged(object sender, EventArgs e)
{
if ((richTextBox1.GetLineFromCharIndex(Int32.MaxValue) + 1) != total_lines)
{
panel1.Invalidate();
}
}
}
}
This doesn't require at least 2 lines in the richTextBox. Just get the font height from the richTextBox font height property. I can image that this is going to be fairly slow because you can actually see the gutter being updated (white flashes). I wonder what would happen if DrawTextBoxLineNumbers() were called in a thread.
-- modified at 9:18 Monday 19th February, 2007
|
|
|
|
 |
|
 |
Thanks a lot.
This conversion to C# was very helpful. Warks good
For me this is best programed line numbering I have seen.
|
|
|
|
 |
|
 |
I realy like it!
Soon I'll have to implement something similar + a code block indicator (something like the c# editor from VS) + some intellisense
|
|
|
|
 |
|
 |
Plz I need your help
i want to send email msg from system windows form (mail format = TXT)
mail body = RichTextBox1.text
i want select mail body.(font + color + siz) from RichTextBox1 Properties
thnx
Ahmed El-Badry
|
|
|
|
 |
|
 |
Hi!
I just stumbled across your article because of a question in the forums and saw that there's a resource leak that you should fix.
Instead of creating a new Graphics object every time and not disposing of it you should simply invalidate the PictureBox to let it do it's own painting.
Simply replace your calls toDrawRichTextBoxLineNumbers(MyPictureBox.CreateGraphics) withMyPictureBox.Invalidate() and you should be fine.
Regards,
mav
--
Black holes are the places where God divided by 0...
|
|
|
|
 |
|
 |
Thanks for the tip. I have modified the code accordingly.
|
|
|
|
 |
|
 |
Hi,
I was trying to modify the code to make it show line numbers ONLY upto the number of lines of text contained in the Rich Text Box - instead of showing all possible line numbers upto the VisibleClipBounds. As I increase text in the RTBox, new line numbers should be displayed. Can you please point me to the right direction?
Thanks,
k^b
PeoPLe aRe cRazY anD tiMeS aRe sTRaNGe, i'M LoCKeD iN tiGhT, i'M OuT oF raNGe, i uSeD t0 caRe BuT - thiNGs haVe chaNGeD.
|
|
|
|
 |
|
 |
Try this new DrawRichTextBoxLineNumbers method:
Private Sub DrawRichTextBoxLineNumbers(ByRef g As Graphics)
Dim total_lines As Integer
With MyRichTextBox
Dim font_height As Single
font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
- .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
If font_height = 0 Then Exit Sub
'Get the first line index and location
Dim first_index As Integer
Dim first_line As Integer
Dim first_line_y As Integer
first_index = .GetCharIndexFromPosition(New _
Point(0, g.VisibleClipBounds.Y + font_height / 3))
first_line = .GetLineFromCharIndex(first_index)
first_line_y = .GetPositionFromCharIndex(first_index).Y
'get the total number of lines in the richtextbox
total_lines = MyRichTextBox.Lines.Length
'Print on the PictureBox the visible line numbers of the RichTextBox
g.Clear(Control.DefaultBackColor)
Dim i As Integer = first_line
Dim y As Single
Do While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
y = first_line_y + 2 + font_height * (i - first_line - 1)
If total_lines <= i Then
g.DrawString((i).ToString, .Font, Brushes.DarkBlue, MyPictureBox.Width _
- g.MeasureString((i).ToString, .Font).Width, y)
Else
Exit Do
End If
i += 1
Loop
End With
End Sub
Michael Elly
|
|
|
|
 |
|
 |
..mm that doesn't seem to work until i change your:
If total_lines <= i Then
to
If total_lines >= i Then
and delete the else stmt, ..course maybe I should stop coding after 2am..
|
|
|
|
 |
|
 |
Thanks for the correction (I did not check the code snippet I replied to you with, just wrote it off hand... ).
Anyway after playing with your use case a bit more I realized that there is no event that takes care of updating the line numbers when a new line is just added. So two things need to happen (1) the total_lines variable need to be a class level variable (2) need to add one more event for MyRichTextBox_TextChanged.
The overall code for making it show line numbers only up to where there are actual lines is:
Public Class Form1
Dim total_lines As Integer
Private Sub DrawRichTextBoxLineNumbers(ByRef g As Graphics)
With MyRichTextBox
Dim font_height As Single
font_height = .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(2)).Y _
- .GetPositionFromCharIndex(.GetFirstCharIndexFromLine(1)).Y
If font_height = 0 Then Exit Sub
'Get the first line index and location
Dim first_index As Integer
Dim first_line As Integer
Dim first_line_y As Integer
first_index = .GetCharIndexFromPosition(New _
Point(0, g.VisibleClipBounds.Y + font_height / 3))
first_line = .GetLineFromCharIndex(first_index)
first_line_y = .GetPositionFromCharIndex(first_index).Y
'get the total number of lines in the richtextbox
'total_lines = MyRichTextBox.Lines.Length
total_lines = MyRichTextBox.GetLineFromCharIndex(Int32.MaxValue) + 1
'Print on the PictureBox the visible line numbers of the RichTextBox
g.Clear(Control.DefaultBackColor)
Dim i As Integer = first_line
Dim y As Single
Do While y < g.VisibleClipBounds.Y + g.VisibleClipBounds.Height
y = first_line_y + 2 + font_height * (i - first_line - 1)
If total_lines >= i Then
g.DrawString((i).ToString, .Font, Brushes.DarkBlue, MyPictureBox.Width _
- g.MeasureString((i).ToString, .Font).Width, y)
Else
Exit Do
End If
i += 1
Loop
End With
End Sub
Private Sub r_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyRichTextBox.Resize
MyPictureBox.Invalidate()
End Sub
Private Sub r_VScroll(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyRichTextBox.VScroll
MyPictureBox.Invalidate()
End Sub
Private Sub p_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyPictureBox.Paint
DrawRichTextBoxLineNumbers(e.Graphics)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
MyRichTextBox.Text = vbCrLf & vbCrLf & vbCrLf
End Sub
Private Sub MyRichTextBox_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyRichTextBox.TextChanged
If MyRichTextBox.GetLineFromCharIndex(Int32.MaxValue) + 1 <> total_lines Then MyPictureBox.Invalidate()
End Sub
End Class
One more comment: The control was designed for RichTextBox with WordWrap=False.
|
|
|
|
 |
|
 |
with word wrap on the MyRichTextBox.Lines.Length isn't right, use
total_lines = MyRichTextBox.GetLineFromCharIndex(Int32.MaxValue) + 1
|
|
|
|
 |
|
|
 |
|
 |
are you using .NET 2.0?
and what exactly is the problem?
http://www.stormbase.net
|
|
|
|
 |
|
 |
I just sent a new package. The sample application now works out of the box.
|
|
|
|
 |
|
 |
Works great, the line font changed and increased with the text box. Very nice.
Thx.
|
|
|
|
 |