0

When compiling my project made in VS14 in VS2017 I get the Exception Unhandled as shown in the picture "Value off 0 is not valid for index." is the translation of the swedish part.. I'm not all to familiar with VB, haven't touched it since VB98, and that was a long time ago.

Picture

Public Class frmMain
    Dim myPort As Array
    Delegate Sub SetTextCallback(ByVal [text] As String)

    'Page Load Code Starts Here....

    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        myPort = IO.Ports.SerialPort.GetPortNames()
        cmbBaud.Items.Add(9600)
        cmbBaud.Items.Add(19200)
        cmbBaud.Items.Add(38400)
        cmbBaud.Items.Add(57600)
        cmbBaud.Items.Add(115200)
        For i = 0 To UBound(myPort)
            cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0)
        cmbBaud.Text = cmbBaud.Items.Item(0)
        btnDisconnect.Enabled = False
    End Sub
    'Page Load Code Ends Here ....

    '------------------------

    'Connect Button Code Starts Here ....
    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        SerialPort1.PortName = cmbPort.Text
        SerialPort1.BaudRate = cmbBaud.Text
        SerialPort1.Parity = IO.Ports.Parity.None
        SerialPort1.StopBits = IO.Ports.StopBits.One
        SerialPort1.DataBits = 8
        SerialPort1.Open()
        btnConnect.Enabled = False
        btnDisconnect.Enabled = True
        If SerialPort1.IsOpen = True Then
            lblConnected.Visible = True
            lblDisconnected.Visible = False
        Else
            lblConnected.Visible = False
            lblDisconnected.Visible = True
        End If

    End Sub
    'Connect Button Code Ends Here ....
Slangfil
  • 23
  • 6
  • Yes, shortly after posting I suspected the issue was that I had no COM port active. How can I stop this error from happening when COM = null? Nothing connected at all that is. – Slangfil Jan 21 '21 at 08:23
  • So, you're saying that you only want to do something IF a particular condition is satisfied? I wonder if there's something in the VB language that can do that. – jmcilhinney Jan 21 '21 at 08:44
  • Thank you jmcilhinney, your comment made me realize my dumbassery.. Wee bit embarrassed but atleast it's working now. If (myPort IsNot Nothing AndAlso myPort.Length > 0) Then – Slangfil Jan 21 '21 at 09:42

0 Answers0