Click here to Skip to main content
15,891,664 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Greetings! I'm currently making a project involving USB Modem and Visual Basic. I created a ComboBox wherein it should show all available ports. Here's my code for the ComboBox.

VB
Imports System
Imports System.IO.Ports
Imports System.Threading

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim ports As String() = SerialPort.GetPortNames
            Dim port As String
            For Each port In ports
                ComboBox1.Items.Add(port)

            Next port
            ComboBox1.SelectedIndex = 0
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class


And I keep getting this error:

InvalidArgument=Value of '0' is not valid for 'SelectedIndex'
Parameter name: SelectedIndex

I don't know where I did wrong. Please help. Thank you!
Posted

1 solution

Try reading the documentation on the ComboBox[^]. If there are no items in the ComboBox, 0 is an invalid index.

If there are no items in the Combo, the only valid index value is -1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900