Go Back  Xtreme Visual Basic Talk > Legacy Visual Basic (VB 4/5/6) > General > RTB Line Numbering with Word Wrap


Reply
 
Thread Tools Display Modes
  #1  
Old 03-13-2003, 01:07 PM
sidhighwind sidhighwind is offline
Newcomer
 
Join Date: Dec 2002
Posts: 16
Default RTB Line Numbering with Word Wrap


I am wondering if any one can help me with this. I am working an advance text editor and i want it to to line number like this when it is word wraped

1
-
-
2
-
3
4
5
-
-

what the '-' is for is when the line wraps i dont want it to number it with the following number i want it ot be a '-' and i dont want it to show the number until the line is actually anew line. my code that i have words when your on line one but once you start scrolling it get off set beacause i am using an api call to get the first visible line. below is my sub that i use to write the line numbers with.
Code:
Private Sub WriteLineNumbers()
   Dim y           As Long
   Dim x           As Long
   Dim lStart      As Long
   Dim FontHeight  As Long
   Dim lFinish     As Long
   Dim lCurrent    As Long

   lStart = SendMessage(RichTxtBox.hwnd, EM_GETFIRSTVISIBLELINE, 0, 0) + 1
   lCurrent = CurrentLine <-- calls an api function
   y = 1
   With picLines
       .Cls
       .Font = RichTxtBox.Font
       FontHeight = .textHeight("12345")
       .CurrentY = Screen.TwipsPerPixelY * 1
       '.CurrentY = .CurrentY + 15
       lFinish = (RichTxtBox.Height / FontHeight) + lStart
       If lFinish > LineCount Then lFinish = LineCount ' LineCount is the api call in a function
       y = lStart
       ' loop from the first visible line in the rtb to the end of the page
       For x = lStart To lFinish
           If x = lCurrent Then
               .FontBold = True
           Else
               .FontBold = False
           End If
           ' check for wordwrap
           If WordWrap Then
               If GetLine(x) Or x = 1 Then
                   picLines.Print Right$("     " & y, 5)
                   y = y + 1
               Else
                   picLines.Print Right$("     -", 5)
               End If
           Else
               picLines.Print Right$("     " & x, 5)
           End If
       Next x
   End With
End Sub
RichTxtBox is a richtext box and picLines is a Picture Box.

Thanks,

Sid
Reply With Quote
  #2  
Old 03-13-2003, 02:45 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

If you think the problem is with the api call, why don't you post that function?
Easiest thing might be to post your project (or a mini project with this functionality), so we can see better what the idea is and what goes wrong.
Reply With Quote
  #3  
Old 03-13-2003, 02:57 PM
FreakOZ FreakOZ is offline
Junior Contributor

* Expert *
 
Join Date: Sep 2002
Location: Sydney, Australia
Posts: 228
Default

sidhighwind,

If you mean the line gets cropped when you scroll due to the RTB smooth scrolling the line you need to subclass the RTB and capture the WM_VScroll message. Check if the entire line is visible by calling the GetScrollPos API and divide the result by i think the textheight and check the result a round number, if not round it using Int() to the next whole number and pass it back to the RTB for processing..

Karl.
Reply With Quote
  #4  
Old 03-13-2003, 06:10 PM
sidhighwind sidhighwind is offline
Newcomer
 
Join Date: Dec 2002
Posts: 16
Default

Quote:
Originally Posted by Deadalus
If you think the problem is with the api call, why don't you post that function?
Easiest thing might be to post your project (or a mini project with this functionality), so we can see better what the idea is and what goes wrong.
Here is the Project. Just run the group. Type in some lines of code and then all you have to do is scroll down and you'll see the problem..
attached is a screen shot.


THanks,

Sid
Attached Images
File Type: jpg screenshot.JPG (50.3 KB, 39 views)
Attached Files
File Type: zip HTMLCtrl.zip (16.0 KB, 31 views)
Reply With Quote
  #5  
Old 03-13-2003, 07:39 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

As you said, the problem is how you're counting the lines. In CurrentLine (and probably also LineCount), you should do that differently when WordWrap is on. One way:
- Get the current line like you do now
- get the first character of that line with SendMessage and EM_LINEINDEX
- count the number of vbCr from the start to that point

Hope that helps.
Edit: corrected mistake
Reply With Quote
  #6  
Old 03-13-2003, 07:44 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,742
Default

Um, this doesn't sound as impressive, but couldn't you check for the ascii enter code? When that is pressed, you add a number, else it will be a -?

I'm just thinking. Not as impressive, but it is just a thought.
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #7  
Old 03-13-2003, 07:49 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

That doesn't make much sense, MikeJ. The problem was printing the right line numbers on the picturebox, not what happens in the textbox when you press Enter.
Reply With Quote
  #8  
Old 03-13-2003, 07:51 PM
MikeJ's Avatar
MikeJ MikeJ is offline
Retread

Retired Moderator
* Expert *
 
Join Date: Sep 2002
Location: Austin, Texas
Posts: 6,742
Default

Oops again, I meant that you detect the ... nevermind ...

My mind is so not here today. Only one week till spring break is killing my mind.
__________________
{ Lex Fori } { Locus Classicus } { Rutilus Scrinium }
Osculare pultem meam!
Reply With Quote
  #9  
Old 03-14-2003, 05:22 AM
sidhighwind sidhighwind is offline
Newcomer
 
Join Date: Dec 2002
Posts: 16
Default

