Serial Ports

  • Thread starter Thread starter HardySpicer
  • Start date Start date
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
 
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
 
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
 
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.
 
Back
Top