Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I tell where next character will be when overriding WndProc

Status
Not open for further replies.

SBendBuckeye

Programmer
May 22, 2002
2,166
US
Hello all,

I'm overriding WndProc to do some data validation before a given key press is accepted in a TextBox. Nothing fancy, just the standard stuff and doing a Return if the validation fails so that the key press is never handled by MyBase.WndProc and is thus discarded.

Everything is working properly. How do I know where the current key press will be inserted in the Text property. When entering data, I can click or use the arrow keys to move my insertion point anywhere in the TextBox. Any ideas and/or suggestions would certainly be appreciated!

Have a great day!

j2consulting@yahoo.com
 
Hello all,

Here is what I found (at least for TextBoxes):

A. Me.Text is NOT updated until after MyBase.WndProc
B. Me.SelectionStart is where the Insertion Pointer is
C. Since I am only concerned with numeric data the following code will create a preTest string for validation:
Code:
Select Case m.Msg
  Case WM_CHAR
    With m.WParam
      If .ToInt32 >= Keys.D0 And .ToInt32 <= Keys.D9 Then
        Dim newChar As String = (.ToInt32 - Keys.D0).ToString
        Dim temp As String = Me.Text.Substring(0, Me.SelectionStart) & _
          newChar & Me.Text.Substring(Me.SelectionStart)
        'Return discards character as MyBase.WndProc is never called
        If Not IsValid(temp) Then Return
      End If
    End With
End Select
MyBase.WndProc(m)
D. In the above code, temp will match Me.Text if the character is validated and processed by MyBase.WndProc
E. Odd behavior - If you put a msgbox after defining temp and before calling IsValid, data entry behavior is changed.

Have a great day!

j2consulting@yahoo.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top