Serial Ports

H

HardySpicer

I have

' Get serial com port names
For i = 0 To My.Computer.Ports.SerialPortNames.Count - 1

cboCommPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
cboCommPorts.SelectedIndex = 0

where 0 is used as the default. However there are many new PCs that
don't have a serial port at all and the software fails. How do I put
code in here so as to check if teh serial port exists in the first
place? Then how do I default the combo box?

Hardy
 
A

Armin Zingler

Am 27.10.2010 11:55, schrieb HardySpicer:
I have

' Get serial com port names
For i = 0 To My.Computer.Ports.SerialPortNames.Count - 1

cboCommPorts.Items.Add(My.Computer.Ports.SerialPortNames(i))
Next
cboCommPorts.SelectedIndex = 0

where 0 is used as the default. However there are many new PCs that
don't have a serial port at all and the software fails. How do I put
code in here so as to check if teh serial port exists in the first
place? Then how do I default the combo box?

I'd disable the combo if there are no ports:

Dim names = System.IO.Ports.SerialPort.GetPortNames

If names.Length = 0 Then
combo.Enabled = False
Else
combo.Items.AddRange(names)
combo.SelectedIndex = 0
End If
 
H

HardySpicer

Am 27.10.2010 11:55, schrieb HardySpicer:





I'd disable the combo if there are no ports:

     Dim names = System.IO.Ports.SerialPort.GetPortNames

      If names.Length = 0 Then
         combo.Enabled = False
      Else
         combo.Items.AddRange(names)
         combo.SelectedIndex = 0
      End If

Works fine too! Thanks


Hardy
 
R

Robert Roland

I'd disable the combo if there are no ports:

Please remember to also let the user know *why* the control is
disabled. It is extremely frustrating for a user to be presented with
a disabled control, and no explanation as to why it is not available.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top