Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / October 2003

Tip: Looking for answers? Try searching our database.

Catching the paste message

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
C?lio Cidral Junior - 27 Oct 2003 20:25 GMT
Hello all. I don't know if this is the right newsgroup to post this message,
so sorry for any cross-posting.

I have a class inheriting from System.Windows.Forms.TextBox which contraints
user input to only numeric digits. It works well until user paste some text
from clipboard or by right clicking in the textbox's area and selecting
'paste'. My textbox need by someway intercepts the paste message (or do
anything like that). How can I make it?

Thank you in advance.

---
Celio C. J.
Gabriele G. Ponti - 27 Oct 2003 21:20 GMT
Hi,

Override the WndProc of the control, and handle the WM_PAINT message. When
handling the message you want to check if the data in the clipboard is valid
and allow normal processing, otherwise don't let the base control get the
message.

For example:

Public Class CustomTextBox
   Inherits System.Windows.Forms.TextBox

   Private Const WM_PASTE = &H302

   Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)

       ' check for WM_PASTE message
       If m.Msg = WM_PASTE Then

           Dim i As IDataObject
           Dim s As String = ""

           ' get data currently in the clipboard
           i = Clipboard.GetDataObject()

           ' check for text
           If i.GetDataPresent(DataFormats.Text) Then
               s = CType(i.GetData(DataFormats.Text), String)
           End If

           ' exit if invalid data in the clipboard
           If IsInvalid(s) Then Return

       End If

       ' default processing
       MyBase.WndProc(m)

   End Sub

   Private Function IsInvalid(ByVal s As String) As Boolean

       ' put your logic here
       Return (s = "can't paste this")

   End Function

End Class

Regards,

   Gabriele

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.