Show Number of Lines In Rich Text Box

Like in Visual Studio and Dreamweaver

Page 1 of 1

6 Replies - 14277 Views - Last Post: 01 September 2010 - 05:14 AM Rate Topic: -----

#1 Protego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-August 10

Show Number of Lines In Rich Text Box

Posted 31 August 2010 - 06:01 AM

I have 2 panels set up, one contains the rich text box the other one contains a single label. The am currently using the following code however it is not working. Help would be greatly appreciated.

        Dim line_count As Integer = mainEditor.Lines.Length
        Dim linecounter As Integer = line_count
        linecounter += 1
        line_count = 0
        lineNumbers.Text = ""
        While line_count < linecounter
            line_count += 1
            lineNumbers.Text += line_count + "\n"
        End While


Is This A Good Question/Topic? 0
  • +

Replies To: Show Number of Lines In Rich Text Box

#2 jens  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 67
  • View blog
  • Posts: 430
  • Joined: 09-May 08

Re: Show Number of Lines In Rich Text Box

Posted 31 August 2010 - 06:16 AM

Hi!

Ok, what is the problem? What errors do you get? What do you want to get? It seems to me that you are just looping as many times as there are rows and writing each rownumber followed by a newline...

Regards
Jens

PS: Cleaned and "beautified" (in my opinion) your code a little. Now it does count the rows (one to many) but I don't know if this is what you wanted. Check the code and come back if you don't understand.
PPS: Added a button to the form in order to get the code running. You didn't show where you'd put it so... :)

Option Explicit On
Option Strict On

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim linecounter As Integer = 0
        Dim line_count As Integer = 0

        linecounter = mainEditor.Lines.Length + 1
        lineNumbers.Text = ""

        While line_count < linecounter
            line_count += 1
            lineNumbers.Text += line_count.ToString() + Environment.NewLine()
        End While
    End Sub

End Class


This post has been edited by jens: 31 August 2010 - 07:10 AM

Was This Post Helpful? 0
  • +
  • -

#3 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Show Number of Lines In Rich Text Box

Posted 31 August 2010 - 07:18 AM

Can't you just use
lineNumbers.Text = mainEditor.Lines.Count()

Or am I mis-understanding what you're trying to do?
Was This Post Helpful? 0
  • +
  • -

#4 Protego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-August 10

Re: Show Number of Lines In Rich Text Box

Posted 31 August 2010 - 08:08 AM

I mean like in Dreamweaver and Visual studio where on the left it shows the line numbers.

Edit: Oh wow, thanks, the only problem is that if you have a long string of text and then press enter the line count continues, this might be a bit confusing so I took a screenshot:

http://i34.tinypic.com/1t51tk.jpg

This post has been edited by Protego: 31 August 2010 - 08:15 AM

Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Show Number of Lines In Rich Text Box

Posted 31 August 2010 - 09:05 AM

Could you use bullets and then send Ctrl+Shift+L to switch the bullets to numbers?

Here's an example of what I'm talking about

Create 2 buttons, Button1 and Button2 and set their .Text property to # On and # off

In Button1_Click (# On) add the following:
mainEditor.SelectAll()
mainEditor.Focus()
SendKeys.Send("^+L") 'switch selection to bullets
SendKeys.Send("^+L") 'switch selection to numbers
Button1.Enabled = False


In Button2_Click (# Off) add the following:
mainEditor.SelectionBullet = False
Button1.Enabled = True


See if that gives you something to work with.

This post has been edited by CharlieMay: 31 August 2010 - 09:06 AM

Was This Post Helpful? 0
  • +
  • -

#6 Protego  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 31-August 10

Re: Show Number of Lines In Rich Text Box

Posted 01 September 2010 - 04:52 AM

Ok so this is what I have now:

    Private Sub mainEditor_KeyDown1(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles mainEditor.KeyDown
        Dim prevent As Integer = 0
        While prevent = 0
            mainEditor.SelectAll()
            mainEditor.Focus()
            SendKeys.Send("^+L") 'switch selection to bullets
            SendKeys.Send("^+L") 'switch selection to numbers
            mainEditor.DeselectAll()
            prevent += 1
        End While
    End Sub



But now it just keeps looping and if I remove the delselectall if just freezes.
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Show Number of Lines In Rich Text Box

Posted 01 September 2010 - 05:14 AM

With what you're trying to achieve with prevent. Notice that you are always setting prevent to 0 and then trying to perform the while if prevent = 0. Prevent ALWAYS equals 0 with the way your code is written.

Move the Dim Prevent As Integer = 0 to just below the Public Class <formname> and it should work.

The reason I set it up in my example as I did, was to give you the option to toggle the line numbers on and off. If you always want them on you can just do the run the routine in the form_shown event

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
        mainEditor.SelectAll()
        mainEditor.Focus()
        SendKeys.Send("^+L")
        SendKeys.Send("^+L")
    End Sub


This will start the RTB with line number bullets.

This post has been edited by CharlieMay: 01 September 2010 - 05:22 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1