Double-click to select from ListBox

Double-click to select from ListBox

karthur1
Mentor Mentor
479 Views
3 Replies
Message 1 of 4

Double-click to select from ListBox

karthur1
Mentor
Mentor

I am using the InputListBox command to display a list of choices. With this the user selects the item in the list and then clicks OK.

 

karthur1_1-1727364477327.png

 

karthur1_2-1727364533393.png

 

Is there a way to make it so that the user can double-click on the item in the list to select it? 

 

Thanks

 

 

0 Likes
480 Views
3 Replies
Replies (3)
Message 2 of 4

WCrihfield
Mentor
Mentor

Hi @karthur1.  I do not think we have that level of control over that dialog, as it is presented using that simple tool.  However, that is a dialog that is controlled by the iLogic add-in, but has a regular Windows Form behind it, and we do actually have a way to access that base Form, if we initialize that dialog in a slightly different way.  You should take a look at the following forum topic, where I posted some useful information on this subject.

https://forums.autodesk.com/t5/inventor-programming-ilogic/multiselectlistbox-based-on-ilogic-s-inpu... 

That will show you how to present the dialog differently, and how to access the Form behind it, then the ListBox control within that.  Once you have that, you can change how it works, or how it reacts to events, and can handle the MouseDoubleClick Event for that control.  It may be possible, but likely more complicated than what you had in mind.  I have not tried that one before yet myself either.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes
Message 3 of 4

Michael.Navara
Advisor
Advisor

You can't control default InputListBox, but you can easily define your own with full control of events mentioned by @WCrihfield

AddReference "System.Drawing"

Sub Main
	Dim listBox As New InputListBox2()
	listBox.Items = {"A", "B" }
	listBox.ShowDialog()
	MsgBox(listBox.SelectedItem)
End Sub

Class InputListBox2
	Inherits System.Windows.Forms.Form
	Public Sub New()
		InitializeComponent()
	End Sub

	Private _items As String()
	Public Property Items As String()
		Get
			Return _items
		End Get
		Set (value As String())
			_items = value
			lbItems.Items.Clear()
			lbItems.Items.AddRange(value)
		End Set
	End Property


	Public Property SelectedItem As String
		Get
			Return If (lbItems.SelectedItem Is Nothing, "", lbItems.SelectedItem.ToString())
		End Get
		Set (value As String)
			lbItems.SelectedItem = value
		End Set
	End Property

	Friend WithEvents lbItems As System.Windows.Forms.ListBox
	Friend WithEvents btnOk As System.Windows.Forms.Button
	Friend WithEvents TableLayoutPanel1 As System.Windows.Forms.TableLayoutPanel

	Private Sub InitializeComponent()
		Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
		Me.lbItems = New System.Windows.Forms.ListBox()
		Me.btnOk = New System.Windows.Forms.Button()
		Me.TableLayoutPanel1.SuspendLayout()
		Me.SuspendLayout()
		'
		'TableLayoutPanel1
		'
		Me.TableLayoutPanel1.ColumnCount = 1
		Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 100.0!))
		Me.TableLayoutPanel1.Controls.Add(Me.lbItems, 0, 0)
		Me.TableLayoutPanel1.Controls.Add(Me.btnOk, 0, 1)
		Me.TableLayoutPanel1.Dock = System.Windows.Forms.DockStyle.Fill
		Me.TableLayoutPanel1.Location = New System.Drawing.Point(0, 0)
		Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
		Me.TableLayoutPanel1.RowCount = 2
		Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 100.0!))
		Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Absolute, 29.0!))
		Me.TableLayoutPanel1.Size = New System.Drawing.Size(284, 261)
		Me.TableLayoutPanel1.TabIndex = 0
		'
		'lbItems
		'
		Me.lbItems.Dock = System.Windows.Forms.DockStyle.Fill
		Me.lbItems.FormattingEnabled = True
		Me.lbItems.Location = New System.Drawing.Point(3, 3)
		Me.lbItems.Name = "lbItems"
		Me.lbItems.Size = New System.Drawing.Size(278, 226)
		Me.lbItems.TabIndex = 0
		'
		'btnOk
		'
		Me.btnOk.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
		Me.btnOk.Location = New System.Drawing.Point(206, 235)
		Me.btnOk.Name = "btnOk"
		Me.btnOk.Size = New System.Drawing.Size(75, 23)
		Me.btnOk.TabIndex = 1
		Me.btnOk.Text = "OK"
		Me.btnOk.UseVisualStyleBackColor = True
		'
		'InputListBox2
		'
		Me.ClientSize = New System.Drawing.Size(284, 261)
		Me.Controls.Add(Me.TableLayoutPanel1)
		Me.Name = "InputListBox2"
		Me.TableLayoutPanel1.ResumeLayout(False)
		Me.ResumeLayout(False)

	End Sub

	Private Sub lbItems_DoubleClick(sender As Object, e As EventArgs) Handles lbItems.DoubleClick
		If Not String.IsNullOrEmpty(SelectedItem) Then
			DialogResult = DialogResult.OK
			Close()
		End If
	End Sub

	Private Sub btnOk_Click(sender As Object, e As EventArgs) Handles btnOk.Click
		DialogResult = DialogResult.OK
		Close()
	End Sub
End Class
Message 4 of 4

WCrihfield
Mentor
Mentor

I now agree that I do not think it will be possible to use the iLogic 'InputListBox' with DoubleClick working the way you seem to want it to work.  I think it would require having access permission that we do not have, and would also require a separate, custom event handler routine, which may simply be overridden by the dialog's own internal code designed behaviors.

 

However, the regular DoubleClick event does not appear to be the proper tool for this specific situation either, because it does not care where you double-clicked, as long as it was within the bounds of the entire ListBox area, not necessarily over one of the elements in its list.  Because it will allow me to select an 'element' in the list, then double-click somewhere else within the open area of the list, not over any items in the list, and it will simply return the previously selected item, even though you did not double click on a specific element in the list.  If you want this tool to only exit the dialog and return a value when you have actually double-clicked over a specific element in the list, then we will need to switch from the more general DoubleClick Event, to the more specific MouseDoubleClick Event, because this more specific one will give us the actual location of the double-click.  Then, it can use that location to get the 'index' of the element within the list, if it was actually over one of its elements, using its 'IndexFromPoint' Function.  Then that 'index' can be set to the ListBox.SelectedIndex property, if the 'index' returned was valid.  Then the event can set the value of the form's public SelectedItem property.

 

Below is the alternate EventHandler block of code that seemed to be working better for me.

Private Sub lbItems_MouseDoubleClick(sender As Object, mea As MouseEventArgs) Handles lbItems.MouseDoubleClick
	Dim iIndex As Integer = lbItems.IndexFromPoint(mea.Location)
	Try
		lbItems.SelectedIndex = iIndex
		Me.SelectedItem = lbItems.SelectedItem
	 Catch : End Try
	If Me.SelectedItem IsNot Nothing AndAlso Me.SelectedItem <> String.Empty Then
		Me.DialogResult = DialogResult.OK
		Me.Close()
	End If
End Sub

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

0 Likes