TextBoxElement in Ribbon

5 posts, 1 answers
  1. Jerome Prunera-Usach
    Jerome Prunera-Usach avatar
    12 posts
    Member since:
    Oct 2009

    Posted 19 Nov 2010 Link to this post

    Hello,

    I have a form with a RadRibbonBar and a RadGridView.
    Within the toolbar I have programmatically added a RadTextBoxElement (and a button) in order to search through the grid.
    Everything is working perfectly well except when I try to implement the CTRL+F shortcut. The event do get fired and I am able to catch the requested action, but then I cannot get the textbox to focus.

    I have tried both mytextboxelement.TextBoxItem.Focus() and mytextboxelement.Focus() with no success.

    private void searchKeyDown(object sender, KeyEventArgs e)
    {
        if(e.KeyCode == Keys.F3)
            btnSearch_Click(null, null);
        else if (e.KeyCode == Keys.F && e.Modifiers == Keys.Control)
        {
            RadTextBoxElement txt = (RadTextBoxElement)this.radRibbonBarButtonGroup1.Items[0];
            txt.TextBoxItem.Focus();
        }
    }

    Any idea if I'm doing something wrong or if there is a problem ?

    Thank you,

    • OS Information : Windows XP x64 (version 2003) SP2
    • Telerik : RadControls for WinForms 2010.3 10.1109
    • Microsoft Visual Studio 2008
    • .NET 3.5
  2. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Nov 2010 Link to this post

    Hello Jerome,

    I'd like to try this and help you with your issue. Please can you tell me, the keydown event that you are using below, what is it the KeyDown event of?

    Let me know and I'll do my best to help
    Thanks
    Richard
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Answer
    Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 20 Nov 2010 Link to this post

    Hello again Jerome,

    Ok, i've been having a look at this for you, and it's ended up to be quite simple. In the code below, I have a RadRibbonBar and a RadGridView on the form. I've added one tab to the RadRibbonBar, and one group to the tab. In the group, I have then added a RadTextBoxElement during Form Load and wired up the KeyDown events of each of the items on the form.

    The magic line to get the cursor to flash in the textbox is:
    txt.TextBoxItem.HostedControl.Focus();


    Here is the whole thing..
    using System;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
      
    namespace RadControlsWinFormsApp1
    {
        public partial class Form1 : Form
        {
      
            public Form1()
            {
                InitializeComponent();
            }
      
            private void Form1_Load(object sender, EventArgs e)
            {
                RadTextBoxElement textboxElment = new RadTextBoxElement();
                this.radRibbonBarGroup1.Items.Add(textboxElment);
                this.radRibbonBar1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SearchKeyDown);
                this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SearchKeyDown);
                this.radGridView1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.SearchKeyDown);
            }
      
            private void SearchKeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.F3)
                    MessageBox.Show("Here");
                else if (e.KeyCode == Keys.F && e.Modifiers == Keys.Control)
                {
                    RadTextBoxElement txt = (RadTextBoxElement)this.radRibbonBarGroup1.Items[0];
                    txt.TextBoxItem.Text = "";
                    // Focus on the hosted control, not the textbox item
                    txt.TextBoxItem.HostedControl.Focus();
                
            }
      
        }
    }

    Hope this helps, but let me know if you need more information. Please remember to mark as answer if this has provided the answer for you.
    Thanks
    Richard
  5. Richard Slade
    Richard Slade avatar
    3000 posts
    Member since:
    May 2009

    Posted 22 Nov 2010 Link to this post

    Hello,

    How did you get on with this? If it helped, please remember to mark as answer. If you need more assistance, please just let me know.
    thanks
    Richard
  6. Jerome Prunera-Usach
    Jerome Prunera-Usach avatar
    12 posts
    Member since:
    Oct 2009

    Posted 22 Nov 2010 Link to this post

    Hello Richard,

    txt.TextBoxItem.HostedControl.Focus();

    That single line of code did the trick.

    Thank you
Back to Top
UI for WinForms is Visual Studio 2017 Ready