C# 对可编辑组合框使用LostFocus事件时的Messagebox
在对可编辑组合框使用LostFocus事件时,我遇到了一个问题C# 对可编辑组合框使用LostFocus事件时的Messagebox,c#,combobox,messagebox,C#,Combobox,Messagebox,在对可编辑组合框使用LostFocus事件时,我遇到了一个问题 private void comboBox8_LostFocus(object sender, RoutedEventArgs e) { ... else if (8int <= 7int && 8int >= 100) { MessageBox.Show("Error description", "E
private void comboBox8_LostFocus(object sender, RoutedEventArgs e)
{
...
else if (8int <= 7int && 8int >= 100)
{
MessageBox.Show("Error description", "Error!", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
...
}
private void comboBox8\u LostFocus(对象发送方,RoutedEventArgs e)
{
...
否则如果(8int=100)
{
Show(“错误描述”,“错误!”,MessageBoxButton.OK,MessageBoxImage.Information);
}
其他的
...
}
除了messagebox显示外,一切正常,即使我用鼠标单击comboBox文本字段进行编辑。如果使用上一个字段中的“tab”,则没有messagebox。但是直接点击鼠标会导致错误的消息框。我只需要在使用“tab”离开组合框或在其他地方单击鼠标(lostfocus)时显示它。谁能帮我提个建议吗?我找不到类似的情况。谢谢。您想改为收听
组合框的文本框部分
private void comboBox8_Loaded(object sender, RoutedEventArgs e)
{
TextBox tb = (TextBox)(sender as ComboBox).Template.FindName("PART_EditableTextBox", (sender as ComboBox));
if (tb != null)
tb.LostFocus += new RoutedEventHandler(tb_LostFocus);
}
void tb_LostFocus(object sender, RoutedEventArgs e)
{
...
else if (8int <= 7int && 8int >= 100)
{
MessageBox.Show("Error description", "Error!", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
...
}
private void comboBox8_已加载(对象发送方、路由目标方)
{
TextBox=(TextBox)(发送者作为组合框).Template.FindName(“PART_EditableTextBox”,(发送者作为组合框));
如果(tb!=null)
tb.LostFocus+=新路由EventHandler(tb_LostFocus);
}
void tb_LostFocus(对象发送方,路由目标)
{
...
否则如果(8int=100)
{
Show(“错误描述”,“错误!”,MessageBoxButton.OK,MessageBoxImage.Information);
}
其他的
...
}
哦,这对我很有用:
private void comboBox8_LostFocus(object sender, RoutedEventArgs e)
{
...
else if (8int <= 7int && 8int >= 100)
{
if (!comboBox8.IsKeyboardFocusWithin)
{
MessageBox.Show("Error description", "Error!", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
else
...
}
private void comboBox8\u LostFocus(对象发送方,RoutedEventArgs e)
{
...
否则如果(8int=100)
{
如果(!Combobobox8.IsKeyboardFocusWithin)
{
Show(“错误描述”,“错误!”,MessageBoxButton.OK,MessageBoxImage.Information);
}
}
其他的
...
}
谢谢你 可能还有另一种方法,不是上面的方法没有用:)从RoutedEventArgs检查源和原始源
如果事件发生在控件内,则它是一个内部lostfocus事件(combobox具有多个内部控件),并且source和originalsource将具有相同的类型
这个代码对我来说很好用。当组合框失去焦点时,我“做点什么”
希望对你有帮助
/T现在,当您直接单击下拉箭头时,不会出现错误消息框,但当您直接单击文本时,仍然会出现错误消息框。正如我写的,这个组合框是可编辑的。你是把它添加到我的答案中还是这是一个不同的回答?它是不同的,但我也非常感谢你的解决方案!谢谢。你把这个添加到你的失焦事件中了吗?因为当我尝试这个方法时,当我离开focus.woops时,我会收到两个消息框,忽略我的最后一条评论,因为我还有其他代码。
**private void TimePicker_LostFocus(object sender, System.Windows.RoutedEventArgs e)
{
if (e.OriginalSource.GetType().Name != e.Source.GetType().Name)
{
Validate();
}
}**