This is a migrated thread and some comments may be shown as answers.

Set focus in searchcell

6 Answers 215 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jan
Top achievements
Rank 1
Jan asked on 13 Jun 2016, 12:41 PM

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; }
    }
}

6 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 13 Jun 2016, 02:25 PM
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.
0
Jan
Top achievements
Rank 1
answered on 14 Jun 2016, 06:36 AM

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

0
Hristo
Telerik team
answered on 14 Jun 2016, 06:13 PM
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.
0
Jan
Top achievements
Rank 1
answered on 15 Jun 2016, 08:37 AM

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

0
Accepted
Hristo
Telerik team
answered on 16 Jun 2016, 01:06 PM
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.
0
Jan
Top achievements
Rank 1
answered on 16 Jun 2016, 01:28 PM

Hello Hristo!

Thank you! 

Application.DoEvents(); did the trick!

Best regards

Janne

Tags
GridView
Asked by
Jan
Top achievements
Rank 1
Answers by
Hristo
Telerik team
Jan
Top achievements
Rank 1
Share this question
or