Link to home
Start Free TrialLog in
Avatar of huji
hujiFlag for United States of America

asked on

OnPaste!

I want a way to perform some actions when a user is pasting something to a textbox. Can you help please?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of huji

ASKER

@Idle_Mind: I couldn't use your code, though I could feel how brilliant the solution was! Were should I put this:     Dim tbp As New TextBoxPaste(Me.TextBox1)    ? Putting that inside the Public Class Form1 results in this error:
A first chance exception of type 'System.NullReferenceException' occurred in WindowsApplication1.exe

@Bob: Yours worked for me. However, this is not to allow or disallow paste only. I want to check if adding what is going to be pasted, to the current value of the textbox, will match a regular expression pattern for example. If not, paste should be blocked. Although I'm more or less aware of how to modify your code to achieve this, I'd like to ask you to do it for me.
Did you put the code into it's own class?

Bob
It should work from the Form_Load() event.
Avatar of huji

ASKER

Bob is right. I forgot to place Mike's class in a seperate Class file, and change it to Public.
@Bob: You both answered the @Idle_Mind part of my previous post, didn't you?! (I mean, don't forget the @Bob part :op )
Avatar of huji

ASKER

@Idle_Mind:
Two questions: (1) What if I create a second class which also overrides the WndProc method? Currently your code calls Mybase.WndProc(m) in the end to ensure the normal WndProc is undergone after what we want. Now, adding a second overriding method will need to call the first one at the end? (2) I'm going to submit an article based on your code somewhere. How do you prefer to be referenced there as an acknowledgement?
Best regards
Huji
Why would you have two classes that Override WndProc?...

You should only have one WndProc...either in:

    (1) The Class that Inherits from NativeWindow    (as I showed)

    (2) The Class that Inherits from TextBox    (as Bob showed)

I wouldn't recommend mixing the two approaches.
Avatar of huji

ASKER

Perhaps I didn't clearly stated what I meant. Suppose that I modify your approach to create a second class (e.g. on that controls the values pasted in a control other than textbox.) I will have a function overriding WndProc in this second class too. Will this make a conflict?
Avatar of huji

ASKER

@Idle_Mind:
I updated your code. Selecting "." in 2.2 and trying to paste "." again was not handled in your code. Here is the final version I'm using:

Public Class TextBoxOnPaste
    Inherits NativeWindow

    Private tb As TextBox

    Enum validTypes
        Intgr = 1
        Dbl = 2
    End Enum

    Private Sub New()
    End Sub

    Public Sub New(ByVal tb As TextBox, ByVal type As validTypes)
        Me.tb = tb
        Me.AssignHandle(tb.Handle)
    End Sub

    Private Const WM_PASTE As Integer = &H302

    Protected Overrides Sub WndProc(ByRef m As Message)
        Select Case m.Msg
            Case WM_PASTE
                Debug.WriteLine("Paste")
                If Clipboard.GetDataObject.GetDataPresent(DataFormats.Text) Then
                    Dim str As String = Clipboard.GetDataObject.GetData(DataFormats.Text)
                    Dim NewVal As String
                    Debug.WriteLine("Pasted: " & str)
                    NewVal = Mid(tb.Text, 1, tb.SelectionStart) & str & Mid(tb.Text, tb.SelectionStart + tb.SelectionLength, Len(tb.Text))
                    If IsNumeric(NewVal) Then
                        ' you can change what is pasted like this:
                        tb.SelectedText = str
                    ElseIf tb.SelectedText = "." Then
                        tb.SelectedText = str
                    End If
                    ' If you want to cancel the paste, then use Exit Sub
                    Exit Sub
                End If
        End Select

        MyBase.WndProc(m)
    End Sub
End Class
Avatar of huji

ASKER

<insisting>
Dear Idle_Mind, please contact me on the emial address you can find in my profile, about the best way to credit you in the article in which I'm going to use your above posted code.
</insisting>
That's funny that it didn't work, because I created an instance of the extended TextBox on a form, set AllowPaste = False, and it didn't allow pasting.

Bob
Avatar of huji

ASKER

Read the article here. You can vote for me as well:
http://www.codeproject.com/useritems/NumericTextBox.asp
Thanks
Huji