Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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:
VB
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)


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?
Posted
Comments
Maciej Los 7-Apr-11 11:44am    
1)Try to declare NewTabs() as array of (new) tabs. Declare it as global variable. I think, the garbage collector removes handle to unused object(s).
2)
The other way (to find solution) is to store 2 TabControl objects on the form. First TabControl (TC1) is visible and has 1 page, the second TabControl (TC2) has n pages with Richtextbox. You can add new pages to TC1 using tabpages from TC2.
Kschuler 7-Apr-11 15:42pm    
I'm not sure I understand your suggestions. I add my new tab to a TabControl collection, so it's not unused. I don't see how what I have is any different than adding the tab to an array. Also, it's not the event handlers that are broken. They still work just fine with the code I have now. It's the shortcut key to an item on the context menu that doesn't work. Regarding your suggestion 2, I'm not sure what you mean about creating a tabcontrol with n pages....when I say n I mean that it could be 1 or it could be 100. The user decides how many are there. I don't want to create hundreds of TabPages at design time just in case the users want them. That's why I made it dynamic. Am I misunderstanding what you meant? If so, can you supply a bit of code to help explain it?
Maciej Los 8-Apr-11 11:51am    
OK, now i understand your problem. Have richtextbox_TextChanged and FieldName_KeyDown functions some parameters?
Kschuler 8-Apr-11 12:01pm    
The same parms as any TextChanged or KeyDown event. But I don't think the problem has anything to do with those event handlers. I commented them out and ran the same scenario and still have the same problem.
Maciej Los 9-Apr-11 16:41pm    
I have made form to check your code. It's strange, but my program working perfect. As i wrote before, i think the problem is in the TextChanged and KeyDown procedures. I've made some trick. Here is my piece of code:
AddHandler rtxt.TextChanged, AddressOf RichTextBox1_TextChanged
AddHandler rtxt.KeyDown, AddressOf RichTextBox1_KeyDown
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Me.Text = sender.GetType.Name & "_Changed"
End Sub

Private Sub RichTextBox1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles RichTextBox1.KeyDown
Me.Text = sender.GetType.Name & "_KeyDown"
End Sub

Private Sub CutCMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CutCMS.Click
Me.Text = sender.GetType.Name & "_Cut_Click"
End Sub

Private Sub CopyCMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CopyCMS.Click
Me.Text = sender.GetType.Name & "_Copy_Click"
End Sub

Private Sub PasteCMS_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PasteCMS.Click
Me.Text = sender.GetType.Name & "_Paste_Click"
End Sub
It work perfect, no matter how many times i change focus between tabs on (both) tabcontrols.

1 solution

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.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900