CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    May 2007
    Posts
    5

    Resolved [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.

  2. #2
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: [2005] Contextmenustrip - One function to rule them all

    Well i'm not too sure what your looking for... but try this quick example i wipped up..

    Fire it up, right click on any textbox and select an option...

    and that option will be placed in the textbox...

    Gremmy...
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  3. #3
    Join Date
    May 2007
    Posts
    5

    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.

    I've been playing around with
    Code:
    ContextMenuStrip.SourceControl.Text = ContextMenuStrip.Text
    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.


    MadCat

  4. #4
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: [2005] Contextmenustrip - One function to rule them all

    well i added a little more to it.. it's still very crude, but does demonstrate the method i'm trying to describe..

    try this out...
    Attached Files Attached Files
    Last edited by GremlinSA; May 29th, 2007 at 08:25 AM. Reason: fixed zip...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  5. #5
    Join Date
    May 2007
    Posts
    5

    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:
    Code:
    ContextMenuStrip.SourceControl.Text = ContextMenuStrip.Text
    which does exactly what I need but the whole
    Code:
    = ContextMenuStrip.Text
    doesn't work.

    I think it has something to do with the sender in order to get the text from the selected menuitem.


    MadCat

  6. #6
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  7. #7
    Join Date
    May 2007
    Posts
    5

    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!


    MadCat

  8. #8
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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....

    Gremmy....
    Attached Files Attached Files
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  9. #9
    Join Date
    May 2007
    Posts
    5

    Resolved 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.

  10. #10
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    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...

    Gremmy...
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured