Determine List Item by Coordinates

Determine List Item by Coordinates

I wanted users to be able to get a definition for each item in a list box by right-clicking on the item. Unfortunately, right-clicking doesn’t automatically select the item, so you need some other way of knowing your location in the list box. Simply reading the Y value from the MouseDown event and converting that value to a line number will work, unless the user scrolls the list. I used the SendMessage API, which can get information from controls, to solve the problem. Here’s the code:

 		'Declarations section		Private Declare Function SendMessage Lib "user32" _			Alias "SendMessageA" (ByVal hWnd As Long, ByVal _			wMsg As Long, ByVal wParam As Long, lParam As _			Any) As LongConst LB_GETITEMHEIGHT = &H1A1'value to get height of one line in listbox		'listbox codePrivate Sub List1_MouseDown(Button As Integer, Shift As _	Integer, X As Single, Y As Single)	Dim msg As String	Dim TopIndex As Long	Dim CharHeight As Long	Dim CurIndex As Long	With List1		'find height of one line in listbox		CharHeight = SendMessage(.hWnd, LB_GETITEMHEIGHT, _			0, 0)		'function returns height in pixels so convert to 		'twips		CharHeight = CharHeight * Screen.TwipsPerPixelY		If Button = 2 Then 'right click			'find index number of item that received right 			'click			CurIndex = Y  CharHeight + .TopIndex			'If index number is valid then display information			If CurIndex < .ListCount Then				'Code to retrieve and display item definition				'goes here. Mine looks like this.				msg = GetMessage(.List(CurIndex))				frmInfo.Label1.Caption = msg				frmInfo.Show			End If		End If	End WithEnd Sub

If the items in your list box are always displayed in the same order and no deletions occur, then all you need is an array of definitions that correspond to each item in the list box. To retrieve the appropriate definition, use this code:

 	msg = DefinitionArray(CurIndex)

After displaying the definition in a window, you only need code for the list-box MouseUp event:

 	If Button = 2 Then frmInfo.Hide
Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as