• Welcome to Valhalla Legends Archive.
 

VB6: what would be the best way to do this

Started by Tontow, May 06, 2004, 11:35 AM

Previous topic - Next topic

Tontow

I have a textbox.
This textbox has text that is being streemed into it. --(several chat whispers a min.)

I need to be able to click(or double click to select) on a word in that textbox and have it return that word that I clicked/selected along with the contens of the line that it is on

IE:
event would be eather a selection of a word OR a click on a word
it would return the word + the contencs of the line it is on

What would be the best way to do this???

Grok

#1
TextBox has a DblClick event.  Put code in that event to get the currently selected text.  If your goal is instead to get the whole line, use SendMessage and a combination of EM_CHARFROMPOS, EM_FINDTEXT, EM_GETTEXTRANGE, and maybe EM_GETLINE.  Oh, and you'll need EM_LINEINDEX.

Tontow

my goal is to get both the currently seleced text and the whole line of text that the selection is on.

umm, whats EM? (is it what you put inplace of the function name inorder to show me?)

Eli_1

No. Those are constants that you pass to SendMessage (bitwise OR?).

Tontow

so im gessing that frmui.Text1.SelText would return selected text? (and yes, im kind of new to VB6, everyone has seen how sloppy my programming is)

Tontow

and i would also help if someone would explan to me what SendMessage , EM_CHARFROMPOS, EM_FINDTEXT, EM_GETTEXTRANGE, EM_GETLINE are; i cant find them in the VB6 online reference.

Eli_1

#6
Quoteand i would also help if someone would explan to me what SendMessage , EM_CHARFROMPOS, EM_FINDTEXT, EM_GETTEXTRANGE, EM_GETLINE are; i cant find them in the VB6 online reference.


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_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1


e.g.

Dim Buffer as String // Store the line here
Dim LineIndex as Long
Dim LineLen as Long

LineIndex = SendMessage(SomeRichTextBox.hwnd, EM_LINEINDEX, 0, 0)
LineLen = SendMessage(SomeRichTextBox.hwnd, EM_LINELENGTH, LineIndex, 0)
Buffer = Space$(LineLen)
SendMessage SomeRichTextBox.hwnd, EM_GETLINE, 0, ByVal Buffer

MsgBox Buffer, vbOkOnly + vbInformation


This should be enough to get you started. If not, may god be with you.

Tontow

ok, thx

just wondering, what are &HC4, &HBA, &HC1 ?

Eli_1

#8
They're just numbers.

&H is hex donation (1 = &H01, 2 = &H02, ..., 10 = &H0A). Same with packet headers. For example, a packet with the header:
Quote
FF 50 5A C9 ...
The packet ID is 0x50 (&H50).

&HC4 (0xC4), &HBA (0xBA), and &HC1 (0xC1) are just the constant values that SendMessage uses to figure out what you want it to do.

Example:
Say your program is a soda machine... (I like soda)  ;D

Private Const GS_COKE As Long = &HC4
Private Const GS_WATER As Long = &HC1
Private Const GS_PEPSI As Long = &HBA

GetSoda GS_PEPSI '// Damn right I want a pepsi...



Public Function GetSoda(ByVal SodaFlags as Long) as String
   If SodaFlags = &HC4 Then
       'User wants a coke (eww)
   ElseIf SodaFlags = &HC1 Then
       'User wants a water (wtf, why's he at a soda machine?)
   ElseIf SodaFlags = &HBA Then
       'User wants a Pepsi (yeah baby...)
   End If
End Function

Those constant numbers are just what the function uses to determine what operation you need done. In this example, I want a god damn Pepsi...

Tontow

umm,
buffer is alwasy returning the frist line
and
LineIndex is alwasy 0
LineLen correctly returns the frist lines length
this is wrong, the textbox is multiline, i need the line that has the selected word

this is in the header (top or whatever its called)

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_GETLINE = &HC4
Private Const EM_GETLINECOUNT = &HBA
Private Const EM_LINEINDEX = &HBB
Private Const EM_LINELENGTH = &HC1


and this is the function (i imply a that the user has selected text with his double click)

Private Sub Text1_DblClick()
Dim user As String
user = frmui.Text1.SelText

Dim Buffer As String
Dim LineIndex As Long
Dim LineLen As Long

LineIndex = SendMessage(frmui.Text1.hwnd, EM_LINEINDEX, 0, 0)
LineLen = SendMessage(frmui.Text1.hwnd, EM_LINELENGTH, LineIndex, 0)
Buffer = Space$(LineLen)
SendMessage frmui.Text1.hwnd, EM_GETLINE, 0, ByVal Buffer

MsgBox Buffer & " l " & LineIndex & " l " & LineLen & " l " & user, vbOKOnly + vbInformation


End Sub


did i mention that i dont know a thing about sendmessage aside from whats in this post?

Eli_1

It's always returning the first line because:
LineIndex = SendMessage(frmui.Text1.hwnd, EM_LINEINDEX, 0, 0)

The number in bold is the line number your getting the index of.

Tontow

im haveing troble comming up with something to find the line on wich the text is selected.
Any help?

Grok

#12
Quote from: Tontow on May 08, 2004, 12:49 AM
im haveing troble comming up with something to find the line on wich the text is selected.
Any help?

Q186271 HOWTO: Manipulate Text Box Contents

The following messages are used:

EM_LINEFROMCHAR (&HC9)-returns the line number of the line where the cursor is positioned. Pass the cursor position with this message.


EM_LINELENGTH (&HC1)-returns the length of the line where the cursor is positioned. Pass the cursor position with this message.


EM_LINEINDEX (&HBB)-returns the number of characters in all the lines previous to the cursor position. Pass the results of the EM_LINEFROMCHAR message. Each line includes a carriage return and a line feed character.


EM_GETLINECOUNT (&HBA)-returns the total number of lines in a multi-line text box.


EM_GETLINE (&HC4)-returns the text of the line where the cursor is positioned. Pass the results of the EM_LINEFROMCHAR message.

Tontow

thx, that was one of the bigger helps in this thred