Trouble populating a ComboBox in a VB.NET Windows Form

Trouble populating a ComboBox in a VB.NET Windows Form

JBerns
Advisor Advisor
777 Views
5 Replies
Message 1 of 6

Trouble populating a ComboBox in a VB.NET Windows Form

JBerns
Advisor
Advisor

Community,

 

I am developing a VB.NET add-in (DLL) for AutoCAD.

 

I have a Windows Form with a ComboBox on it.

 

For debugging purposes, I have manually added items to the ComboBox within the main subroutine.

 

cboSelCust.Clear
cboSelCust.Items.Add("ABC")
cboSelCust.Items.Add("DEF")

During debugging, the ComboBox shows that it has two items:

2022-09-12_15-35-04.png

However, when I show the dialog box,

' Declare form and show it
Dim myForm As New MainForm
Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myForm)

the ComboBox is empty.

2022-09-12_15-31-50.png

 

I hope it is something simple that I have overlooked to get the names to show in the ComboBox.

 

Thanks for your time and attention. I look forward to your replies.

 

 

Regards,

Jerry

 

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
778 Views
5 Replies
Replies (5)
Message 2 of 6

_gile
Consultant
Consultant

Hi,

 

Try setting the selected index:

cboSelCust.SelectedIndex = 1

You should also pay attention to the ComboBox.DropDownStyle, it should be ComboBoxStyle.DropDownList if you do not want an editable text portion.

 



Gilles Chanteau
Programmation AutoCAD LISP/.NET
GileCAD
GitHub

0 Likes
Message 3 of 6

norman.yuan
Mentor
Mentor

Where these lines of code runs:

 

cboSelCust.Clear
cboSelCust.Items.Add("ABC")
cboSelCust.Items.Add("DEF")

 

That is, you'd better show more code where above lines are included.

 

Also, where "Select a customer" comes from in the combobox? If it is not entered by you after the form shows), then you must have code somewhere does that, which replaces your hard-coded 2 items in the combo box. So, you really examine your code carefully, or post all the code related to the form being populated.

 

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 4 of 6

JBerns
Advisor
Advisor

The majority of my code is functioning correctly, so I did not post all of it.

After posting this message, I did some more testing.

I double-clicked the form which created the MainForm_Load function. I thought the form was being initialized and loaded, but apparently not.

After placing this line of code:

cboSelCust.DataSource = lstCustomernames

in the MainForm_Load, the ComboBox list was populated as expected.

 

So my confusion is then how to create a user defined command (PDF_DXF2) and then open the dialog box when the command starts.

 

If it is of benefit, I will show the condensed version of the code I have so far, which is now working, but I am not sure if I am initializing and loading correctly.

 

Imports Autodesk.AutoCAD.ApplicationServices
Imports Autodesk.AutoCAD.DatabaseServices
Imports Autodesk.AutoCAD.Runtime
Imports Autodesk.AutoCAD.EditorInput
Imports Autodesk.AutoCAD.Geometry
Imports System.Windows.Forms
Imports System.Data.OleDb
Imports System.Data.Sql
Imports System.Data
Imports System.Data.SqlClient
Imports System.DateTime
Imports System.Drawing
Imports System.IO

Public Class MainForm

	Public strCustomerName As String
	Private lstCustomernames As New List(Of String)

	<CommandMethod("PDF_DXF2")>
	Public Sub IntializeUserForm()
		' Declare form and show it
		Dim myForm As New MainForm
		Autodesk.AutoCAD.ApplicationServices.Application.ShowModalDialog(myForm)
	End Sub

	Function GetPNCatalogs() As String
		' Returns a string, such as "ProNest14"
        	' This code is working
	End Function

	Sub GetCustomerNamesFromSql()
		' Declare the ProNest catalog name as a string
		Dim PNcatalog As String
		' Get the largest catalog from the ProNest database on the ENG server
		PNcatalog = GetPNCatalogs()
		' Gets customer names from a SQL database and stores the names in a List(of Strings) variable
        	'This code is working
	End Sub

	Private Sub BtnContinue_Click(sender As Object, e As EventArgs) Handles btnContinue.Click
		MsgBox("You selected the Continue button.")
	End Sub

	Private Sub BtnCancel_Click(sender As Object, e As EventArgs) Handles btnCancel.Click
		MsgBox("You selected the Cancel button.")
		Me.Close()
	End Sub


	Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
		' Get Customer data from SQL database
		GetCustomerNamesFromSql()

		' Assign customer names to ComboBox
		cboSelCust.DataSource = lstCustomernames

	End Sub

End Class

 

If you need to see the full VS project files, I can provide it.

 

Thank you all for your feedback.

 

Regards,

Jerry

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes
Message 5 of 6

norman.yuan
Mentor
Mentor

In your basic showing UI case, the rule of thumb is to separate the UI code (i.e. class MainForm here) from AutoCAD operation: the form should no nothing about whether it is called from AutoCAD or not. So, you do not put <CommandMethod> in the form class.

 

I usually structure my simple Acad plugin in this way:

 

MyCommands.cls - which is decorated with <Assembly: CommandClass()> and contains all methods decorated by <CommandMethod()>.

 

A few classes for specific AutoCAD operations, such as 

TitleBlockUpdate.cls

PdfCreation.cls

 

Then most likely one or more common utility classes

CadUtilities.cls

 

Then the UI classes

MainForm.cls

TitleBlockForm.cls

PdfOutputForm.cls

 

All the operations run from MyCommands.cls:

 

<CommandMethod("TitleBlock"> 

Public Shared Sub DoTitleBlock() 

  Dim tb As New TitleBlockUpdate()

  tb.DoWork() '' Which could show its own UI

End Sub

 

<CommandMethod("MyPdf")>

Public Shared Sub CreatePdfForClient()

  '' Get client name from MainForm

  Dim clientName As String

  Using frm As MainForm = New MainForm()

   If Application.ShowModalDialog(frm)==DialogResult.OK Then

     clientName=frm.SelectedCleint

   End If

  End Using

  If String.IsNullOrEmpty(clientName) Then Exit Sub

  '' Assume the PdfCreation class requires a client name in its constructor

  Dim pdtTool As New PdfCreation(clientName) 

  '' inside this process, more UIs can be showed up, but the MyCommands class does not need to know

  pdfTool.CreatePdfSet() 

End Sub

 

<CommandMethod("xxxxx")>

Public Shared Sub XXXXXXX()

  ... ...

End Sub

 

As you can see, each class does its own work and the MyCommands.cls is like a "switch board" to start each operation as a command by newing operation class and calling its exposed public method, but it does not need to know what the operation classes do.

 

Hope this gives some ideas.

Norman Yuan

Drive CAD With Code

EESignature

0 Likes
Message 6 of 6

JBerns
Advisor
Advisor

@norman.yuan and @_gile,

 

Thank you both for the information you provided. I hope to understand it more as I continue to learn AutoCAD .NET add-in development.

 

Regards,

Jerry  

-----------------------------------------------------------------------------------------
CAD Administrator
Using AutoCAD & Inventor 2025
Autodesk Certified Instructor
Autodesk Inventor 2020 Certified Professional
Autodesk AutoCAD 2017 Certified Professional
0 Likes