|
-
Nov 7th, 2002, 05:03 AM
#1
Thread Starter
Addicted Member
Trapping cut-n-paste
I have a textbox and a command button. I need to disable the command button in case the user changes the content of the textbox. For some other restrictions, I cannot use the textbox_change event. I was doing this with textbox_keydown and thought everything was working perfect before I discovered yesterday that if user cuts or pastes the content with mouse right click, my command button is not disabled. Is there any way to trap cut 'n' paste in a textbox?
Thanks in advance.
-
Nov 7th, 2002, 05:06 AM
#2
Frenzied Member
U could also Trap Mouse movements or subclass the textbox and capture a "paste" windows message.
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:07 AM
#3
Hyperactive Member
A couple of things...
Is the Text Box not supposed to be changed at all, except via the program? If so then either lock it or disable the text box.
If it is supposed to be changed, then use the validate to run tests to ensure the information is correct before effecting your command button.
-
Nov 7th, 2002, 05:08 AM
#4
Thread Starter
Addicted Member
Well
-
Nov 7th, 2002, 05:08 AM
#5
Hyperactive Member
Ok,
To restrict the user to paste something in textbox, clear the clipboard in the mouse over event of the textbox
VB Code:
Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Clipboard.Clear
End Sub
Gary
-
Nov 7th, 2002, 05:11 AM
#6
Thread Starter
Addicted Member
No
No no.
I don't want to unable the user to paste in the textbox. What I want is to disable the command button. Which will be enabled only after another command button is pressed.
-
Nov 7th, 2002, 05:12 AM
#7
Frenzied Member
Sorry I misread. I thought that **all** events of the texbox cannot be used
VB Code:
Private Sub Form_Load()
Text1.Text = "Hello World"
End Sub
Private Sub Text1_LostFocus()
If Text1.Text <> "Hello World" Then
Command1.Enabled = False
End Sub
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:12 AM
#8
Hyperactive Member
The method you adopt is strongly dictated by what you are trying to acheive. To me it seems that you want the text to remain static, and hence you should make means to prevent any change at all.
Other than that, I think you should be using validation to test whether entires made are correct or not... I see no reason why you want to restrict pastes, unless I have utterly missed the point of what you're trying to acheive.
-
Nov 7th, 2002, 05:16 AM
#9
Thread Starter
Addicted Member
Oops! Not so easy!
Thanks KayJay, but this is not that simple.
Suppose user entered 1 in the textbox and clicked the Save button to write the value in a file. Then if he changes it to 2, I want to disable the Next button, which will be enabled only after he press the Save button again, so that he cannot move forward to the next form without saving the new value to the file.
-
Nov 7th, 2002, 05:18 AM
#10
Frenzied Member
ok.....
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:18 AM
#11
Thread Starter
Addicted Member
Originally posted by LucaUWF
The method you adopt is strongly dictated by what you are trying to acheive. To me it seems that you want the text to remain static, and hence you should make means to prevent any change at all.
Other than that, I think you should be using validation to test whether entires made are correct or not... I see no reason why you want to restrict pastes, unless I have utterly missed the point of what you're trying to acheive.
Please read my posts. I don't want to disable pasting in the textbox. You just got the opposite of what I want. My purpose is completely different and I am using validation event where required.
-
Nov 7th, 2002, 05:22 AM
#12
Hyperactive Member
Ok...
When the text box is changed take a copy of the text at the end of the validation routine (This will require initialisation or a conditional for the first change).
Now, everytime the text is changed after that, compare the Old Value (The copy) with the new value, if they don't match, i.e. there has been a change, disable the next button, and then update the copy... the cycle continues.
Hitting Save will re-enable it.
-
Nov 7th, 2002, 05:23 AM
#13
Thread Starter
Addicted Member
How...
KayJay, please tell me how can I read the cut or paste windows messages?
-
Nov 7th, 2002, 05:25 AM
#14
Frenzied Member
Originally posted by LucaUWF
Ok...
When the text box is changed take a copy of the text at the end of the validation routine (This will require initialisation or a conditional for the first change).
Now, everytime the text is changed after that, compare the Old Value (The copy) with the new value, if they don't match, i.e. there has been a change, disable the next bitton, and then update the copy... the cycle continues.
Hitting Save will re-enable it.
Tried that..
VB Code:
Private tmpString As String
Private Sub Command2_Click()
Command1.Enabled = True 'Next Button
End Sub
Private Sub Form_Load()
Text1.Text = "Hello World"
End Sub
Private Sub Text1_GotFocus()
Command1.Enabled = False
tmpString = Text1.Text
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
If Text1.Text <> tmpString Then
Command1.Enabled = False 'Next Button
Else
Command1.Enabled = True
End If
End Sub
Can't get out of the text box
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:26 AM
#15
Thread Starter
Addicted Member
Aaaaaaaah! here is something
Originally posted by LucaUWF
Ok...
When the text box is changed take a copy of the text at the end of the validation routine (This will require initialisation or a conditional for the first change).
Now, everytime the text is changed after that, compare the Old Value (The copy) with the new value, if they don't match, i.e. there has been a change, disable the next button, and then update the copy... the cycle continues.
Hitting Save will re-enable it.
Thanks!
This seems to be a good idea to me. Ok, I will try for this. But I really do have more than 100 textboxes in my whole project. So it is a great load for me. Anyway, don't worry.
-
Nov 7th, 2002, 05:32 AM
#16
Frenzied Member
Ok to UR PM............
Two minutes..........
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:32 AM
#17
Hyperactive Member
The validation shouldn't re-enable the button, else a second successive validation will be true and re-enable the button.
Private Sub Command1_Click()
cmdNext.Enabled = True
End Sub
Private Sub Text1_GotFocus()
ThisStr = Text1.Text
End Sub
Private Sub Text1_Validate(Cancel As Boolean)
If Text1.Text <> ThisStr Then
cmdNext.Enabled = False
End If
ThisStr = Text1.Text
End Sub
Works fine.
-
Nov 7th, 2002, 05:35 AM
#18
Frenzied Member
i understand that, but as there is no validate event firing unless the textbox loses focus, when will it be fired with only two buttons and a textbox on the form?
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:37 AM
#19
Hyperactive Member

