Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Tell me what COLUMN that I clicked in a List Box

Status
Not open for further replies.

dmreda

Technical User
Mar 31, 2004
5
US

Hi,

If I create a multi-column list box, and the user clicks on it anywhere, I can get what ROW they clicked on easily enough (using the ListIndex attribute).

However, how can I determine which COLUMN the user clicked in?

Is there an object better suited to this than a listbox?

Thanks,

-dmreda

 
Interesting question!

AFAIK, there is no way to tell what column of the listbox was clicked. In terms of the users selection, the whole row is considered to be a single "item" (even though you can read the values in the columns seperately).

What about putting several listboxes side by side, and populating them with synchronized data? Then put code in the click event of each one to set the ListIndex of the other boxes to be the same as the clicked one. If you had three listboxes, the click event for the first one might look something like:

Code:
Private Sub ListBox1_Click()
ListBox2.ListIndex = ListBox1.ListIndex
ListBox3.ListIndex = ListBox1.ListIndex
End Sub
If that approach doesn't work, give us more detail on the actual application/issue you're trying to resolve, and maybe we can suggest something else.


VBAjedi [swords]
 
You should be able to process the MouseMove event and calculate which column the mouse is in based on the X value.
Code:
Private Sub ListBox1_MouseMove( _
           ByVal Button As Integer, _
           ByVal Shift As Integer, _
           ByVal X As Single, _
           ByVal Y As Single)
  TextBox1.Text = X
End Sub
 
But Zathras, wouldn't that essentially be a hard-coded solution? In other words, if the contents of the lisbox changed, and the column widths followed, wouldn't your code be off then?

Or am I missing something?

VBAjedi [swords]
 
VBAjedi, I suppose you're right in a general sense, but if you set the column widths property I don't think the widths would change. (Or if they did, perhaps you could read the property to see the current widths.)

I don't say it's a better solution, just a different one.
 
No, I like it - I was just trying to understand it. I had forgotten (duh!) that you can set the column widths.

Let us know if that works, dmreda! (And don't forget to give Zathras a star if it does!)



VBAjedi [swords]
 

Thanks for all the responses! VBAJedi's idea of several listboxes adjacent seems the quickest to implement, so I'm going with that. It's just a look-and-feel prototype I've got to deliver with a project proposal, but the mouse position idea is very clever, and I may wind up using something like that if I get funding.

I've never done much with mouse positions, mouse events, etc. Should be fun learning. :)

Thanks again!

-dmreda
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top