RadTextBox Mouse Hover to change Backcolor

4 posts, 1 answers
  1. Ke
    Ke avatar
    18 posts
    Member since:
    Feb 2016

    Posted 02 Mar Link to this post

    I need to achieve a function that when mouse hover over a RadTextBox, it changes its background color. Like what is shown in the demo, text box changed color from gray to white when mouse cursor moves inside the text box.  

    I tried to use Mouse-Enter event to change the backcolor. However, this event only triggers when the mouse cursor touches exactly at the border of the text box, as soon as it leaves the border, although the mouse cursor stays inside the text box, this event is no longer valid. I also tried Mouse-hover event, which did not give me a good result either. 

    It seems to me that what I need to achieve is a common issue and must have a relatively easy solution. I am wondering if I missed anything.

    Thank you! 

  2. Answer
    Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 04 Mar Link to this post

    Hi Ke,

    Thank you for writing.

    You should use the events of the TextBoxItem:
    public RadForm1()
    {
        InitializeComponent();
        radTextBox1.TextBoxElement.TextBoxItem.MouseEnter += radTextBox1_MouseEnter;
        radTextBox1.TextBoxElement.TextBoxItem.MouseLeave += radTextBox1_MouseLeave;
    }
     
    void radTextBox1_MouseLeave(object sender, EventArgs e)
    {
        radTextBox1.BackColor = Color.White;
    }
     
    void radTextBox1_MouseEnter(object sender, EventArgs e)
    {
         radTextBox1.BackColor = Color.Red;
    }

    Let me know if you have additional questions.

    Regards,
    Dimitar
    Telerik

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Ke
    Ke avatar
    18 posts
    Member since:
    Feb 2016

    Posted 04 Mar in reply to Dimitar Link to this post

    Thank you, this answers my question. Just a quick fix to your code. I also need to use the TextBoxItem to change the background color. 

    void radTextBox1_MouseLeave(object sender, EventArgs e)
    {
        radTextBox1.TextBoxElement.TextBoxItem.BackColor = Color.White;
    }

  5. Dimitar
    Admin
    Dimitar avatar
    1408 posts

    Posted 07 Mar Link to this post

    Hi Ke,

    Thank you for writing back.

    In the latest version, you should be able to set the color directly. For  previous versions, you should use the TextBoxItem property.

    In addition, I want to mention that you with the latest version you can directly set the Back/Fore color of most controls.

    I hope this will be useful. 

    Regards,
    Dimitar
    Telerik

Back to Top