?
Solved

current line and column in textbox

Posted on 2003-03-26
14
Medium Priority
?
335 Views
Last Modified: 2012-06-21
In my form, I have a multi-line textbox. I also have a status bar at the bottom. In the status bar, I want to display the current line and column that the blinking cursor is at in the textbox. I was trying to use txtTextBox.SelStart to find the location of the blinking cursor, but that just gives you the character number for the whole textbox. Anyone know how to do something like this?
0
Comment
Question by:vitanza
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 11

Expert Comment

by:supunr
ID: 8214039
how about using a richtextbox instead.  Add "Microsoft Rich Texbox Control 6.0"

The richtextbox has a property called "GetLineFromChar" which you could use to get the line number.

e.g.

Private Sub RichTextBox1_Change()
    Dim RowNum as long
    Dim ColNum as long
    Dim i as long

    with RichTextbox1
        RowNum = .GetLineFromChar(.SelStart)

        for i = .SelStart to 1 Step -1
            if (.GetLineFromChar(i) < RowNum) then
               exit for
            end if
        next i
        ColNum = .SelStart - i
    end with
 
End Sub

' To get the row number in a textbox, it is bit of work. let me give you example for that as well....

Good Luck!
0
 
LVL 11

Expert Comment

by:supunr
ID: 8214078
Private Sub Text1_Change()
   Dim RowNum as long
   Dim ColNum as long
   Dim i as long
   Dim PrevCol as long

   with Text1
       RowNum = 0
       i = InStrRev(.Text, vbcrlf, .SelStart)
       PrevCol = i
       do while i > 0
           RowNum = RowNum + 1
           i = InStrRev(.Text, vbcrlf, i - 1)
       loop

       ColNum = .SelStart - PrevCol
   end with

End Sub
0
 
LVL 11

Expert Comment

by:supunr
ID: 8214150
actually correct the last code to...

Private Sub Text1_Change()
  Dim RowNum As Long
  Dim ColNum As Long
  Dim i As Long
  Dim PrevCol As Long

  With Text1
      RowNum = 0
      If (.SelStart <= 1) Then
        i = InStrRev(.Text, vbCrLf, 1)
      Else
        i = InStrRev(.Text, vbCrLf, .SelStart - 1)
      End If
      PrevCol = i
      Do While i > 0
          RowNum = RowNum + 1
          i = InStrRev(.Text, vbCrLf, i - 1)
      Loop

      ColNum = .SelStart - PrevCol
  End With
  Me.RichTextBox1.Text = RowNum & " : " & ColNum

End Sub
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Accepted Solution

by:
emadat earned 150 total points
ID: 8214454
Declare the following:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const EM_LINEFROMCHAR = &HC9


and use this function:

Public Function GetLineNumber(txt As TextBox) As Long
    GetLineNumber = SendMessage(txt.hwnd, EM_LINEFROMCHAR, txt.SelStart, 0&)
End Function


It will return the line number (zero based)

Regards
0
 
LVL 3

Expert Comment

by:emadat
ID: 8214501
The other function:

Public Function GetCharNumber(txt As TextBox) As Long
Dim CharPos%
    CharPos = txt.SelStart - InStrRev(txt, vbCrLf, txt.SelStart)
End Function

Please note that the cursor have to be in the text box prior to calling the function or you will get an error

You can add error handling into both the functions.

Regards
0
 
LVL 3

Expert Comment

by:emadat
ID: 8214509
Sorry here is the correct function

Public Function GetCharNumber(txt As TextBox) As Long
   GetCharNumber = txt.SelStart - InStrRev(txt, vbCrLf, txt.SelStart)
End Function
0
 

Author Comment

by:vitanza
ID: 8215658
OK, I right-clicked on the Toolbox and clicked Components, but the "Microsoft Rich Textbox Control 6.0" is not in my list of controls. For some reason, I remember it being there before, though... Any clue as to why it's not listed or what DLL it's in?

Also, emadat, those functions work, but not really. Here is my code: (txtFile is my multi-line textbox)

Private Sub txtFile_KeyDown(KeyCode As Integer, Shift As Integer)
    Dim lineNum, charNum
    lineNum = GetLineNumber()
    charNum = GetCharNumber()
    StatusBar1.Panels.Item(1).Text = "Line " & lineNum & ", Char " & charNum
End Sub

I have the same code in the txtFile_Click() procedure. The status bar usually displays correctly, but sometimes it doesn't display right away; sometimes you have to press an arrow key twice for the status bar text to change.
0
 
LVL 3

Expert Comment

by:emadat
ID: 8218264
The call to each function have to take the text box name as a parameter. If your textbix name is MyTextBox then your function should look like:

Private Sub txtFile_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim lineNum, charNum
   lineNum = GetLineNumber(MyTextBox)
   charNum = GetCharNumber(MyTextBox)
   StatusBar1.Panels.Item(1).Text = "Line " & lineNum & ", Char " & charNum
End Sub

However; as I mentioned; the cursor have to be in the text box. so try this function:

Private Sub txtFile_KeyDown(KeyCode As Integer, Shift As Integer)
Dim lineNum, charNum
   MyTextBox.SetFocus
   lineNum = GetLineNumber(MyTextBox)
   charNum = GetCharNumber(MyTextBox)
   StatusBar1.Panels.Item(1).Text = "Line " & lineNum & ", Char " & charNum
End Sub


Regards
0
 

Author Comment

by:vitanza
ID: 8219811
I forgot to mention that I modified the functions GetLineNumber and GetCharNumber so that it uses txtFile automatically:

Public Function GetLineNumber() As Long
    On Error Resume Next
    GetLineNumber = SendMessage(txtFile.hwnd, EM_LINEFROMCHAR, txtFile.SelStart, 0&)
End Function

Public Function GetCharNumber() As Long
    On Error Resume Next
    GetCharNumber = txtFile.SelStart - InStrRev(txtFile, vbCrLf, txtFile.SelStart)
End Function
0
 
LVL 3

Expert Comment

by:emadat
ID: 8219944
What is the problem with this code?
0
 

Author Comment

by:vitanza
ID: 8220371
Private Sub txtFile_KeyDown(KeyCode As Integer, Shift As Integer)
   Dim lineNum, charNum
   lineNum = GetLineNumber()
   charNum = GetCharNumber()
   StatusBar1.Panels.Item(1).Text = "Line " & lineNum & ", Char " & charNum
End Sub

I have the same code in the txtFile_Click() procedure. The status bar sometimes displays correctly, but sometimes it doesn't display right away; sometimes you have to press an arrow key twice for the status bar text to change. Also, it seems like it's always one character off.
0
 
LVL 3

Expert Comment

by:emadat
ID: 8220444
If it is ALWAYS one character away; then add 1 to the result of the function.

For the other problem; issue a refresh after updating the statusbar
0
 

Author Comment

by:vitanza
ID: 8220703
Thanks, but I want to capture the < and > keys. How should I do this? Should I use MyControl_KeyPress() instead?
0
 

Author Comment

by:vitanza
ID: 8220710
Sorry, that comment was supposed to be for a different question :)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month11 days, 5 hours left to enroll

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question