Quote:
Originally Posted by Deadalus
As you said, the problem is how you're counting the lines. In CurrentLine (and probably also LineCount), you should do that differently when WordWrap is on. One way:
- Get the current line like you do now
- get the first character of that line with SendMessage and EM_LINEINDEX
- count the number of vbCr from the start to that point

Hope that helps.
Edit: corrected mistake

ok that makes sense. I'll try that tonight and i'll get back to you about how it works.

Sid
Reply With Quote
  #10  
Old 03-27-2003, 05:11 PM
sidhighwind sidhighwind is offline
Newcomer
 
Join Date: Dec 2002
Posts: 16
Default

Quote:
Originally Posted by Deadalus
As you said, the problem is how you're counting the lines. In CurrentLine (and probably also LineCount), you should do that differently when WordWrap is on. One way:
- Get the current line like you do now
- get the first character of that line with SendMessage and EM_LINEINDEX
- count the number of vbCr from the start to that point

Hope that helps.
Edit: corrected mistake


Deadalus

I am having problems getting this to work. Can you please give me some more pointers. I'm not sure how i'm going to get this going. here is what i though of right now.

1. Get the FirstVisibleLine
2. Find some way to get the number of line returns from line 1 to the first visible line.
3. check the line right above the first visible line for a return. if no return exists place a '-' and then just keep on going down the line from there.

I am just not sure how to do step 2. If you could please provide some pointers it would be great. Thanks!

Edit
here is what i have done so far. it seems to work good but it gets off count when i start scrolling. if you have any ideas. they will be appricated.

Code:
Private Sub WriteLineNumbers() Dim y As Long Dim x As Long Dim lStart As Long Dim FontHeight As Long Dim lFinish As Long Dim lCurrent As Long, _ twiX As Long, _ twiY As Long twiX = Screen.TwipsPerPixelX twiY = Screen.TwipsPerPixelY lStart = FirstVisibleLine lCurrent = CurrentLine With picLineNumbers .Move 2 * twiX, 2 * twiY, picLineNumbers.TextWidth("12345") + twiX, ZeroIfNegative(ScaleHeight - VScrollBarHeight) .Cls .Font = rtbText.Font .FontSize = rtbText.Font.Size FontHeight = .TextHeight("12345") .CurrentY = Screen.TwipsPerPixelY * 2 '.CurrentY = .CurrentY + 15 lFinish = (rtbText.Height / FontHeight) + lStart If lFinish > LineCount Then lFinish = LineCount y = CountLineReturns ' changed this line ' loop from the first visible line in the rtb to the end of the page For x = lStart To lFinish If x = lCurrent Then .FontBold = True Else .FontBold = False End If ' check for wordwrap If WordWrap Then If GetLine(x) Or x = 1 Then picLineNumbers.Print Right$(" " & y, 5) y = y + 1 Else picLineNumbers.Print Right$(" -", 5) End If Else picLineNumbers.Print Right$(" " & x, 5) End If picLineNumbers.Refresh Next x End With End Sub ' added these two functions Private Function CountLineReturns() As Integer Dim FVL As Long Dim x, y As Integer y = 0 FVL = FirstVisibleLine For x = 0 To FVL If GetLineNoMinus(x) Then y = y + 1 End If Next x CountLineReturns = y End Function Private Function GetLineNoMinus(ByVal lineNum As Integer) As Boolean Dim sBuffer As String Dim retVal Dim retVal1 sBuffer = String(255, Chr(32)) retVal = SendMessageStr(rtbText.hWnd, EM_GETLINE, lineNum, ByVal sBuffer) retVal1 = Left(sBuffer, retVal) If Right(retVal1, 1) = vbLf Or Right(retVal1, 1) = vbCrLf Or Right(retVal1, 1) = vbCr Then GetLineNoMinus = True Else GetLineNoMinus = False End If End Function
SiD

Last edited by sidhighwind; 03-27-2003 at 05:33 PM.
Reply With Quote
  #11  
Old 03-27-2003, 08:27 PM
Deadalus Deadalus is offline
Promising Talent

Retired Moderator
* Guru *
 
Join Date: May 2002
Location: Brussels
Posts: 3,601
Default

I'm too tired to really look at your code now, but this should give you the "return line" if you pass it the line number, meaning in this context that it gives the line number that you want to be in the left margin.

Code:
Private Function GetReturnLine(rtb As RichTextBox, Line As Long) As Long Dim lpos As Long Dim s As String Dim i As Long Dim count As Long lpos = SendMessage(rtb.hwnd, EM_LINEINDEX, Line, 0) s = rtb.Text For i = 1 To lpos If Mid$(s, i, 1) = vbCr Then count = count + 1 End If Next i GetReturnLine = count + 1 End Function
Edit: The declaration for the constant is
Const EM_LINEINDEX = &HBB

Last edited by Deadalus; 03-27-2003 at 08:39 PM.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
word wrap with line numbering sidhighwind General 0 12-30-2002 10:21 AM
word wrap TheTez General 7 10-17-2002 03:02 PM
Transfer private controls to another project.. Help! Jazler General 8 07-01-2002 09:59 AM
Selection line numbering TrentG Word, PowerPoint, Outlook, and Other Office Products 3 06-03-2002 07:48 AM
Word - Empty line deletion Tommy Gun Word, PowerPoint, Outlook, and Other Office Products 2 04-22-2002 08:07 PM

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
 
 
-->