Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

context menu on RTB 2

Status
Not open for further replies.

RonRepp

Technical User
Feb 25, 2005
1,031
US
Hi all:

I have a context menu that I want to show on my rich text box. The code seems to work fine, but the backcolor is white and the menu text is also white.

This is what I have:

[code>>MouseDown Event]
Dim M As MenuItem
Dim i As Integer
Dim PT As New Point(Cursor.Position.X, Cursor.Position.X)
cmEditor.MenuItems.Clear()

For Each M In EditMenu.MenuItems
M = New MenuItem
cmEditor.MenuItems.Add(M.Text)
Next

cmEditor.Show(Editor, PT)
[/code]

I've never experienced this type of behavior before.

Any help will be greatly appreciated.

Thanks,


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
What are the cmEditor and EditMenu ? Are they connected somehow? I cant try your code unless i know that
 
Ron, I no longer have 2003 installed and I believe menu handling etc. changed in 2005.

However this was written using 2003 and may be of some help, it demonstrates displaying a ListBox at the mouse cursor position in a RichTextBox, so shouldn't be too difficult to convert.

Look at my post dated 20 Nov 05 20:32

thread796-1153488


Hope this helps.

[vampire][bat]
 
TipGiver:

cmEditor is a context menu, and EditMenu is from a MainMenu control. I'm pretty sure that the menu items are copying, or the size of the shadow left by cmEditor would not show anything.

Editor is the RTF control name.

E & A:

My basic question here was why cmEditor took on a white backcolor with white text (same backcolor as the RTF control)?

I'm sure I didn't catch all the logic in your link, and I'll try to digest it. I can see using a form, as did Zameer (sp?), but wouldn't I be better off with a User Control?


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
2003 and 2005 handling is different. This was causing me errors.
The white seems to be the default backcolor

 
So, it's looking like a form or a user control, eh?

Thanks for your help,

Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Hi Ron
what are you tring to do, filling the context menu ?
It seems that when you click the RTB, you copy all the items of the mainmenu (the text property) to a context menu and then show it.

For your question, uc vs form, a uc needs a place to be loaded (a parent, say like mdi form children). I would choose a form instead of a uc.
 
I'm using VB 2008, which has ContextMenuStrip, and ToolStripMenuItem objects to work with. I've added the old ContextMenu to my ToolBox and tried this, which seems to work without a problem:

Code:
	Private Sub rtb1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles rtb1.MouseDown

		If e.Button = Windows.Forms.MouseButtons.Right Then
			Dim mi As MenuItem
			If cm1.MenuItems.Count > 0 Then
				For a As Integer = cm1.MenuItems.Count - 1 To 0 Step -1
					mi = cm1.MenuItems(a)
					RemoveHandler mi.Click, AddressOf cmMenuItem_Click
					cm1.MenuItems.RemoveAt(a)
					mi = Nothing
				Next
			End If
			For Each m As MenuItem In EditMenu.MenuItems
				mi = New MenuItem(m.Text)
				cm1.MenuItems.Add(mi)
				AddHandler mi.Click, AddressOf cmMenuItem_Click
			Next
			cm1.Show(rtb1, New Point(e.X, e.Y))
		End If


	End Sub

	Private Sub cmMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

		Dim mi As MenuItem = CType(sender, MenuItem)
		'do whatever is needed based on specific menu item
		MessageBox.Show(mi.Text)

	End Sub

Additionally the MenuItem has a Font property and a BackColor property so if necessary you could manipulate those.

In the thread that I mentioned earlier, the RichTextBox was a UserControl.

One final point with regard to displaying the ContextMenu, in the Form's Load Event you could set:

[tt]rtb1.ContextMenu = cm1[/tt],

which would mean that you don't have to handle the positioning or display of the context menu - I'm presuming that that property is available in 2003.


Hope this helps.

[vampire][bat]
 
E & A:

Good approach. I was merely clearing the menuitems and after looking at your code, I need to get rid of the handlers as well.

I wish I could give you another star.

Thanks,


Ron Repp

If gray hair is a sign of wisdom, then I'm a genius.

My newest novel: Wooden Warriors
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top