MultiSelect ListBox help needed

MultiSelect ListBox help needed

_KarlH
Enthusiast Enthusiast
1,178 Views
3 Replies
Message 1 of 4

MultiSelect ListBox help needed

_KarlH
Enthusiast
Enthusiast

Hey all,

 

I'm trying to code up my first Multiselect ListBox, but am getting a little lost in the methods available for execution. Here's where I've started - the below simply pushes ViewReps into an array and displays them as an InputListBox

 

	Dim oAsmCompDef As AssemblyComponentDefinition = ThisApplication.ActiveDocument.ComponentDefinition
	Dim oViewRep 	As DesignViewRepresentation 'define view rep 
	Dim viewRepList 	As New ArrayList 'define an arraylist to hold the list of  view rep names

	For Each oViewRep In oAsmCompDef.RepresentationsManager.DesignViewRepresentations
					viewRepList.Add(oViewRep.Name)
					Logger.Info("For Each: " + oViewRep.Name)		
	Next	

	myValue = InputListBox("Select View Rep", viewRepList, State_List, Title := "View Reps", ListName := "Select View Rep")
	iProperties.Value("Custom", "Selected_ViewReps") = myValue
	iLogicVb.UpdateWhenDone = True

Unfortunately however it doesn't allow for user multi-selection (unless I'm mistaken?) - The user will be needing to process some ViewReps, but not all, so I figured that through a multiselect ListBox would be the easiest way for the user to progress with ViewRep selection, and their selections then execute via my main sub (not detailed here).

 

Researching the topic of Multiselect ListBox lead me to reading the below two articles;

 

https://knowledge.autodesk.com/support/inventor/learn-explore/caas/simplecontent/content/create-wind...

 

https://docs.microsoft.com/en-us/dotnet/api/system.windows.forms.listbox?view=windowsdesktop-6.0

 

So from these two articles, I can mash together a semi-functioning Multi-Select ListBox with string values, as per below;

 

_KarlH_1-1643095349619.png

 

 

However I've been unable to figure out how to inject my own viewRepList array list into my hacked up Autodesk / Microsoft article code example below;

 

