简体   繁体   English

显示弹出框时TextBox失去焦点

[英]TextBox loosing focus when displaying popup

I try to implement something similar to Intellisense: while the user enters text in a (WinForms) TextBox, I'd like to display suggestions. 我尝试实现类似于Intellisense的功能:当用户在(WinForms)TextBox中输入文本时,我想显示建议。

Currently, I'm doing something like this in the TextChanged-EventHandler of the TextBox: 当前,我正在TextBox的TextChanged-EventHandler中执行以下操作:

        ListBox lb = new ListBox();
        lb.Items.Add(new DataItem() { Id = "1", DisplayText = "Abc" });
        lb.Items.Add(new DataItem() {Id = "2", DisplayText = "Abuiaec"});


        ToolStripDropDown popup = new ToolStripDropDown();
        popup.Margin = Padding.Empty;
        popup.Padding = Padding.Empty;
        ToolStripControlHost host = new ToolStripControlHost(lb);
        host.Margin = Padding.Empty;
        host.Padding = Padding.Empty;
        popup.Items.Add(host);
        popup.Show(this, Point.Empty);

However, as soon as the popup comes up, the TextBox loses its focus. 但是,一旦弹出窗口出现,TextBox就会失去焦点。 The popup does not get focus - it seems like the focus disappears completely. 弹出窗口没有获得焦点-似乎焦点完全消失了。

What can I do against this behavior? 我该如何应对这种行为?

Thanks! 谢谢!

尝试在最后添加textbox.Select()以将焦点放在textbox上。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM