上下文菜单重置 ComboBox 的 SelectedIndex
我有一个在用户右键单击组合框后显示的上下文菜单。 当用户在上下文菜单中选择一个项目时,将使用 ShowDialog()
方法调出一个表单。
If frmOptions.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
LoadComboBoxes()
End If
当该表单关闭时,我刷新父表单上组合框中的所有数据。 但是,当发生这种情况时,打开 ContextMenu 的 ComboBox 将重置为选定索引为 -1,但其他 ComboBox 的其他选定索引保持不变。
如何防止打开上下文菜单的组合框被重置?
I have a ContextMenu that is displayed after a user right clicks on a ComboBox. When the user selects an item in the context menu, a form is brought up using the ShowDialog()
method.
If frmOptions.ShowDialog() = Windows.Forms.DialogResult.Cancel Then
LoadComboBoxes()
End If
When that form is closed, I refresh all the data in the ComboBoxes on the parent form. However, when this happens the ComboBox that opened the ContextMenu is reset to have a selected index of -1 but the other selected indexes of the other ComboBoxes remain the same.
How do I prevent the ComboBox that opened the context menu from being reset?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想到了。
我创建了一个通过引用传递 ContextMenu.SourceControl() 属性的方法,以便我可以操作调用 ContextMenu 的控件。 在该方法的开头,我获取了 ComboBox 的 SelectedValue 并重新加载了 ComboBox 中的数据。 然后,我将 SelectedValue 设置为我在方法开头获得的值。
谢谢 DaveK 为我指明了正确的方向。
I figured it out.
I created a method that passed the ContextMenu.SourceControl() property by reference so I could manipulate the control that called the ContextMenu. In the beginning of the method, I got the SelectedValue of the ComboBox and the reloaded the data in the ComboBoxes. I then set the SelectedValue to the value I had got in the beginning of the method.
Thank you DaveK for pointing me in the right direction.
处理此问题的一种方法是使用上下文菜单的 Popup 事件来获取启动菜单的组合框的选定索引。 当对话框关闭时重置所选索引。
One way to handle this would be to use the context menu's Popup event to grab the selected index of the combobox launching the menu. When the dialog form closes reset the selected index.