huji
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Did you put the code into it's own class?
Bob
Bob
It should work from the Form_Load() event.
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 )
@Bob: You both answered the @Idle_Mind part of my previous post, didn't you?! (I mean, don't forget the @Bob part :op )
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
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
ASKER
Need your help here as well:
https://www.experts-exchange.com/questions/21742221/Validating-textbox-2.html
Huji
https://www.experts-exchange.com/questions/21742221/Validating-textbox-2.html
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.
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.
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?
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.Ge tDataPrese nt(DataFor mats.Text) Then
Dim str As String = Clipboard.GetDataObject.Ge tData(Data Formats.Te xt)
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
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.Ge
Dim str As String = Clipboard.GetDataObject.Ge
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
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>
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
Bob
ASKER
Read the article here. You can vote for me as well:
http://www.codeproject.com/useritems/NumericTextBox.asp
Thanks
Huji
http://www.codeproject.com/useritems/NumericTextBox.asp
Thanks
Huji
ASKER
A first chance exception of type 'System.NullReferenceExcep
@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.