AddReference "System.Drawing"
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class WinForm
	Inherits System.Windows.Forms.Form
	'declare any thing here that you want to use/access throughout all Subs & Functions
	Public oLargerFont As System.Drawing.Font = New Font("Arial",10)
	Public Sub New() 'creates the new instance
		oForm = Me
		With oForm
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 300
			.Height = 300
			.TopMost = True
			.Font = oLargerFont
			.Text = "Windows Form"
			.Name = "Windows Form"
			.ShowInTaskbar = False
		End With
		
		
		Dim oOptions As New List(Of String)
		oOptions.AddRange({"Option 1","Option 2","Option 3"})
		
		Dim oComboBox As New ListBox()
		With oComboBox
			'.DropDownStyle = ComboBoxStyle.DropDownList
			.Items.Clear()
			.Items.AddRange(oOptions.ToArray)
			.SelectedIndex = 1
			.SelectionMode = SelectionMode.MultiExtended
			'.Top = oCheckBox.Bottom + 10
			.Left = 25
			.Width = 150
		End With
		'AddHandler oComboBox.SelectionChangeCommitted, AddressOf oComboBox_SelectionChangeCommitted
		oForm.Controls.Add(oComboBox)
	End Sub

	Private Sub WinForm_FormClosing(ByVal oSender As Object, ByVal oFormCloseEvents As FormClosingEventArgs) Handles Me.FormClosing
	    If MsgBox("Are you sure you want to close this Form?",vbYesNo+vbQuestion, "CLOSE") = vbYes Then
	    Else
	      oFormCloseEvents.Cancel = True
	    End If
	 End Sub
	
	Private Sub oComboBox_SelectionChangeCommitted(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
		'oSender is a ComboBox
		MsgBox("You just chose " & oSender.Text & " from the ComboBox.",vbOKOnly+vbInformation,"EVENT FEEDBACK")
	End Sub
End Class

'This is the code that actually shows/runs the Form
Public Class RunMyForm
	Private Sub Main
		Dim oMyForm As New WinForm
		oMyForm.Show
	End Sub
End Class

 

 

I'm so very close to getting this working, but it's just a little bit deep-end of the pool for me, so to speak. Would appreciate any guidance, that's for sure! Or perhaps there's an easier way - such as, I have considered individual check-boxes as an alternative solution, but figured will try this MultiSelect ListBox method first.

 

Thanks!

0 Likes
Accepted solutions (1)
1,179 Views
3 Replies
Replies (3)
Message 2 of 4

nedeljko.sovljanski
Advocate
Advocate

Hi,

As I can see you need list of selected item to use in your code. In your method 

oComboBox_SelectionChangeCommitted

you have two inputs. oSender and oEventArgs. Second one (oEventArgs) should keep list of selected item. But also I see this line

'AddHandler oComboBox.SelectionChangeCommitted, AddressOf oComboBox_SelectionChangeCommitted

why is that commented? This mean you will never trigger event.

0 Likes
Message 3 of 4

JelteDeJong
Mentor
Mentor
Accepted solution

try this:

AddReference "System.Drawing"
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Public Class WinForm
	Inherits System.Windows.Forms.Form
	'declare any thing here that you want to use/access throughout all Subs & Functions
	Public oLargerFont As System.Drawing.Font = New Font("Arial", 10)
	Private oComboBox As New ListBox()
	Public Sub New(ViewRepList As List(Of String)) 'creates the new instance
		oForm = Me
		With oForm
			.FormBorderStyle = FormBorderStyle.FixedToolWindow
			.StartPosition = FormStartPosition.CenterScreen
			.Width = 300
			.Height = 300
			.TopMost = True
			.Font = oLargerFont
			.Text = "Windows Form"
			.Name = "Windows Form"
			.ShowInTaskbar = False
		End With
		
		
		Dim oOptions As New List(Of String)
		oOptions.AddRange(ViewRepList)
		
		
		With oComboBox
			'.DropDownStyle = ComboBoxStyle.DropDownList
			.Items.Clear()
			.Items.AddRange(oOptions.ToArray)
			.SelectedIndex = 1
			.SelectionMode = SelectionMode.MultiExtended
			'.Top = oCheckBox.Bottom + 10
			.Left = 25
			.Width = 150
			.Height = 100
		End With
		
		'AddHandler oComboBox.SelectionChangeCommitted, AddressOf oComboBox_SelectionChangeCommitted
		oForm.Controls.Add(oComboBox)
		
		Dim oButton1 As New Button()
		With oButton1
			.Text = "Ok"
			.Top = 120
			.Left = 25
			.Enabled = True
			.AutoSize = True
		End With
		oForm.Controls.Add(oButton1)
		AddHandler oButton1.Click, AddressOf Ok_Click
	End Sub

	Private Sub WinForm_FormClosing(ByVal oSender As Object, ByVal oFormCloseEvents As FormClosingEventArgs) Handles Me.FormClosing
'	    If MsgBox("Are you sure you want to close this Form?",vbYesNo+vbQuestion, "CLOSE") = vbYes Then
'	    Else
'	      oFormCloseEvents.Cancel = True
'	    End If
	 End Sub
	 
	 Private Sub Ok_Click(ByVal oSender As System.Object, ByVal oEventArgs As System.EventArgs)
	 	SelectedViewRepList = oComboBox.SelectedItems.Cast(Of String).ToList()
		' MsgBox("You just chose " & oSender.Text & " from the ComboBox.",vbOKOnly+vbInformation,"EVENT FEEDBACK")
		me.Close()
	End Sub

	Public Property SelectedViewRepList As List(Of String)	
End Class

'This is the code that actually shows/runs the Form
Public Class ThisRule
	Public Sub Main
		
		Dim doc As AssemblyDocument = ThisDoc.Document
        Dim names As List(Of String) = doc.ComponentDefinition.RepresentationsManager.DesignViewRepresentations.
            Cast(Of DesignViewRepresentation).Select(Function(d) d.Name).ToList()
				
		Dim oMyForm As New WinForm(names)
		oMyForm.ShowDialog()
		
		Dim joinedSeletcedNames = String.Join(" - ", oMyForm.SelectedViewRepList)
		MsgBox(joinedSeletcedNames)
		iProperties.Value("Custom", "Selected_ViewReps") = joinedSeletcedNames
	End Sub
End Class

Jelte de Jong
Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.

EESignature


Blog: hjalte.nl - github.com

Message 4 of 4

_KarlH
Enthusiast
Enthusiast

Yah, as I tried to reverse engineer the code, I was commenting out lines that I was yet to fully understand. I could see that the AddHandler was required, but it was conflicting with other inserted code as I tried to figure things out - and commenting out allowed me to at least capture the screenshot of what I was trying to achieve.

 

Massive shout-out to @JelteDeJong - that executes well! I will have a look through what you've crafted here to gain a better understanding. Would you happen to have any tutorials or learning resources to recommend?

0 Likes