Consulting

Results 1 to 12 of 12

Thread: Solved: a VBA code example for Right Click Menu On a ListBox

  1. #1
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location

    Solved: a VBA code example for Right Click Menu On a ListBox

    Hi everyone,

    I need a VBA code example for Right Click Menu On a ListBox.

    Can someone give me any suggestion or show me a way to solve it?

    Thanks in advance.

    [VBA]
    Private Sub ListBox1_RightClick()
    MsgBox "Well Done!"
    End Sub
    [/VBA]
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  2. #2
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    This is for a message box. Maybe you can alter it a little. Past this into your userform code.
    [VBA]Private Sub ListBox1_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal Y As Single)
    Dim strMsg As String
    Select Case Button
    Case 1: strMsg = "Left"
    Select Case Shift
    Case 0: strMsg = strMsg & " + No Shift"
    Case 1: strMsg = strMsg & " + Shift"
    Case 3: strMsg = strMsg & " + Ctrl + Shift"
    End Select
    Case 2: strMsg = "Right"
    Select Case Shift
    Case 0: strMsg = strMsg & " + No Shift"
    Case 1: strMsg = strMsg & " + Shift"
    Case 3: strMsg = strMsg & " + Ctrl + Shift"
    End Select
    End Select
    MsgBox strMsg
    End Sub
    [/VBA]Charlize

  3. #3
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi Charlize,

    Thank you for your help. It's a very nice sample and it works fine.

    But, i can't modified for my project.

    Actually i have some datas on a ListBox1 and a ListBox2 by clicking CommandButton1. (On ListBox1 and ListBox2 the data counts are the same; for example: 5 rows on ListBox1 and 5 rows on ListBox2.)

    I want to have a right click menu ( "called Delete this row" ) on ListBox1 or ListBox2 for Delete the row which is i have right clicked.

    If i right clicked 3th row of ListBox1 then code should delete the 3th row of ListBox1 and delete the 3th row of ListBox2.

    I tried to tell the problem on Problem.JPG picture.

    The rows into red hoops. These are the same datas by repeated. I have to delete one of these data rows. ( 2th row or 3th row must be deleting.)

    Any ideas please?

    Thanks a lot.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  4. #4
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    I found a API code for right click menu on a listbox but still i need a code for clicked row is deleted.

    Here is the API code:

    [VBA]
    Private Type POINTAPI
    X As Long
    y As Long
    End Type
    '
    Private Declare Function CreatePopupMenu Lib "user32" () As Long
    Private Declare Function TrackPopupMenuEx Lib "user32" _
    (ByVal hMenu As Long, ByVal wFlags As Long, ByVal X As Long, ByVal y As Long, _
    ByVal hWnd As Long, ByVal lptpm As Any) As Long
    Private Declare Function AppendMenu Lib "user32" Alias "AppendMenuA" _
    (ByVal hMenu As Long, ByVal wFlags As Long, ByVal wIDNewItem As Long, _
    ByVal lpNewItem As Any) As Long
    Private Declare Function DestroyMenu Lib "user32" (ByVal hMenu As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
    Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
    (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
    '
    Const MF_CHECKED = &H8&
    Const MF_APPEND = &H100&
    Const TPM_LEFTALIGN = &H0&
    Const MF_SEPARATOR = &H800&
    Const MF_STRING = &H0&
    Const TPM_RETURNCMD = &H100&
    Const TPM_RIGHTBUTTON = &H2&
    '
    Dim hMenu As Long
    Dim hWnd As Long
    Private Sub lbx_Kayit_Listesi_Parca_1_MouseUp(ByVal Button As Integer, ByVal Shift As Integer, ByVal X As Single, ByVal y As Single)
    If Button <> 2 Then Exit Sub
    Dim Pt As POINTAPI
    Dim ret As Long
    hMenu = CreatePopupMenu()
    AppendMenu hMenu, MF_STRING, 1, "Delete This Row On ListBox1 and ListBox2"
    GetCursorPos Pt
    ret = TrackPopupMenuEx(hMenu, TPM_LEFTALIGN Or TPM_RETURNCMD Or _
    TPM_RIGHTBUTTON, Pt.X, Pt.y, hWnd, ByVal 0&)
    DestroyMenu hMenu
    Select Case ret
    Case 1
    Call Delete_This_Rows
    End Select
    End Sub

    Private Sub Delete_This_Rows()
    MsgBox "There should be a code for clicked row is deleted from ListBox1 and ListBox2"
    End Sub

    Private Sub UserForm_Initialize()
    hWnd = FindWindow(vbNullString, Me.Caption)
    End Sub
    [/VBA]
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  5. #5
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Single listbox setup with right click and delete. But how is your listbox filled ? If you want to delete rows in a sheet, you have to have a unique value of some kind to search for. Anyway, play with this one to give you some ideas ...

  6. #6
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Quote Originally Posted by Charlize
    Single listbox setup with right click and delete. But how is your listbox filled ? If you want to delete rows in a sheet, you have to have a unique value of some kind to search for. Anyway, play with this one to give you some ideas ...
    Hi Charlize,

    Actually your codes are perfect but, me is a unsuccessful.

    When i work in your workbook its ok but when i modified to my workbook, itsn't work ...

    I added my a piece of my project here. Please help me if you have a little time Clarlize...

    On "EEK_008_Irsaliyeli_Fatura" userform i save some invoices row by row... Sometimes i add listboxes an extra data (repeated data) by click to a commandbutton is called "Add the datas to the listboxes" and i have to delete the extra row and this row is variable..

    If i can modified your last sample to my profect it will be perfect for me...

    And finally, i don't want to delete rows in a sheet.

    Thanks a lot for all...
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Erdin?, That file showpopup works for me okay.

  8. #8
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    I noticed that you're listboxes have multiselect. Set them to multiselect single. I only tested it with one item at a time. Otherwise we have to loop through all the selected items and delete them if they are selected. After I changed them to fmmultiselectsingle it did showed the popup (and setting the boundcolumn to 4 at the form_initialize because I needed a value and column 1 hasn't any value in it).

    Your file a little bit modified. Right click without selecting. Then select and right click.

    Charlize

    ps.: Check it thoroughly before implementing this.

  9. #9
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Quote Originally Posted by xld
    Erdin?, That file showpopup works for me okay.
    Hi Bob,

    As i said, actually Charlize's codes are perfect but i didn't modified to my project. Anyway, it's my mistake. But i solved the problem via modified Charlize's code to DoubleClick event of ListBox.

    I want to thank again to you and to Charlize..

    Take care...
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  10. #10
    VBAX Master
    Joined
    Jul 2006
    Location
    Belgium
    Posts
    1,286
    Location
    Quote Originally Posted by Erdin? E. Ka
    Hi Bob,

    As i said, actually Charlize's codes are perfect but i didn't modified to my project. Anyway, it's my mistake. But i solved the problem via modified Charlize's code to DoubleClick event of ListBox.

    I want to thank again to you and to Charlize..

    Take care...
    Have you looked at the modified file that you posted. If you run that (with your own menu) you'll see that right click will work for the first of the two listboxes.

    Charlize

  11. #11
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Quote Originally Posted by Charlize
    Have you looked at the modified file that you posted. If you run that (with your own menu) you'll see that right click will work for the first of the two listboxes.

    Charlize
    Hi Charlize

    I saw your last post after i sent my last message... And i have downloaded your last workbook now but, i have to go home at the moment ... ( because here is 20:43 o'clock and the break time of the job...) But i will work on the project and i will feedback to you.

    Thank you very much to kindly helps and care... You are so good.

    I hope to see you tomorrow...

    Bye
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

  12. #12
    VBAX Tutor Erdin? E. Ka's Avatar
    Joined
    Sep 2006
    Location
    Bursa
    Posts
    264
    Location
    Hi Charlize,

    Finally, today i modified your codes to my project!

    It works fine. I didn't deleted DoubleClick event and your right click method. Now i can use both of them.

    Thank you very very much.
    Erdin? E. Kara?am | Loves from Bursa city in Republic of T?rkiye

Posting Permissions

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