problem with visualbasic 2010 coding

(Please use code tags (# button in the editor toolbar above) when posting code.)

Please have a look at this (corrected) version of the code:

Private Sub Button1_Click(ByVal sender As Object, ByVal _e As EventArgs) Handles Button1.Click
    If Button1.Text = "Close" Then
        SerialPort1.Close()
        Button1.Text = "Open"
    Else
        If Not SerialPort1.IsOpen Then
            SerialPort1.Open()
            Button1.Text = " Close"
        End If
    End if
End Sub

Let's try to explain it in pseudocode:

if button1 label is "Close" then (A)
close the serial port
change button1 label to "Open"
else
if serial port is not already open
open it
change button1 label to "Close" (B)
endif
endif

(B) If the string assigned here is not exactly equal to that tested in (A) the code won't work.

In the code fragment posted earlier, in (B) you have " Close", with an extra space before the first letter. That's not going to be equal to "Close" in (A), so the IF will always fail.

I hope it's clearer now.