Results 1 to 24 of 24

Thread: Trapping cut-n-paste

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Question 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.
    I am new to VB...

  2. #2
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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

  3. #3
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Question Well

    But how????
    I am new to VB...

  5. #5
    Hyperactive Member CHAMPGARY's Avatar
    Join Date
    Jul 2002
    Posts
    386
    Ok,
    To restrict the user to paste something in textbox, clear the clipboard in the mouse over event of the textbox

    VB Code:
    1. Private Sub Text1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
    2. Clipboard.Clear
    3. End Sub

    Gary

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Cool 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.
    I am new to VB...

  7. #7
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Sorry I misread. I thought that **all** events of the texbox cannot be used
    VB Code:
    1. Private Sub Form_Load()
    2. Text1.Text = "Hello World"
    3. End Sub
    4.  
    5. Private Sub Text1_LostFocus()
    6. If Text1.Text <> "Hello World" Then
    7.     Command1.Enabled = False
    8. End Sub

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  8. #8
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Red face 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.
    I am new to VB...

  10. #10
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    ok.....

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Cool

    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.
    I am new to VB...

  12. #12
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Question How...

    KayJay, please tell me how can I read the cut or paste windows messages?
    I am new to VB...

  14. #14
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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:
    1. Private tmpString As String
    2.  
    3. Private Sub Command2_Click()
    4.     Command1.Enabled = True 'Next Button
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8. Text1.Text = "Hello World"
    9. End Sub
    10.  
    11. Private Sub Text1_GotFocus()
    12. Command1.Enabled = False
    13. tmpString = Text1.Text
    14. End Sub
    15.  
    16.  
    17. Private Sub Text1_Validate(Cancel As Boolean)
    18. If Text1.Text <> tmpString Then
    19.     Command1.Enabled = False 'Next Button
    20. Else
    21.     Command1.Enabled = True
    22. End If
    23. End Sub

    Can't get out of the text box

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Smile 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.
    I am new to VB...

  16. #16
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    Ok to UR PM............

    Two minutes..........

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  17. #17
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  18. #18
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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

  19. #19
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258


    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.

  20. #20

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131

    Talking 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).
    I am new to VB...

  21. #21
    Hyperactive Member
    Join Date
    Sep 2002
    Posts
    258
    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.

  22. #22
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    To capture a "Paste" windows message. Make sure U close the form using only the X button!
    VB Code:
    1. 'In a Form
    2. Private Sub cmdSave_Click()
    3. cmdNext.Enabled = True
    4. End Sub
    5.  
    6. Private Sub Form_Load()
    7.     Hook Text1
    8. End Sub
    9. Private Sub Form_Unload(Cancel As Integer)
    10.     UnHook Text1
    11. End Sub
    12.  
    13. 'In a module
    14. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    15. 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
    16. Const WM_PASTE As Long = &H302
    17. Const GWL_WNDPROC = (-4)
    18. Dim PrevProc As Long
    19. Public Sub Hook(T As TextBox)
    20.     PrevProc = SetWindowLong(T.hwnd, GWL_WNDPROC, AddressOf WindowProc)
    21. End Sub
    22. Public Sub UnHook(T As TextBox)
    23.     SetWindowLong T.hwnd, GWL_WNDPROC, PrevProc
    24. End Sub
    25. Public Function WindowProc(ByVal hwnd As Long, ByVal uMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    26.     WindowProc = CallWindowProc(PrevProc, hwnd, uMsg, wParam, lParam)
    27.     If uMsg = WM_PASTE Then
    28.         Form1.cmdNext.Enabled = False
    29.     End If
    30. End Function

    "Brothers, you asked for it."
    ...Francisco Domingo Carlos Andres Sebastian D'Anconia

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Sep 2002
    Location
    Guwahati, Assam, India
    Posts
    131
    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.
    I am new to VB...

  24. #24
    Frenzied Member KayJay's Avatar
    Join Date
    Jul 2001
    Location
    Chennai
    Posts
    1,849
    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
  •  



Click Here to Expand Forum to Full Width