Article ID: 141073
Article Last Modified on 1/8/2003
72677 : How to Limit User Input in VB Combo Box with SendMessage API
Control Control Name Height Left Top Width ------------------------------------------------------------ Label aGetLine 360 120 Label aGetLineCount 360 480 Label aGetSel 360 840 Label aLineFromChar 360 1200 Label aLineIndex 360 1560 Label aLineLength 360 1920 Label aReplaceSel 360 2280 Command Command1 375 360 2640 1815 Text Text1 1815 2640 480 3495 Text Text2 375 2520 2640 3615
Private Declare Function GetFocus% Lib "user" ()
' Enter the following Declare as one, single line:
Private Declare Function SendMessage& Lib "user"(ByVal hWnd%, ByVal
wMsg%, ByVal wParam%, ByVal lParam As Any)
' lParam is actually a double word, or long, but declaring
' lParam "As Any" allows flexibility for certain cases of
' using SendMessage.
Private Sub Form_Load ()
Show
X% = fReplaceSel("") '* Used to display the correct text.
End Sub
Private Sub Text1_KeyUp (KeyCode As Integer, Shift As Integer)
'* Update the text control information whenever the key
'* is pressed and released.
CharPos& = fGetSel()
LineNumber& = fLineFromChar(CharPos&)
X% = fGetLine(LineNumber&)
X% = fGetLineCount()
X% = fLineIndex(LineNumber&)
X% = fLineLength(CharPos&)
End Sub
Private Sub Command1_Click ()
'* This routine will insert a line of text at the current location
'* of the caret.
D$ = Text2.text
CharPos& = fGetSel()
X% = fReplaceSel(D$)
X% = fSetSel(CharPos&)
'* Text has been inserted at the caret location. No update the
'* text controls information.
Call Text1_KeyUp(0, 0)
Text1.SetFocus
End Sub
Private Function fGetLineCount& ()
'* This function return the number of lines in the edit control.
Const EM_GETLINECOUNT = &H400 + 10
Text1.SetFocus
' In versions 2.0 3.00, you need to use a long integer to avoid
' a bad DLL calling convention error message. As an alternative,
' you can use the new HWND property instead of GetFocus():
Pos& = SendMessage(GetFocus(), EM_GETLINECOUNT, 0&, 0&)
' Use the following Pos& if you have Visual Basic version 1.0:
' Pos& = SendMessage(GetFocus(), EM_GETLINECOUNT, 0%, 0%)
aGetLineCount.Caption = "GetLineCount = " + Str$(Pos&)
fGetLineCount = Pos&
End Function
Private Function fGetLine (LineNumber As Long)
'* This function copies a line of text specified by LineNumber
'* from the edit control. The first line starts at zero.
Const MAX_CHAR_PER_LINE = 80
Const EM_GETLINE = &H400 + 20
byteLo% = MAX_CHAR_PER_LINE And (255) '[changed 3/25/92]
byteHi% = Int(MAX_CHAR_PER_LINE / 256) '[changed 3/25/92, 2 lines:]
Buffer$ = chr$(byteLo%) + chr$(byteHi%) + Space$(MAX_CHAR_PER_LINE-2)
Text1.SetFocus
Pos& = SendMessage(GetFocus(), EM_GETLINE, CINT(LineNumber), Buffer$)
aGetLine.Caption = "GetLine = " + Buffer$
fGetLine = Pos&
End Function
Private Function fGetSel& ()
'* This function returns the starting/ending position of the
'* current selected text. This is the current location of the
'* cursor if start is equal to ending.
'* LOWORD-start position of selected text.
'* HIWORD-first no selected text.
Const EM_GETSEL = &H400 + 0
Text1.SetFocus
location& = SendMessage(GetFocus(), EM_GETSEL, 0, 0&)
ending% = location& \ &H10000
starting% = location& And &H7FFF
aGetSel.Caption = "Caret Location = " + Str$(starting%)
fGetSel = location& mod 65536
End Function
Private Function fLineFromChar& (CharPos&)
'* This function will return the line number of the line that
'* contains the character whose location(index) specified in the
'* third argument of SendMessage. If the third argument is -1,
'* then the number of the line that contains the first character
'* of the selected text is returned. If start = end from GetSel,
'* then the current caret location is used. Line numbers start
'* at zero.
Const EM_LINEFROMCHAR = &H400 + 25
Text1.SetFocus
Pos& = SendMessage(GetFocus(), EM_LINEFROMCHAR, CINT(CharPos&), 0&)
aLineFromChar.Caption = "Current Line = " + Str$(Pos&)
fLineFromChar = Pos&
End Function
Private Function fLineIndex (LineNumber As Long)
'* This function will return the number of bytes that
'* precede the given line. The returned number reflects the CR/LF
'* after each line. The third argument to SendMessage specifies
'* the line number, where the first line number is zero. If the
'* third argument to SendMessage is -1, then the current line
'* number is used.
Const EM_LINEINDEX = &H400 + 11
Text1.SetFocus
Pos& = SendMessage(GetFocus(), EM_LINEINDEX, CINT(LineNumber), 0&)
aLineIndex.Caption = "#Char's before line = " + Str$(Pos&)
fLineIndex = Pos&
End Function
Private Function fLineLength& (CharPos As Long)
'* This function will return the length of a line in the edit
'* control. CharPos specifies the index of the character that
'* is part of the line that you would like to find the length. If
'* this argument is -1, the current selected character is used as
'* the index.
Const EM_LINELENGTH = &H400 + 17
Text1.SetFocus
Pos& = SendMessage(GetFocus(), EM_LINELENGTH, CINT(CharPos), 0&)
aLineLength.Caption = "LineLength = " + Str$(Pos&)
fLineLength = Pos&
End Function
Private Function fSetSel& (Pos&)
'* This function selects all characters in the current text that
'* are within the starting and ending positions given by
'* Location. The low word is the starting position and the high
'* word is the ending position. If you set start to end, this
'* can be used to position the cursor within the edit control.
Const EM_SETSEL = &H400 + 1
location& = Pos& * 2 ^ 16 + Pos&
Text1.SetFocus
X% = SendMessage(GetFocus(), EM_SETSEL, 0%, location&)
fSetSel = Pos&
End Function
Private Function fReplaceSel (Buffer$)
'* This function will replace the current selected text with the
'* new text specified in Buffer$. You must call SendMessage with
'* the EM_GETSEL constant to select text.
Const EM_REPLACESEL = &H400 + 18
Text1.SetFocus
Pos& = SendMessage(GetFocus(), EM_REPLACESEL, 0%, Buffer$)
aReplaceSel.Caption = "String inserted = " + Buffer$
fReplaceSel = Pos&
End Function
Additional query words: 1.00 2.00 3.00 4.00 docerr vb4win vb416
Keywords: kbhowto KB141073