按下输入键时从内部自定义控件中触发事件

时间:2017-10-27 17:05:07

标签: c# winforms events

我有一个自定义控件,它需要触发一个事件,然后在表单上捕获并处理信息。

自定义控件是一个列表框。当我左键单击鼠标时,表单上的事件被触发,我完全更新了这些部分。

当我使用向上/向下键查看列表框数据然后按ENTER键时,我此时在列表框上触发单击事件,但表单未收到通知。我该如何解决这个问题?

控件中的事件处理程序

/// <summary>
        /// Event on the client when the selection has been made (click or hit enter)
        /// </summary>
        public event EventHandler ItemSelected
        {
            add { listBox.Click += value; }
            remove { listBox.Click -= value;  }
        }

列表框click and keydown

private void listBox_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && listBox.Items.Count > 0)
            {
                EventArgs x = e as EventArgs;

                listBox_Click(sender, x); //treat it as click to select the item 
            }
        }

        private void listBox_Click(object sender, EventArgs e)
        {
            if (listBox.Items.Count > 0)
            {
                ComboBoxItem item = (ComboBoxItem)listBox.SelectedItem;
                if (item != null)
                {
                    _id = item.ID;
                    _code = item.Code;
                    //_text = item.Text;

                    //MessageBox.Show("U: " + _code + "-" + _texts);

                    base.Text = item.Text;  //item.DisplayField; 
                }
            }
            listBox.Visible = false;
        }

表单上的事件处理程序

Private Sub YtxtDesc_ItemSelected(sender As Object, e As System.EventArgs) Handles YtxtDesc.ItemSelected
        btnDesc.PerformClick()
    End Sub

0 个答案:

没有答案