Hi,

I am writing a small function that will calculate the contents of a TextBox after the KeyPress event, but during the KeyPress event. I know, it sounds strange.

The reason for this is because I have been given a project at work, where I am to fix any errors in a current program.

There is currently a TextBox, which triggers a search on a SQL Server, in the KeyPress event.
This searches for the current contents of the TextBox concatenated with Chr(KeyAscii)

This means that if your TextBox contains Hello and your cursor is at the end of the word it will work correctly, but if you have the o selected and press a it will search for Helloa instead of Hella.

So to correct, this I have come up with the following function
Code:
Private Function TextAfterKeyPress(Ctrl As Control, KeyAscii As Integer) As String
	Dim strUnSelLeft As String
	Dim strUnSelRight As String
	Dim strSel As String
	Dim strMid As String
	
	With Ctrl
		strUnSelLeft = ""
		strUnSelRight = ""
		strMid = .Text
		
		Select Case KeyAscii
			Case 1                                           ' Ctrl + A
				' No change to text
			Case 3                                           ' Ctrl + C
				' No change to text
			Case 8                                           ' BackSpace
				If .SelStart = 0 Then
					' No change to text
				Else
					If .SelLength = 0 Then
						strUnSelLeft = Left(.Text, .SelStart - 1)
						strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
						strMid = ""
					Else
						strUnSelLeft = Left(.Text, .SelStart)
						strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
						strMid = ""
					End If
				End If
			Case 9                                           ' Tab
				' No change to text
			Case 13                                          ' Return
				' No change to text
			Case 22                                          ' Ctrl + V
				strUnSelLeft = Left(.Text, .SelStart)
				strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
				strMid = Clipboard.GetText
			Case 24                                          ' Ctrl + X
				If .SelLength = 0 Then
					' No change to text
				Else
					strUnSelLeft = Left(.Text, .SelStart)
					strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
					strMid = ""
				End If
			Case 26                                          ' Ctrl + Z
			
			
			
			
			
			Case 27                                          ' Esc
				' No Change to text
			Case 137, 153, 160, 169, 188, 189, 190, 215, 247 ' Disallowed Chars
				' No Change to text
			Case 128 To 255                                  ' Allowed non standard Chars
				strUnSelLeft = Left(.Text, .SelStart)
				strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
				strMid = Chr(KeyAscii)
			Case 32 To 127                                   ' Standard Printable Chars
				strUnSelLeft = Left(.Text, .SelStart)
				strUnSelRight = Right(.Text, Len(.Text) - (.SelStart + .SelLength))
				strMid = Chr(KeyAscii)
			Case Else
		End Select
		
		TextAfterKeyPress = strUnSelLeft & strMid & strUnSelRight
	End With
End Function
Now for the section where it says "Case 26", it will perform an undo action, and then the search won't work correctly again.

Is there any way to find out what the contents of the TextBox will be when you press Ctrl + Z, but during the KeyPress in which it happens? The KeyPress event fires before the content of the TextBox changes so I would need to find out what is in the Undo buffer so that I can run a correct search.

Can anyone point me in the right direction?