将消息发送到在toolstripdownhost

时间:2019-02-12 12:53:45

标签: winforms parent-child toolstripdropdown

无法中继ProcessCmdKey或将ListBox上键入的消息发送到TextBox到其ToolStripDropDown控件主机。

我有一个自定义TextBox(受限制的允许字符和其他各种自定义类型),它在ToolStripDropDown内托管一个ListBox。 ListBox的数据源上下文是BindingSource,因此在TextBox中键入的任何文本都将用作BindingSource的BindingFilter。 我应该在TextBox具有焦点时显示所有选项,并且如果需要,用户应过滤TextBox中的列表键。     因此,在TextBox焦点事件内部,我设置为显示ToolStripDropDown(ListBox)。但是,当显示下拉列表时,用户无法键入文本框进行过滤,因为它不再是活动控件。     如何将在ListBox或ToolStripDropDown中键入的数据中继或发布到其父级TextBox,以便其文本相应更改。

操纵ProcessCmdKey路由到(TextBox)base.ProcessCmdKey没有帮助,因为传递了消息,但TextBox文本未更改。

 public class PopupWindow : System.Windows.Forms.ToolStripDropDown  
    {
        private System.Windows.Forms.ToolStripControlHost _host;
        public System.Windows.Forms.Control contentControl;
        public System.Windows.Forms.Control ParentControl;       

        public PopupWindow(System.Windows.Forms.Control content, System.Windows.Forms.Control parentControl)
        {
            //Basic setup...
            this.AutoSize = false;
            this.DoubleBuffered = true;
            this.ResizeRedraw = true;
            contentControl = content;
            ParentControl = parentControl;

            this._host = new System.Windows.Forms.ToolStripControlHost(contentControl)
            {
                Margin = Padding.Empty,
                Padding = Padding.Empty,
                AutoSize = false
            };            

            //Positioning and Sizing
            this.MinimumSize = content.MinimumSize;            
            this.Size = content.Size;
            this.Dock = DockStyle.Fill;
            content.Location = Point.Empty;

            //Add the host to the list
            this.Items.Add(this._host);
        }


        #region keystroke events

        private bool ProcessKeyDown(Keys keyData)
        {
            try
            {
                if (contentControl == null || !_host.IsOnDropDown || (contentControl is ListBox) == false)
                    return false;

                ListBox lb = contentControl as ListBox;

                switch (keyData)
                {
                    case Keys.Down:                        
                        if (lb.SelectedIndex < lb.Items.Count - 1)
                            lb.SelectedIndex++;
                        return true;
                    case Keys.Up:
                        if (lb.SelectedIndex > 0)
                            lb.SelectedIndex--;
                        return true;
                    case Keys.Enter:
                        //suggestionList.SelectedSuggestion = suggestionList.suggestionListBox.Text;
                        ParentControl.Text = lb.GetItemText(lb.SelectedItem);
                        this.Hide();
                        return true;
                    case Keys.Escape:
                        this.Hide();
                        ParentControl.Focus();
                        return true;
                    default:
                        this.Hide();
                        ParentControl.Focus();
                        return false;
                }
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
        {
            if (ProcessKeyDown(keyData))
                return true;

            return ((PopupTextBox)ParentControl).parentProcessCmdKey(ref msg, keyData); 
        }

        #endregion
    }

0 个答案:

没有答案