[RESOLVED] [2005] Contextmenustrip - One function to rule them all
Hi,
This is my first post and i'm quite new to VB2005 so hopefully i'll explain what i'm after properly.
Basically i'm creating a single contextmenu for each of the text boxes I have on a form. Most of the contextmenu will be populated with text which when the user selects it, is set in the textbox which used the contextmenu.
Now I can do the contextmenu, thats fine and populate it with the data, the problem i'm having is telling the textbox to display the text the user has selected on the contextmenu.
I'm using sourcecontrol to find out which textbox fired the contextmenu but once one of the items is selected nothing is displayed in the textbox. I have also tried it with a msgbox to try and see whats going on but nothing is displayed in that also.
Here's a quick sample to show what i'm trying to do:
Code:
Private Sub ContextMenuStrip_click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ContextMenuStrip.Click
If ContextMenuStrip.SourceControl Is TextBox1 Then
TextBox1.Text = ContextMenuStrip.Text
ElseIf ContextMenuStrip.SourceControl Is TextBox2 Then
TextBox2.Text = ContextMenuStrip.Text
End If
End Sub
What I would eventually like to do is write just one function which finds which textbox fired the contextmenu and then set its text to that of the chosen menuitem on the contetmenu.
I really appreciate any help someone can give me.
MadCat
Last edited by madcat1981; May 29th, 2007 at 12:06 PM.
Re: [2005] Contextmenustrip - One function to rule them all
Gremmy,
Many thanks for your reply and taking the time to put together the code for me.
That does work and I have toyed with that option, only problem though is that the number of items on the menu will change throughout the application so I need something a bit more generic which will work across the board.
Now this works to a degree. Firstly it will allow me to enter the text straight into the textbox which fired the contextmenu but the problem I have is that it doesn't put the menu item text in it.
Re: [2005] Contextmenustrip - One function to rule them all
Hi,
Thanks again for taking some time to provide me with some code.
Basically the prob i'm going to have with that solution is that I wont know what the items will be called so its not really going to work making a .click event for each item. Plus i'm trying to make it all within the one function so its more universal and flexible.
I think I should explain more what i'm trying to do.
I'm creating an application to help with filling in forms I use at work. Currently i'm having to open and go between several different applications to get all the information which can be very time consuming. So what i'm doing is creating an application which listens for a change in the clipboard and then adds that text to the context menu. The user would then copy everything thats needed then on the form with all the text boxes the user can just select the required item from the context menu.
I tried using comboboxes and a datasource but ran into problems I couldn't resolve so i'm now taking the contextmenu route which is proving to work as I want.
What I would really like is to extend from this code:
Re: [2005] Contextmenustrip - One function to rule them all
Ok ... Comment out all the option#_click subs...
and put this sub in..
Code:
Private Sub ContextMenuStrip1_ItemClicked(ByVal sender As Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles ContextMenuStrip1.ItemClicked
ContextMenuStrip1.SourceControl.Text = e.ClickedItem.Text
End Sub
and try out the app again..
I am assumig that you got code in to catch text from the clipboard, and load it into an item on the context menu....
Time to sleep for me...
It's almost 2AM.. here...
Check in tomorow...
Gremmy
Last edited by GremlinSA; May 29th, 2007 at 08:01 AM.
Reason: Fixed formatting..
Bookmarks