When they try to hit the button. 
Also, for many text boxes, just use a function to pass the parameters via the Validation and GotFocus events.
-
Nov 7th, 2002, 05:38 AM
#20
Thread Starter
Addicted Member
Hi hi...
May be, when you are going to press the Next button, seeing it enabled. But after clicking, you will find that it has benn disabled (you may also think that it became disabled because you clicked it).
-
Nov 7th, 2002, 05:40 AM
#21
Hyperactive Member
Jeez, what do you guys want. In any case, you should provide a useful message telling them that they must save the text first.
Of course you could argue as to why you don't just automatically save it for them and cut all of this out? Or at least provide a message box asking if they want to save it, if they don't disable next until they are ready to, and if they do want to save you can leave the Next button enable.
I think you need to give it some more thought.
-
Nov 7th, 2002, 05:40 AM
#22
Frenzied Member
To capture a "Paste" windows message. Make sure U close the form using only the X button!
VB Code:
'In a Form
Private Sub cmdSave_Click()
cmdNext.Enabled = True
End Sub
Private Sub Form_Load()
Hook Text1
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook Text1
End Sub
'In a module
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Const WM_PASTE As Long = &H302
Const GWL_WNDPROC = (-4)
Dim PrevProc As Long
Public Sub Hook(T As TextBox)
PrevProc = SetWindowLong(T.hwnd, GWL_WNDPROC, AddressOf WindowProc)
End Sub
Public Sub UnHook(T As TextBox)
SetWindowLong T.hwnd, GWL_WNDPROC, PrevProc
End Sub
Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
If uMsg = WM_PASTE Then
Form1.cmdNext.Enabled = False
End If
End Function
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
-
Nov 7th, 2002, 05:47 AM
#23
Thread Starter
Addicted Member
Originally posted by LucaUWF
Jeez, what do you guys want. In any case, you should provide a useful message telling them that they must save the text first.
Of course you could argue as to why you don't just automatically save it for them and cut all of this out? Or at least provide a message box asking if they want to save it, if they don't disable next until they are ready to, and if they do want to save you can leave the Next button enable.
I think you need to give it some more thought.
Yes, I did what you are telling for the Cancel button. If the user does not want to save the changed values, he will press the cancel, not Next. Purpose of Next is a bit different and after lot of thoughts I asked you how could I trap a mouse right click cut or paste event so that I can disable the Next button which specifically fits my need.
Thanks for all your replies and time. I think now I can make out something out of this thread.
-
Nov 7th, 2002, 05:49 AM
#24
Frenzied Member
Originally posted by arnabbandyo
..............Thanks for all your replies and time. I think now I can make out something out of this thread.
Let us know once U have
"Brothers, you asked for it."
...Francisco Domingo Carlos Andres Sebastian D'Anconia
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
|