PDA

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



Erdin? E. Ka
04-22-2007, 11:30 AM
Hi everyone, :hi:

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


Private Sub ListBox1_RightClick()
MsgBox "Well Done!"
End Sub

Charlize
04-22-2007, 02:58 PM
This is for a message box. Maybe you can alter it a little. Past this into your userform code.
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
Charlize

Erdin? E. Ka
04-22-2007, 10:38 PM
Hi Charlize,:hi:

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

But, i can't modified for my project. :doh:

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?:friends:

Thanks a lot.

Erdin? E. Ka
04-22-2007, 11:01 PM
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:


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

Charlize
04-23-2007, 07:14 AM
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 ...

Erdin? E. Ka
04-23-2007, 10:17 AM
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,:hi:

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

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

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

Thanks a lot for all...:friends:

xld
04-23-2007, 02:12 PM
Erdin?, That file showpopup works for me okay.

Charlize
04-24-2007, 03:27 AM
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.

Erdin? E. Ka
04-24-2007, 03:44 AM
Erdin?, That file showpopup works for me okay.

Hi Bob,:hi:

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... :friends::hi:

Charlize
04-24-2007, 07:39 AM
Hi Bob,:hi:

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... :friends::hi: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

Erdin? E. Ka
04-24-2007, 10:46 AM
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 :hi:

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

I hope to see you tomorrow...

Bye:hi:

Erdin? E. Ka
04-25-2007, 06:36 AM
Hi Charlize,

Finally, today i modified your codes to my project! :whistle:

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. :friends: :thumb :clap: