当我们显示一个弹出窗口时,请专注于主控件 [英] keep focus on main control when we display a popup

查看:74
本文介绍了当我们显示一个弹出窗口时,请专注于主控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在创建带有ToolStripDropDown控件和Gridview的Multicolumn comboBox.我想要一个功能,当用户在组合框中输入文本时,gridview控件应在任何位置过滤与输入文本匹配的文本.

示例:带有2个项目的组合框:测试",这是一个测试".
->当用户在组合框中输入测试"时,建议同时使用这两项(默认功能仅列出第一项).

因此,我必须实现组合框事件TextChanged,然后弹出窗口将列出匹配的项(基于我的搜索功能).但是,当显示弹出窗口时,组合框将失去焦点,因此我无法输入更多要增加的文本.

http://social.msdn.microsoft.com/论坛/en-US/csharpgeneral/thread/a64dd06f-a55e-4a0c-b5e1-ce9fa9f5cca5 [

Hi All,

I am creating a Multicolumn comboBox with ToolStripDropDown control and a Gridview. I want a feature when user input a text in the combo box, gridview control should filtered with the input text match with them in any where.

Example: combo box with 2 items: "test", "this is a test".
-> when user input "test" in combo box, both of items are suggested (default feature only lists the first item).

So, I must implement the event TextChanged of combo box, and a popup window will list the matched items (based on my search feature). However, when popup window is displayed, the combo box will lost the focus, so I can''t input more text to increase.

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/a64dd06f-a55e-4a0c-b5e1-ce9fa9f5cca5[^]

Please guide me to solve this problem.
Thanks.

推荐答案

请这些文章
MultiColumnComboBoxEx:扩展了数据绑定的多列ComboBox [ ^ ]
和此帮助页面
http://msdn.microsoft.com/en-us/library/system. windows.forms.toolstripdropdown.aspx [ ^ ]
他们可能会有所帮助.

但是从问题中可以看出,在ComboBox中键入文本时,DatagridView被过滤了.我认为没有必要将重点放在DataGridView 上,因为DataGridView 仅用于显示过滤后的数据.在这种情况下,请在其下使用TextBoxDataGridView. TextBox 收到焦点时,显示DataGridView,而TextBox 失去焦点时隐藏DataGridView.在TextBoxTextChanged 事件中,将过滤器应用于分配给DataGridViewDataSource属性的BindingSource .这样,将滤镜应用于DataGridView时,不会丢失TextBox 的焦点,如下所示:
Please these article
MultiColumnComboBoxEx: An Extended Data-Bound Multiple Column ComboBox[^]
and this help page
http://msdn.microsoft.com/en-us/library/system.windows.forms.toolstripdropdown.aspx[^]
they may be helpful.

But as seen from the question it appears that as the text is being typed in the ComboBox the DatagridView is filtered. I think there is no requirement of focus to be given to the DataGridView as it is being used only for the purpose of displaying the filtered data. In such case, use a TextBox with a DataGridView below it. When, the TextBox receives focus, show the DataGridView and when the TextBox loses focus hide the DataGridView. In the TextChanged event of TextBox, apply the filter to the BindingSource which is assigned to the DataSource property of the DataGridView. In this way the focus from the TextBox is not not lost, when the filter is applied to the DataGridView as shown below:
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;

namespace GridViewComboxBox {
    public partial class Form1 : Form {
        DataTable DataTable1;
        BindingSource BindingSource1;
        DataGridView DataGridView1;
        TextBox TextBox1;
        Button Button1;
        
        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            DataTable1 = new DataTable();
            DataTable1.Columns.Add("ItemNo", typeof(int), null);
            DataTable1.Columns.Add("ItemDescription", typeof(string), null);

            DataTable1.Rows.Add(1, "This is first Item");
            DataTable1.Rows.Add(2, "This is second Item");
            DataTable1.Rows.Add(3, "This is third");
            DataTable1.Rows.Add(4, "This is fourth");

            BindingSource1 = new BindingSource();
            BindingSource1.DataSource = DataTable1;
            DataGridView1 = new DataGridView();
            DataGridView1.DataSource = BindingSource1;

            TextBox1 = new TextBox();
            TextBox1.GotFocus += (s, args) => DataGridView1.Show();
            TextBox1.LostFocus += (s, args) => DataGridView1.Hide();
            TextBox1.TextChanged += (s, args) => 
                BindingSource1.Filter = string.Format(
                "ItemDescription like '%{0}%'", TextBox1.Text);

            TextBox1.Location = new Point(10, 10);
            TextBox1.Width = 275;
            Controls.Add(TextBox1);

            DataGridView1.Location = new Point(10, 15 + TextBox1.Location.Y);
            DataGridView1.Width = 275;
            Controls.Add(DataGridView1);

            Button1 = new Button();
            Button1.Text = "Button";
            Button1.Location = new Point(10, 50);
            Controls.Add(Button1);
        }
    }
}


要运行上述示例,请创建一个Windows Forms应用程序,并用上述代码替换Form1.cs文件的内容,然后运行该应用程序.


我认为这可能会有所帮助.


To run the above sample, create a Windows Forms application and replace the contents of the Form1.cs file with the above code and run the application.


I think it may be helpful.


只需添加ToolStripDropDown.AutoClose = false;
并触发文本框的textChanged事件时,显示弹出窗口并调用Textbox.Focus();
Just add ToolStripDropDown.AutoClose = false;
and when the textChanged event of textbox fired, show the popup and call Textbox.Focus();


这篇关于当我们显示一个弹出窗口时,请专注于主控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