0

I have a text box and I want to check if any value is pasted in it ?

I am already using its Keypress event to check that any value is inserted in it or not?

But i want to check that a value is pasted in it not?

Salamander
  • 53
  • 5
  • possible duplicate of [Detecting if paste event occurred inside a rich text box](http://stackoverflow.com/questions/5618162/detecting-if-paste-event-occurred-inside-a-rich-text-box) – Adriano Repetti Jul 16 '14 at 07:46
  • 1
    Linked question is for RTF control but same technique applies without changes to a "plain" text box too. VB.NET version is [this one](http://stackoverflow.com/questions/10122320/how-to-detect-multiline-paste-in-richtextbox/10122729#10122729). – Adriano Repetti Jul 16 '14 at 07:47
  • @ Adriano Repetti thanks this seems much easier for me now – Salamander Jul 16 '14 at 07:50
  • Try `Textbox.TextChanged()` event – Nadeem_MK Jul 16 '14 at 08:17
  • @Nadeem_MK that will cause me problems because of the logic i have used – Salamander Jul 16 '14 at 09:48

2 Answers2

2

As for as I understand you said that you have already use TextBox Keypress event and now you want to check if a value is pasted in TextBox or not

If you are pasting your value thorough mouse than may be this will work fine

Private Sub TextBox_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles TextBox.MouseDown
 If e.Button = Windows.Forms.MouseButtons.Right Then
   //Code here what you want to do
 End If
End Sub

Note this will help you if you are pasting your value through mouse right click

YellowFlash
  • 139
  • 8
  • 1
    It is helpful but there is a problem with it That if you click the right button it trigger my event but i want to trigger it when a value is pasted not clicked Any help in this regard? – Salamander Jul 18 '14 at 06:57
0

You may have to do this manually... with some "creativity"

Dim lastProperText As String = ""

In your keypress event, save the last text you are satisfied with, after you ahve "handled" the input...

Textbox_Keypress

'your code here that filters out what you don;t want to handle...

lastProperText = textbox.text

/Textbox_Keypress

Then, in your textbox_textchanged event (This will fire after a paste has occured), compare the new text to your required format, if not in the required format, then set your textbox back to the last "good" text.

textbox.text = lastProperText

See this answer for reference:

https://stackoverflow.com/a/24774607/808404

Community
  • 1
  • 1
Louis van Tonder
  • 3,664
  • 3
  • 31
  • 62
  • @AdrianoRepetti Sorry, I don't follow? – Louis van Tonder Jul 16 '14 at 09:43
  • I mean: if I understood question correctly OP wants to detect a paste event. It's not about validation. – Adriano Repetti Jul 16 '14 at 09:45
  • @AdrianoRepetti. True, but he already states that he "catches" keypresses, which tells me he is probably validating. This, and the whole idead about wanting to "detect" a paste, smells like his keypress validation "can not" validate when text are pasted in (which is a common issue). Maybe the OP can shed some light... – Louis van Tonder Jul 16 '14 at 09:47
  • Hmmm you may be right! OP has to tell us what he's trying to do – Adriano Repetti Jul 16 '14 at 09:51
  • First of all thanks for you response and help Actually I have a scenario in which if a key is pressed in text box or a value is pasted in it and it will trigger another event which I am using – Salamander Jul 16 '14 at 09:53
  • Apologies to Louis Van Tender but the solution which @AdrianoRepetti given before is almost same what i am trying to accomplish – Salamander Jul 16 '14 at 09:56
  • 1
    In that case, just use text_changed? – Louis van Tonder Jul 16 '14 at 09:56
  • @LouisvanTonder that will cause some other problems because of the value in text box changes frequently Thanks again for your response I really feel happy after your resposes – Salamander Jul 16 '14 at 09:59