INFERNOUX
INFERNOUX

Reputation: 19

VB6 Listbox Right click

So i'm working on a game. Actually an mmorpg. And right now, I have a friends list, in the form of a listbox. Now what i would like to do is set it up so that if i right click a line in the listbox, it will ask me yes or no, and if yes it will warp me to that player. If the line is blank it wont have the popup asking yes or no.

And to help, the warp command is WarpMeTo Trim$()

For an example, if i had it warp me to something that was in a textbox it would be this WarpMeTo Trim$(textbox.text)

and yes, i do use the above for warping administrators to other players, where you input the name.

Actually, If anyone could just tell me how to "get" the name that is on that line in the listbox, that would be awesome, because I'm thinking of making a totally separate menu pop up when you click a name.

Upvotes: 0

Views: 2067

Answers (3)

CMaster
CMaster

Reputation: 389

As the other answers show, the problem is that the listbox doesn't really do what you need. It only changes the selected item on a left click. So you could have the user left clicks on their friend, then right clicks anywhere on the control to warp to that player. BUt that's going to lead to a lot of unexpected arrivals.

You could use coordinates to work out which name had been right clicked, without worrying about it being selected, something like this

Private Sub List_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
    Dim IndexRightClicked As Long
    If Button = vbRightButton Then
        IndexRightClicked = X + Y
        WarpMeTo Trim$(List.ItemData(IndexRightClicked))
    End If

End Sub

Where X = Y is replaced by maths, knowing the size of items in your list (and correcting for scrolling).

The best option though, I think, is for you to create a user control for the friends list. This will be more work, but means you can create something with the functionality that a good friends list is going to need. Then you could fairly easily use control arrays of CommandButtons or Labels (both can have appearance customized a lot) which are scrolled etc as appropriate.

Upvotes: 0

Hrqls
Hrqls

Reputation: 2951

This can be split into 2 parts

  • Make the listbox select an item on a right click
  • Show the item text only when it was right clicked

When you right click on an item in a listbox, the item isn't selected

To select the item you will have to simulate the left click via the mouse_event() API

After that you can use the _Click() event to show the item, but you will have to make sure the item was right clicked and not left clicked

'1 form with:
'  1 listbox: name=List1

Option Explicit

Private Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)

Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up

Private mblnRightClick As Boolean

Private Sub Form_Load()
  Dim intIndex As Integer
  'initialize no right click
  mblnRightClick = False
  'fill the listbox with some data
  List1.Clear
  For intIndex = 1 To 10
    List1.AddItem CStr(intIndex)
  Next intIndex
End Sub

Private Sub Form_Resize()
  List1.Move 0, 0, ScaleWidth, ScaleHeight
End Sub

Private Sub List1_Click()
  If mblnRightClick Then
    'process simulated left click
    MsgBox List1.Text
    'release right click simulation
    mblnRightClick = False
  End If
End Sub

Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  If Button = vbRightButton Then
    'simulate left click
    mouse_event MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0
    mouse_event MOUSEEVENTF_LEFTUP, X, Y, 0, 0
    mblnRightClick = True
  End If
End Sub

Upvotes: 1

Gianluca Colombo
Gianluca Colombo

Reputation: 819

If you select an item at once, you can put the following code on Menu Popup event:

listBox1.SelectedItem.ToString()

If you want to check all the selected items on listbox (multi select mode), you can use the following code:

Dim k As Long
Dim s As String

    For k = 0 To listBox1.ListCount - 1
    If listBox1.Selected(k) Then
        s = listBox1.List(k)
        ' ... do something
    End If
    Next

Upvotes: 0

Related Questions