radtextbox, detecting paste

4 posts, 0 answers
  1. Jorge Delgado-Lopez
    Jorge Delgado-Lopez avatar
    82 posts
    Member since:
    Aug 2007

    Posted 06 Jun 2009 Link to this post

    Hi.

    How can I detect the paste drop down event? I need to message the user when the pasting text does not fit the filtering mask. I tried with 0x0302 in WndProc but without success.

    - jorge
  2. Boyko Markov
    Admin
    Boyko Markov avatar
    610 posts

    Posted 10 Jun 2009 Link to this post

    Hello Jorge Delgado-Lopez,

    I have prepared a way to detect WM_PASTE message in RadTextBox. Here is the solution:

    1. Declare  WM_PASTE const:
         private const int WM_PASTE = 0x0302;
    2. Create a class called MessageIntercept:

       public class MessageIntercept : NativeWindow, IDisposable 
            { 
                #region Fields 
     
                private Control targetControl; 
     
                private MessageHandler messageHandlerCallback; 
                #endregion 
                #region Constructor 
     
                /// <summary> 
                /// Creates an instance of the MessageInterceptor class. 
                /// </summary> 
                /// <param name="target">The target Control which messages to intercept</param> 
                /// <param name="messageCallback">A reference to a method that is invoked when a message is received.</param> 
                public MessageIntercept(Control target, MessageHandler messageCallback) 
                { 
                    this.targetControl = target; 
                    this.messageHandlerCallback = messageCallback; 
                    this.targetControl.HandleCreated += new EventHandler(targetControl_HandleCreated); 
                    this.targetControl.HandleDestroyed += new EventHandler(targetControl_HandleDestroyed); 
     
                    if (this.targetControl.IsHandleCreated) 
                    { 
                        this.AssignHandle(this.targetControl.Handle); 
                    } 
                } 
                #endregion 
                #region Properties 
     
                /// <summary> 
                /// Gets the target control associated with the MessageInterceptor. 
                /// </summary> 
                public Control Target 
                { 
                    get 
                    { 
                        return this.targetControl; 
                    } 
                } 
     
                public bool IsActive 
                { 
                    get 
                    { 
                        return this.targetControl.IsHandleCreated; 
                    } 
                } 
                #endregion 
                #region Methods 
     
                public void SetMessageReceivedCallback(MessageHandler callback) 
                { 
                    this.messageHandlerCallback = callback; 
                } 
     
                protected override void WndProc(ref Message m) 
                { 
                    base.WndProc(ref m); 
     
                    if (this.messageHandlerCallback != null
                    { 
                        this.messageHandlerCallback(ref m); 
                    } 
                } 
                #endregion 
                #region Event handling 
     
                private void targetControl_HandleCreated(object sender, EventArgs e) 
                { 
                    this.AssignHandle(this.targetControl.Handle); 
                } 
     
     
                private void targetControl_HandleDestroyed(object sender, EventArgs e) 
                { 
                    this.ReleaseHandle(); 
     
                } 
                #endregion 
                #region IDisposable Members 
     
                public void Dispose() 
                { 
                    if (this.targetControl != null
                    { 
                        this.messageHandlerCallback = null
                        this.ReleaseHandle(); 
                    } 
                } 
                #endregion 
            } 

    3. After doing that, you should create an instance of that class:

            MessageIntercept handler = new MessageIntercept(this.radTextBox1.TextBoxElement.TextBoxItem.HostedControl, MessageInterceptor);
        
    4. We passed the MessageInterceptor callback method as a parameter. Now it is time to declare it:

    public void MessageInterceptor(ref Message msg)
            {
                if (msg.Msg == WM_PASTE)
                {
             // TO DO
                }
            }

    This will handle the WM_PASTE message. Another solution that will handle the Paste through the CTRL+V shortcut could be to subscribe to the KeyDown event

    1.    this.radTextBox1.TextBoxElement.TextBoxItem.HostedControl.KeyDown += new KeyEventHandler(HostedControl_KeyDown);

    2,  In the event handler, you can check whether CTRL + V is pressed

     void HostedControl_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
                {
                
                   // TO DO
                }
            }

    Please write me back if you need more information.

    Regards,
    Boyko Markov
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. jerry
    jerry avatar
    103 posts
    Member since:
    Feb 2008

    Posted 25 Mar 2010 Link to this post

    Boyko,
    How can this be done for a textbox column within a grid?  If I have max length set at 10 and they past a 15 character string 5 characters are chopped off and the user will not know.  I want to be able to let them know that some of their text has been truncated.  Any help would be greatly appreciated.

    Thank You
  5. Svett
    Admin
    Svett avatar
    728 posts

    Posted 31 Mar 2010 Link to this post

    Hello jerry,

    Yes, you can use the code snippet that my colleague Boyko gave you. You should subscribe to CellBeginEdit and CellEndEdit events of the grid where you can register and unregister to the events or messages of the text box editor.

    You can use the following code snippet that is based on Boyko's sample:

    void radGridView_CellBeginEdit(object sender, GridViewCellCancelEventArgs e)
    {
        RadTextBoxEditor textBoxEditor = this.radGridView.ActiveEditor as RadTextBoxEditor;
        RadTextBoxEditorElement element = textBoxEditor.EditorElement as RadTextBoxEditorElement;
        Control hostedControl = element.HostedControl;
        hostedControl.KeyDown += new KeyEventHandler(hostedControl_KeyDown);
    }
     
    void radGridView_CellEndEdit(object sender, GridViewCellEventArgs e)
    {
        RadTextBoxEditor textBoxEditor = this.radGridView.ActiveEditor as RadTextBoxEditor;
        RadTextBoxEditorElement element = textBoxEditor.EditorElement as RadTextBoxEditorElement;
        Control hostedControl = element.HostedControl;
        hostedControl.KeyDown -= new KeyEventHandler(hostedControl_KeyDown);
    }
     
    void hostedControl_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Modifiers == Keys.Control && e.KeyCode == Keys.V)
        {
     
            // TO DO
        }
    }

    Another option is to use Boyko's first approach with MessageIntercept class. Hence, instead of subscribing and unsubscribing for KeyDown event, you should create and dispose the instance of MessageIntercept class that hooks this behavior.

    If you need further assistance, feel free to write us back.

    Regards,
    Svett
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Back to Top