-
Jan 9th, 2015, 08:01 AM
#1
Thread Starter
Junior Member
How do I find what the contents of a textbox will be after undo, before I undo it?
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?
-
Jan 9th, 2015, 08:24 AM
#2
Re: How do I find what the contents of a textbox will be after undo, before I undo it
This means that if your TextBox contains Hello ... if you have the o selected and press a it will search for Helloa instead of Hella.
Woah... so you're telling me that if I enter Hello into the text box, it works fine. If I then highlight the "o" and then press "a"... you end up with "Helloa" ??? ahh.... that shouldn't be happening. I think the reason it is is due to your handling of things above... some of which override what Windows will do for you naturally... where are you calling that from? And why do you have all those conditions? Sounds like this is a case of over-engineering.
-tg
-
Jan 9th, 2015, 08:43 AM
#3
Thread Starter
Junior Member
Re: How do I find what the contents of a textbox will be after undo, before I undo it
Sorry, I haven't made myself very clear.
The textbox is correctly being populated when you type, but on each keypress, it does a search against our database for a customer with a name that starts with entered text
So enter "Mic" and it will find any customers beginning with "Mic" and display the details for the first one in the list.
But, it is currently programmed so that if you have "Microsoft" typed in the box already, then highlight the "Micro" part of the word and press "A" it would search for "A" instead of "Asoft" even though "Asoft" would be showing in the textbox.
This all occurs during the KeyPress event, so it attempts to calculate what the text would be after the KeyPress event to do this.
Currently it is coded
Code:
If txtCustomer.SelLength = 0 Then
FindString = txtCustomer.text & Chr$(KeyAscii)
Else
FindString = left$(txtCustomer.text, txtCustomer.SelStart) & Chr$(KeyAscii)
End If
So I'm changing this part of the code to
Code:
FindString = TextAfterKeyPress(txtCustomer, KeyAscii)
I could re-write the whole of the KeyPress event, but it is rather large and may take a while
The reason why I have written a function to deal with this is because I'm fairly certain that there is similar code in other areas of the program.
-
Jan 9th, 2015, 08:46 AM
#4
Re: How do I find what the contents of a textbox will be after undo, before I undo it
Maybe the logic/actions should not be in the KeyPress/KeyDown events, but rather in the Change event?
-
Jan 9th, 2015, 08:52 AM
#5
Thread Starter
Junior Member
Re: How do I find what the contents of a textbox will be after undo, before I undo it
You are correct, and if I was writing this program from scratch I would do things very differently.
Unfortunately, it is an already existing program that I have been asked to amend and re-writing would take too long.
The reason it is using the KeyPress event is because certain Keys are restricted and they have added the search into the same event.
-
Jan 9th, 2015, 09:02 AM
#6
Re: How do I find what the contents of a textbox will be after undo, before I undo it
Originally Posted by Ste_Moore01
You are correct, and if I was writing this program from scratch I would do things very differently. Unfortunately, it is an already existing program that I have been asked to amend and re-writing would take too long
Appears that function doesn't use any public or module-level variables. From what I can see (which obviously isn't much), looks like you can literally exit the textbox's KeyPress event as soon as the KeyAscii is processed for contrent, but before searching. Then copy whatever SQL search-related stuff exists into the Change event and use the textbox current content for the search. Or write a private method with the search code & in the textbox change event, call that search function and pass the current textbox content.
The reason it is using the KeyPress event is because certain Keys are restricted and they have added the search into the same event.
That logic can remain. If the textbox content doesn't change, then the Change event won't fire
-
Jan 9th, 2015, 09:07 AM
#7
Thread Starter
Junior Member
Re: How do I find what the contents of a textbox will be after undo, before I undo it
I appreciate the idea, but is there any way to do what I originally asked?
Is there a way to find out what the content would be after I pressed Ctrl + Z before I pressed it?
-
Jan 9th, 2015, 09:16 AM
#8
Re: How do I find what the contents of a textbox will be after undo, before I undo it
Originally Posted by Ste_Moore01
I appreciate the idea, but is there any way to do what I originally asked?
Honestly don't know. From the list of edit control messages, doesn't appear so without looking at the text afterwards. Ctrl+Z isn't even guaranteed to do anything if nothing was modified yet, but whether there is an active undo buffer can be checked with EM_CANUNDO. Here's a the MSDN page on edit control messages that can be used with the SendMessage API.
-
Jan 9th, 2015, 09:25 AM
#9
Re: How do I find what the contents of a textbox will be after undo, before I undo it
And as a follow-up. I'd say that tweaking the KeyPress event & conducting the search in the Change event will be a far more robust (i.e., less error prone) than what you are currently working with.
Obviously from the code you posted, fake keystrokes are being sent to the textbox for custom-actions, i.e., 1=Ctrl+A, 3=Ctr+C, etc. In the keypress event, I'd assume there is similar code to this psuedo code:
Code:
Case 1: .SelStart=1:.SelLength = Len(.Text)
Case 3: Clipboard.Clear: Clipboard.SetText .SelText
If so, if not already being done, when those commands are received or any keystroke that shouldn't modify the text, set the event's KeyAscii parameter to zero before event exits & revised logic would look something like this
Code:
process key event for custom commands & restrictions only
is the key allowed to modify the text?
if yes, then do nothing else change keyascii parameter to zero before exit (includes resetting custom commands)
... in textbox Change event, conduct the search
Good luck. Fixing OPC (other people's code) isn't always easy, especially, when the logic may have been faulty from the start.
Last edited by LaVolpe; Jan 9th, 2015 at 09:29 AM.
-
Jan 9th, 2015, 09:36 AM
#10
Re: How do I find what the contents of a textbox will be after undo, before I undo it
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)
Sounds like all you really need to do is change the part that concatenates the text and use .SelText instead.
So Instead of
Code:
Text1.Text=Text1.Text & Chr(KeyAscii)
You would have
Code:
Text1.SelText=Chr(KeyAscii)
That way if no character is selected the new character would go whereever the cursor is be it at the end of somewhere in the middle. If there is text highlighted then it would be replaced by the key pressed.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|