[2005] Contextmenustrip - One function to rule them all
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

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

Hybrid View

  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.

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center