【问题标题】:Catching input events捕获输入事件
【发布时间】:2017-11-17 22:59:21
【问题描述】:

我有一个包含TextBox、ComboBox 和CheckBox 的表单,它们是从一个函数中填充的(例如textbox.text = "newValue";)。

private void updateIdentite(Contact c, Dal dal)
{
    Identite id = dal.GetIdentite(c);
    if (id != null)
    {
        idCivilite.SelectedValue = id.type_civilite;//example of a ComboBox
        ...
        idNom.Text = id.pers_nom;//example of a TextBox
        ...
        idAi.IsChecked = (id.cont_drap_ai == "1");//example of a CheckBox
        ...
    }
}

当用户手动编辑项目时,我需要捕获一个事件,但使用TextChanged、SelectionChanged 或Checked,当表单被之前的函数填充时,这些事件也会被触发。

foreach (object item in identiteGrid.Children)
{
    TextBox tb = item as TextBox;
    if (tb != null)
    {
        tb.TextChanged += new TextChangedEventHandler(idTextBox_TextChanged);
        continue;
    }
    ComboBox droplist = item as ComboBox;
    if (droplist != null)
    {
        droplist.SelectionChanged += new SelectionChangedEventHandler(idCombobox_SelectionChanged);
        continue;
    }
    CheckBox cb = item as CheckBox;
    if (cb != null)
    {
        cb.Checked += new RoutedEventHandler(idCheckbox_Checked);
        continue;
    } 
}

有没有办法为这些控件捕获“onInput”事件?

谢谢

【问题讨论】:

  • “当表单被上一个函数填充时”是什么意思?您指的是哪个“先前的功能”?
  • @mm8 "由函数填充(例如,使用 `textbox.text = "newValue";)"
  • @mm8 确实没必要,但已编辑。

标签: c# wpf events


【解决方案1】:

您可以连接您的事件处理程序,即执行 foreach 循环,在您设置了初始值之后。

或者您可以在以编程方式设置值之前暂时取消事件处理程序:

private void updateIdentite(Contact c, Dal dal)
{
    Identite id = dal.GetIdentite(c);
    if (id != null)
    {
        idCivilite.SelectionChanged -= idCombobox_SelectionChanged;
        idCivilite.SelectedValue = id.type_civilite;//example of a ComboBox
        idCivilite.SelectionChanged += idCombobox_SelectionChanged;

        tb.TextChanged -= idTextBox_TextChanged;
        idNom.Text = id.pers_nom;//example of a TextBox
        tb.TextChanged += idTextBox_TextChanged;

        idAi.Checked -= idCheckbox_Checked;
        idAi.IsChecked = (id.cont_drap_ai == "1");//example of a CheckBox
        idAi.Checked += idCheckbox_Checked;
    }
}

或者您可以使用布尔标志来防止事件处理程序中的代码在特定情况下运行:

private bool handleSelectionChanged = true;
private void updateIdentite(Contact c, Dal dal)
{
    ...
    handleSelectionChanged = false;
    idCivilite.SelectedValue = id.type_civilite;//example of a ComboBox
    handleSelectionChanged = true;
    ...
}

private void idCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (!handleSelectionChanged)
        return;

    //your code...
}

【讨论】:

  • 我会把它当作布尔标志的想法(我已经想出了这个“临时”解决方案),但是谢谢你,请支持我。
【解决方案2】:

获取用户输入

考虑使用:Control.OnKeyDown 方法。

为了用户输入,他必须将注意力集中在控件上并按下某个键。

另外,如果你想被完全覆盖,你也可以在控件上挂上Control.MouseClick事件...

阅读有关这些活动的更多信息。


https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onkeydown(v=vs.110).aspx


https://msdn.microsoft.com/en-us/library/system.windows.forms.control.mouseclick(v=vs.110).aspx

【讨论】:

  • 我想到了这个解决方案,但这对ComboBox 是否适用于MouseClick 事件?如果用户只是点击ComboBox 而不更改所选项目,则不应触发事件
  • 我添加了 MouseClick,主要是为了让用户尝试仅使用鼠标粘贴信息(右键 -> 粘贴)。那不常见。因此,如果您想挂钩此事件,请考虑在函数内部检查单击了哪些鼠标键。在组合框上考虑挂钩到 SelectedIndexChanged
  • 另外,SelectedIndexChanged 在 WPF 中不存在。
  • WPF 中的SelectedIndexChanged ->> stackoverflow.com/questions/28374316/…
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-06-29
  • 1970-01-01
  • 1970-01-01
  • 2014-12-25
  • 2012-04-25
  • 2020-01-02
  • 2011-10-17
相关资源
最近更新 更多