Results 1 to 13 of 13

Thread: [RESOLVED] [2008] shortcut menu, left click, add in reverse order

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Resolved [RESOLVED] [2008] shortcut menu, left click, add in reverse order

    Hello,

    I have created a shortcut menu. And assigned this to the link control. When the user right clicks the link control the shortcut menu will appear.

    Code:
    Me.linkLabel1.ContextMenu = contextMenu1
    However, is there a way to left click instead of right clicking. I could not find any property that would change this?

    My menu is created dynamically as it will display the last number that was dialed. However, I want to display the last number dailed as the top one in the short cut menu. Is there a way to reverse the order of the items in the shortcut menu?

    Code:
    //create a new menu item
    Dim menu As MenuItem = New MenuItem
    'Add the number entered from the text box.
    menu.Text = textBox1.Text
    contextMenu1.MenuItems.Add(menu)
    Many thanks,
    Last edited by steve_rm; Jul 18th, 2008 at 02:45 PM. Reason: Question not fully completed
    steve

  2. #2
    Addicted Member
    Join Date
    Nov 2007
    Posts
    159

    Re: [2008] shortcut menu, left click, add in reverse order

    I'm not sure about the left-click, but for the order of items in the shortcut menu, instead of:-

    vb.net Code:
    1. contextMenu1.MenuItems.Add(menu)

    ...try this:-

    vb.net Code:
    1. contextMenu1.MenuItems.Insert(0,menu)

    This should insert the entry at index position 0 in the list (so the last added is always at the top).

    I assume also, that you have in place some routine to check that TextBox1.Text actually has some text in it before trying to add to the Context Menu?
    If my post helped you, please rate it. Thanks.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2008] shortcut menu, left click, add in reverse order

    I should have seen this.

    vb Code:
    1. contextMenu1.MenuItems.Add(0, menu)

    Just the left click now?

    Thanks,
    steve

  4. #4
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] shortcut menu, left click, add in reverse order

    For the left click, you can handle the LinkClicked event and manually show your contextmenu by calling its Show() method.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2008] shortcut menu, left click, add in reverse order

    thanks,

    Problem solved.
    steve

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2008] shortcut menu, left click, add in reverse order

    Hello,

    Just another question to make this compete.

    How do I get the number that has been added to the shortcut menu? I have created a click event in the designer.
    vb Code:
    1. AddHandler Me.menu.Click, AddressOf menuitemRedial_Click

    And the method that raises the event.
    vb Code:
    1. Private Sub menuitemRedial_Click(ByVal sender As Object, ByVal e As EventArgs)
    2.      'Get the item that has been clicked.
    3.      Dim number As String = menu.Text
    4.  End Sub

    However, when I click on the number in the short cut menu. The event never fires. I want to get the number that the user clicked on.

    This will display all the number that was added on the click event. As in my first post.

    Then when the user clicks on the link control it displays the shortcut menu.

    vb Code:
    1. Private Sub linkLabel1_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs)
    2.     Dim p As New Point(45, 10)
    3.    
    4.      contextmenu.Show(Me.linkLabel1, p)
    5.  End Sub

    Many thanks for extra help,

    Steve
    Last edited by steve_rm; Jul 18th, 2008 at 02:43 PM. Reason: Question not fully completed
    steve

  7. #7
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] shortcut menu, left click, add in reverse order

    Show the code where you create a menuItem and add to your contextmenu.
    As a side note, you should turn Option Strict On and make it a default setting in of VS.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2008] shortcut menu, left click, add in reverse order

    Hello,

    Thanks for the reply.

    here is the code behind:
    vb Code:
    1. Private Sub RedialHistory_Load(ByVal sender As Object, ByVal e As EventArgs)
    2.     Me.linkLabel1.ContextMenu = contextmenu
    3. End Sub
    4.  
    5. Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
    6.     menu = New MenuItem()
    7.    
    8.     menu.Text = Textbox1.Text 'Get the number from the text box and add it.
    9.     contextmenu.MenuItems.Add(0, menu)
    10. End Sub
    11.  
    12. Private Sub linkLabel1_LinkClicked(ByVal sender As Object, ByVal e As LinkLabelLinkClickedEventArgs)
    13.     Dim p As New Point(45, 10)
    14.    
    15.     contextmenu.Show(Me.linkLabel1, p)
    16. End Sub
    17.  
    18. Private Sub menuitemRedial_Click(ByVal sender As Object, ByVal e As EventArgs)
    19.     Dim number As String = menu.Text
    20. End Sub

    The code in the designer:
    vb Code:
    1. Me.menu = New System.Windows.Forms.MenuItem()
    2. AddHandler Me.menu.Click, AddressOf menuitemRedial_Click
    3. Me.contextmenu = New System.Windows.Forms.ContextMenu()
    4.  
    5.  
    6. Private contextmenu As System.Windows.Forms.ContextMenu
    7. Private menu As System.Windows.Forms.MenuItem
    steve

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] shortcut menu, left click, add in reverse order

    Why would you go into the designer code? You should very very very rarely have to go in there. And what happens to all the Handles clauses?
    For every new MenuItem you create, you need to AddHandler to it.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2008] shortcut menu, left click, add in reverse order

    Hello,

    This is how I finally finished. If there is a cleaner way then please advice.

    I am not sure what is wrong with what Stanv suggested by not putting any code in the designer. I

    thought that was perfectly ok to do that. Declaring and Initializing controls in the designer like I have done. Why is that a bad idea?

    Many thanks,


    Designer code:
    Code:
    Me.menu = new System.Windows.Forms.MenuItem()         
    Me.contextmenu = new System.Windows.Forms.ContextMenu()
    
    private System.Windows.Forms.ContextMenu contextmenu
    private System.Windows.Forms.MenuItem menu

    code behind:
    Code:
    Private Sub RedialHistory_Load(ByVal sender As Object, ByVal e As EventArgs)
         Me.linkLabel1.ContextMenu = contextmenu
    End Sub
    
    Private Sub button1_Click(ByVal sender As Object, ByVal e As EventArgs)
         menu = New MenuItem()
        
         menu.Text = Me.textBox1.Text
         contextmenu.MenuItems.Add(0, menu)
         AddHandler Me.menu.Click, AddressOf menuitemRedial_Click
     End Sub
    
    Private Sub linkLabel1_LinkClicked(ByVal sender As Object, ByVal e As 
    
    LinkLabelLinkClickedEventArgs)
         Dim p As New Point(45, 10)
        
         contextmenu.Show(Me.linkLabel1, p)
    End Sub
    
    Private Sub menuitemRedial_Click(ByVal sender As Object, ByVal e As EventArgs)
         Dim redialMenu As MenuItem = DirectCast(sender, MenuItem)
         Dim number As String = redialMenu.Text
    End Sub
    steve

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] shortcut menu, left click, add in reverse order

    The designer generated code, as it's called, is generated and modified by the designer. For example, when you drop a control to your form, some code is generated automatically and when you delete a control from your form, some code is removed automatically. For this reason, a programmer should NOT edit this file manually. If you are editing or adding code to the designer file, be prepared for the designer to alter your code without warning. You should use the class file for all developer generated code.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    Re: [2008] shortcut menu, left click, add in reverse order

    Hello,

    Thanks for the reply.

    I am interested in what you said about the class.

    Not sure that I completely understand. Do you mean create a new class for that control?

    Thanks,
    steve

  13. #13
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2008] shortcut menu, left click, add in reverse order

    Quote Originally Posted by steve_rm
    Hello,

    Thanks for the reply.

    I am interested in what you said about the class.

    Not sure that I completely understand. Do you mean create a new class for that control?

    Thanks,
    With VS2005 and 2008, for each form you create, there exists 3 files:
    1. <formName>.vb This is called class file, and all your code should be in written in this file only
    2. <formName>.Designer.vb This is the designer generated code. You should very rarely have to dig into this file since your code may get changed by the designer without you knowing it.
    3. <formName>.resx This is the resource file. You should never edit this file. Just a single mistake and your form won't load any more.
    Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
    - Abraham Lincoln -

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