Set focus in searchcell

7 posts, 1 answers
  1. Jan
    Jan avatar
    45 posts
    Member since:
    Oct 2007

    Posted 13 Jun Link to this post

    Hello,

    I'm using the very good built in serch function AllowSearchRow.
    However I have it hidden by default and made it possible to use via a menu and a RadShortcut.
    When the user clicks the menu (button in my sample below) I would like the SearchRow to show and put focus to the searchbox textbox. But the searchCell is null when the SearchRow is shown.
    I suppose I need to put the AllowSearchRow = true and the searchCell.SearchTextBox.Focus() in two different Events, but which ones?

    Any suggestions appretiated!

    My sample:

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using Telerik.WinControls.UI;
     
    namespace SearchTextBoxFocus
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
     
            private void Form1_Load(object sender, EventArgs e)
            {
                List<MyObject> myList = new List<MyObject>();
                myList.Add(new MyObject(1, "Outdoor"));
                myList.Add(new MyObject(2, "Hardware"));
                myList.Add(new MyObject(3, "Tools"));
                myList.Add(new MyObject(4, "Books"));
                myList.Add(new MyObject(5, "Appliances"));
                radGridView1.DataSource = myList;
            }
     
            private void button1_Click(object sender, EventArgs e)
            {
                this.radGridView1.AllowSearchRow = !this.radGridView1.AllowSearchRow;
     
                GridSearchCellElement searchCell = radGridView1.TableElement.GetCellElement(radGridView1.MasterView.TableSearchRow, null) as GridSearchCellElement;
                if (searchCell != null)
                {
                    searchCell.SearchTextBox.Focus();
                }
            }
        }
    }
    public class MyObject
    {
        public MyObject(int myInt, string myString)
        {
            _myInt = myInt;
            _myString = myString;
        }
        private int _myInt;
        public int MyInt
        {
            get { return _myInt; }
            set { _myInt = value; }
        }
        private string _myString;
        public string MyString
        {
            get { return _myString; }
            set { _myString = value; }
        }
    }

  2. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 13 Jun Link to this post

    Hi Jan,

    Thank you for writing.

    Indeed, in order to achieve your task you would need to split the operation and perform it in two separate events. In this scenario it is suitable that you handle the MouseDown and MouseUp events. Please check my code snippet below: 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
           //...
        }
     
        private void button1_MouseUp(object sender, MouseEventArgs e)
        {
            GridSearchCellElement searchCell = radGridView1.TableElement.GetCellElement(radGridView1.MasterView.TableSearchRow, null) as GridSearchCellElement;
            if (searchCell != null)
            {
                searchCell.SearchTextBox.Focus();
            }
        }
     
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            this.radGridView1.AllowSearchRow = !this.radGridView1.AllowSearchRow;
        }
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Jan
    Jan avatar
    45 posts
    Member since:
    Oct 2007

    Posted 14 Jun in reply to Hristo Merdjanov Link to this post

    Excellent thank you Hristo!

    But what if I want to exctend this to work with a radmenu instead of a button?
    AND hook a shortcut to that radmenuitem: this.radMenuItem2.Shortcuts.Add(new RadShortcut(Keys.Control, Keys.F));
    What event would you suggest I use to set the focus?

    Many thanks!

    Jan

  5. Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 14 Jun Link to this post

    Hi Jan,

    Thank you for writing back.

    Basically, you would need to do the same. Once you have added the custom menu item, instead of subscribing to its Click event, subscribe to its MouseDown and MouseUp events. Please check my example below: 
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
     
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
     
            List<MyObject> myList = new List<MyObject>();
            myList.Add(new MyObject(1, "Outdoor"));
            myList.Add(new MyObject(2, "Hardware"));
            myList.Add(new MyObject(3, "Tools"));
            myList.Add(new MyObject(4, "Books"));
            myList.Add(new MyObject(5, "Appliances"));
     
            radGridView1.DataSource = myList;
     
            this.radGridView1.ContextMenuOpening += radGridView1_ContextMenuOpening;
        }
     
        private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
        {
            RadMenuSeparatorItem separator = new RadMenuSeparatorItem();
            e.ContextMenu.Items.Add(separator);
     
            RadMenuItem customMenuItem = new RadMenuItem();
            customMenuItem.Text = "Show Search Row";
            customMenuItem.MouseDown += customMenuItem_MouseDown;
            customMenuItem.MouseUp += customMenuItem_MouseUp;
     
            e.ContextMenu.Items.Add(customMenuItem);
        }
     
        private void customMenuItem_MouseUp(object sender, MouseEventArgs e)
        {
            GridSearchCellElement searchCell = radGridView1.TableElement.GetCellElement(radGridView1.MasterView.TableSearchRow, null) as GridSearchCellElement;
            if (searchCell != null)
            {
                searchCell.SearchTextBox.Focus();
            }
     
        }
     
        private void customMenuItem_MouseDown(object sender, MouseEventArgs e)
        {
            this.radGridView1.AllowSearchRow = !this.radGridView1.AllowSearchRow;
        }
    }

    I hope this helps. Please let me know if you need further assistance.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  6. Jan
    Jan avatar
    45 posts
    Member since:
    Oct 2007

    Posted 15 Jun in reply to Hristo Merdjanov Link to this post

    Hello Hristo!

    Thank you for your reply.

    Your sample is working as expected. However I had one more feature request ;) and that is to be able to hook a shortcut to the menuitem. For example: this.radMenuItem2.Shortcuts.Add(new RadShortcut(Keys.Control, Keys.F));
    The shortcut seems to fire the Click event and thus doesn't fire the mouse events at all.
    Thank you for your patience.

    Regards

    Jan

  7. Answer
    Hristo Merdjanov
    Admin
    Hristo Merdjanov avatar
    712 posts

    Posted 16 Jun Link to this post

    Hi Jan,

    Thank you for writing.

    The shortcut you are adding to the context menu is actually firing the Click event of the menu item. In this case, we need to handle this event and apply our logic there. Since the search cell is created dynamically we have to also call the Application.DoEvents method.
    private void radGridView1_ContextMenuOpening(object sender, ContextMenuOpeningEventArgs e)
    {
        RadMenuSeparatorItem separator = new RadMenuSeparatorItem();
        e.ContextMenu.Items.Add(separator);
     
        RadMenuItem customMenuItem = new RadMenuItem();
        customMenuItem.Text = "Show Search Row";
        customMenuItem.Shortcuts.Add(new RadShortcut(Keys.Control, Keys.F));
        customMenuItem.Click += customMenuItem_Click;
     
        e.ContextMenu.Items.Add(customMenuItem);
    }
     
    private void customMenuItem_Click(object sender, EventArgs e)
    {
        this.radGridView1.AllowSearchRow = !this.radGridView1.AllowSearchRow;
        this.radGridView1.ContextMenuManager.HideContextMenu();
        Application.DoEvents();
     
        GridSearchCellElement searchCell = radGridView1.TableElement.GetCellElement(radGridView1.MasterView.TableSearchRow, null) as GridSearchCellElement;
        if (searchCell != null)
        {
            searchCell.SearchTextBox.Focus();
        }
    }

    I hope this helps. Should you have further questions please do not hesitate to write back.

    Regards,
    Hristo Merdjanov
    Telerik
    Check out the Windows Forms project converter, which aids the conversion process from standard Windows Forms applications written in C# or VB to Telerik UI for WinForms.For more information check out this blog post and share your thoughts.
  8. Jan
    Jan avatar
    45 posts
    Member since:
    Oct 2007

    Posted 16 Jun in reply to Hristo Merdjanov Link to this post

    Hello Hristo!

    Thank you! 

    Application.DoEvents(); did the trick!

    Best regards

    Janne

Back to Top
UI for WinForms is Visual Studio 2017 Ready