[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..
Re: [2005] Contextmenustrip - One function to rule them all
Right, I tried that code and I don't know whether i'm overlooking something cause i'm so tired or i've missed something out completely but i'm getting a couple of errors :\
Where it says AsObject i'm getting the error of "Comma or ')' expected" and under e its saying "All parameters must be explicitly typed if any are."
I think its time for sleep.
Many thanks again for your help, its much appreciated!
Re: [2005] Contextmenustrip - One function to rule them all
Well with a fresh head and a little time to think about it at work.. I got it all sorted..
Firstly my pre. post just had formatting errors, (missing spaces) and has been corected...
Now.. The fun part.....
This new example has only Four Subs....
1: Form load sub:
Load and init a Timer
Set all relevant textboxes contextmenu's to our Contextmenu...
2: Form Closing sub:
Unload the Timer (Just better to clean up)
3: Timer tick Event sub:
Check the clipboard for new text and load it into our contextmenu items..
4: Contextmenu Click sub:
Place slected item on the selected textbox..
With this now all you need to do is alter the Form Load sub to include all the relevant textboxes you'd like to use it on...
USAGE
Fire it up, now open a document in notepad or word or anywhere.. select a few blocks of text and copy them to the clipboard (right click, Copy). Do 6 blocks of text this way..
Now return to the form and right click on one of the textboxes, your contextmenu now contains the last 6 clipboard copy items... select any one of them and it will be placed in the textbox...
I'm sure this is exactly what your looking for....
Re: [RESOLVED] [2005] Contextmenustrip - One function to rule them all
Excellent! That works an absolute treat and exactly as I wanted!
Haha, yea I can see now it was just one space that was messing with me last night, sitting right under my nose!
Again, thankyou so much for your help with this, I dont think I could have asked for simpler code
MadCat
Last edited by madcat1981; May 29th, 2007 at 12:08 PM.
Re: [RESOLVED] [2005] Contextmenustrip - One function to rule them all
LOL .. Funny how a little sleep deprivation can cause funny things to happen..
Glad to have sorted it all out.. Looking over the original post, You gave just enough info for Some fresh, but for those of us (hmm.. me) that seem to be getting slower and slower, the extra bit's afterwards made lights twinkle...
But that aside this is a very interesting topic, and i've tagged it in my signature for future reference...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.