嵌套选项卡控件上的快捷键问题 [英] Shortcut key issue on nested tab control

查看:80
本文介绍了嵌套选项卡控件上的快捷键问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有选项卡控件的Windows窗体.它有两个选项卡页面,A和B.选项卡A包含另一个选项卡控件.用户可以向该标签控件动态添加标签,因此可以有1到N个标签.这些选项卡包含一个RichTextBox,在添加选项卡页面时会为其分配一个上下文菜单.这是我用来添加新标签页的代码:

I have a windows form with a tab control. It has two tab pages, A and B. Tab A contains yet another tab control. Users dynamically add tabs to this tab control so there can be from 1 to N tabs. These tabs contain a single richtextbox which is assigned a contextmenu when the tab page is added. Here is the code I use to add a new tab page:

strTabName = "SQL Tab " & tcOnTabPageA.TabPages.Count
Dim tpNew As New TabPage(strTabName)
tpNew.Controls.Add(New RichTextBox)
tpNew.Padding = New Padding(3)
tpNew.BackColor = Color.FromKnownColor(KnownColor.Control)
tpNew.ContextMenuStrip = mnuForTabs

Dim rtxt As RichTextBox = CType(tpNew.Controls(0), RichTextBox)
rtxt.Dock = DockStyle.Fill
rtxt.Font = gblFont
rtxt.BorderStyle = BorderStyle.FixedSingle
rtxt.ContextMenuStrip = mnuForText
rtxt.DetectUrls = False
rtxt.AcceptsTab = True

'Remove handlers before adding again to ensure that the event will only be run once
RemoveHandler rtxt.TextChanged, AddressOf richtextbox_TextChanged
RemoveHandler rtxt.KeyDown, AddressOf FieldName_KeyDown

'Connect handlers to the proper methods
AddHandler rtxt.TextChanged, AddressOf richtextbox_TextChanged
AddHandler rtxt.KeyDown, AddressOf FieldName_KeyDown

'Creating a new tab page at the end
tcOnTabPageA.TabPages.Insert(tcOnTabPageA.TabPages.Count - 1, tpNew)



选项卡B上只有一个RichTextBox.导致问题的情况是:用户位于选项卡A上,并在任何嵌套的选项卡1-N上键入RichTextbox.他们导航到选项卡B,然后在其中输入richtextbox.它们导航回到TabA.这时,ContextMenuItem快捷键不再对嵌套Tabs 1-N上的richtextbox起作用(您可以在上面的代码中看到mnuForText).我可以看到Richtextbox的ContextMenuStrip已正确设置,并且Menu Item的Shortcut键已设置且正确,但是它不会触发MenuItem_Click事件.如果我在文本框中单击鼠标右键,则会显示正确的ContextMenuStrip,并且从那点开始,快捷键将再次在RichTextBox中正常工作,直到用户再次导航到选项卡B.是否有人对导致ContextMenu无法正常启动的原因有任何想法?或对接下来尝试调试以解决此问题的任何建议?



Tab B has just a richtextbox on it. The scenario that causes a problem is this: A user is on Tab A and types into a richtextbox on any of the nested Tabs 1-N. They navigate to Tab B and type in the richtextbox there. They navigate back to Tab A. At this point the ContextMenuItem Shortcut Keys no longer work for richtextbox on the nested Tabs 1-N (you can see in the code above this is mnuForText). I can see that the richtextbox''s ContextMenuStrip is set properly and the Shortcut key for the Menu Item is set and correct, but it doesn''t fire the MenuItem_Click event. If I right click in the textbox, the correct ContextMenuStrip appears and from that point on the shortcut keys work properly again in the richtextbox until the user navigates to Tab B again. Does anyone have any ideas about what is causing the ContextMenu to not fire properly? Or any suggestions of what to try to debug next to solve this issue?

推荐答案

一年多以前,我发布了此问题,终于弄清楚了这个问题是什么.它根本与嵌套的选项卡控件无关.我在上下文菜单的Opening事件中有一些代码,该代码根据当前是否选择了文本来设置是否启用或禁用了所讨论的菜单项.好吧,因为这仅会在菜单的Opening事件中触发,所以当您第一次专注于文本框并使用快捷键时,默认情况下该项目处于禁用状态,这就是为什么它不起作用的原因.然后,当我右键单击以获取菜单时,它将重置为启用状态,并在其余时间内正常工作.

因此,我的解决方案是始终启用此选项,并且在未选择任何文本时仅不执行操作.
I posted this question over a year ago and I have finally figured out what this issue was. It had nothing to do with the nested tab controls at all. I had some code in the context menu''s Opening event that sets if the menu item in question is enabled/disabled based on whether or not text is currently selected. Well, since this only fires in the menu''s Opening event, the first time you focus on the textbox and use the shortcut key, it''s defaulted that the item is disabled which is why it doesn''t work. Then when I right click to get the menu, it resets to be enabled and works for the rest of the time.

So my solution is to always have this option enabled and to just not perform the actions when no text selected.


这篇关于嵌套选项卡控件上的快捷键问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