Re: column count in VB6

From: Rick Rothstein (rickNOSPAMnews_at_NOSPAMcomcast.net)
Date: 11/24/04


Date: Wed, 24 Nov 2004 11:05:46 -0500


> Not sure If I'll address the question correctly, but
.getcurrentcolumnnumber
> is supposed to return me the current, I guess current Character count
on that
> line.
> I am using rich text box, but I guess it's referred to as
"uctmyrichtextbox".
>
> I really don't care what kinds of text box i'm using, all I care for
is the
> ability to have it give me the current character count that my curser
is at.
> The .getcurrentcolumnnumber does that, but stops after I go off the
left side
> of the page.

>From a previous post of mine (which I think addresses your question)...

Rick - MVP

The following, written for a normal TextBox, will also works with a
RichTextBox if the references are changed appropriately (TextBox to
RichTextBox).

Paste this code in the (General)(Declarations) section of your Form for
single form use; or place it into a BAS Module (Project/AddModule from
VB menu bar) for project wide scope:

Private Declare Function SendMessageLong Lib _
    "user32" Alias "SendMessageA" _
    (ByVal hwnd As Long, _
     ByVal wMsg As Long, _
     ByVal wParam As Long, _
     ByVal lParam As Long) As Long

Const EM_LINEFROMCHAR = &HC9
Const EM_LINEINDEX = &HBB

Function CursorLine(TextBoxIn As TextBox) As Long
    CursorLine = SendMessageLong(TextBoxIn.hwnd, _
                        EM_LINEFROMCHAR, -1, 0) + 1
End Function

Function CursorColumn(TextBoxIn As TextBox) As Integer
  CursorColumn = TextBoxIn.SelStart - SendMessageLong( _
                           TextBoxIn.hwnd, EM_LINEINDEX, -1, 0)
End Function

You only asked for the cursor line, but I've included a function that
also gives the column that the cursor is in. The CursorLine function
returns the line number counting the first line as Line #1. The
CursorColumn function returns the number of characters that exist to the
left of the cursor. Simply pass the TextBox in as an argument to either
function; for example

Private Sub Text1_Click()
  MsgBox "The cursor is on Line #" & CursorLine(Text1) & _
               " at Column #" & CursorColumn(Text1)
End Sub



Relevant Pages

  • RE: Setting Cursor Location in Textbox
    ... I thought about using a separate textbox, but I wanted to be able to ... > record's primary key with what had been typed in. ... > cursor showing what had been "searched" would be better. ... >> character, the search will execute and present the new result. ...
    (microsoft.public.access.formscoding)
  • Re: column count in VB6
    ... "Rick Rothstein" wrote: ... >> is supposed to return me the current, I guess current Character count ... > RichTextBox if the references are changed appropriately (TextBox to ... > also gives the column that the cursor is in. ...
    (microsoft.public.vb.general.discussion)
  • Setting Cursor Location in Textbox
    ... I wish to use a textbox to search for a record, i.e., when the user ... This will include the records's primary key with the cursor set after the key ... After each character is typed, ...
    (microsoft.public.access.formscoding)
  • RE: Setting Cursor Location in Textbox
    ... First keystroke of 1 that finds a record with the following primary key ... the I would be the cursor. ... >> offset in a textbox? ... After each character is typed, ...
    (microsoft.public.access.formscoding)
  • Re: How to avoid double entries inside a text box?
    ... I need also to append characters when the textbox is not ... Form_KeyDown event plus respond to Backspace, ... I have not double but triple repeat of the same character! ... Private Sub Form_KeyDown ...
    (microsoft.public.vb.general.discussion)