Click here to Skip to main content
15,885,537 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: Value of type 'String' cannot be converted to 'System.Windows.Forms.Control' Pin
Richard MacCutchan9-Apr-14 23:30
mveRichard MacCutchan9-Apr-14 23:30 
AnswerRe: Value of type 'String' cannot be converted to 'System.Windows.Forms.Control' Pin
Dave Kreskowiak10-Apr-14 2:18
mveDave Kreskowiak10-Apr-14 2:18 
QuestionOverload resolution failed because no accessible 'Add' accepts this number of arguments Pin
Ankush Seth9-Apr-14 23:21
Ankush Seth9-Apr-14 23:21 
AnswerRe: Overload resolution failed because no accessible 'Add' accepts this number of arguments Pin
Richard MacCutchan9-Apr-14 23:27
mveRichard MacCutchan9-Apr-14 23:27 
GeneralRe: Overload resolution failed because no accessible 'Add' accepts this number of arguments Pin
Ankush Seth9-Apr-14 23:35
Ankush Seth9-Apr-14 23:35 
GeneralRe: Overload resolution failed because no accessible 'Add' accepts this number of arguments Pin
Richard MacCutchan9-Apr-14 23:47
mveRichard MacCutchan9-Apr-14 23:47 
GeneralRe: Overload resolution failed because no accessible 'Add' accepts this number of arguments Pin
Ankush Seth10-Apr-14 0:10
Ankush Seth10-Apr-14 0:10 
QuestionHow do I Rectify the Closure of a Form Pin
Member 107017019-Apr-14 7:01
Member 107017019-Apr-14 7:01 
Hi All,
Please find my code below to read the output of two independent temperatures streams.

The form now works well but I cannot close the form when I use the button btnDisconnect_Click at the bottom of the code. The only way I can stop the code is by the stop debugging.

Please can you help

Thank you in advance

Nevjc

VB
Imports System.Drawing.Drawing2D
Imports System.IO.Ports

Public Class Form1
    Dim bmp1 As Bitmap = New Bitmap(165, 550)
    Dim temperature1 As Double = 10
    Dim temperature2 As Double = 10
    Dim WithEvents sp As New SerialPort

    Private Sub pictherm1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles pictherm1.Paint
        Dim g As Graphics = e.Graphics
        g.Clear(Color.White)
        ' fill temperature1
        If temperature1 > 100 Then temperature1 = 100
        g.FillRectangle(Brushes.Green, 48, 525 - CInt(temperature1 * 5), 10, CInt(temperature1 * 5))
        ' fill temperature2
        If temperature2 > 100 Then temperature2 = 100
        g.FillRectangle(Brushes.Green, 108, 525 - CInt(temperature2 * 5), 10, CInt(temperature2 * 5))
        'draw scale
        g.DrawLine(Pens.Black, 60, 525, 60, 25)
        g.DrawLine(Pens.Black, 105, 525, 105, 25)
        ' minor ticks
        For i As Integer = 25 To 525 Step 5
            g.DrawLine(Pens.Black, 50, i, 60, i)
            g.DrawLine(Pens.Black, 105, i, 115, i)
        Next
        'major ticks
        Dim f As Font = New Font("Verdana", 10, FontStyle.Regular)
        Dim scale As Integer
        For i As Integer = 525 To 25 Step -25
            g.DrawLine(Pens.Black, 45, i, 60, i)
            g.DrawLine(Pens.Black, 105, i, 120, i)
            scale = (525 - i) / 5
            g.DrawString(Str(scale), f, Brushes.Black, 65, i - 8)
        Next
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        Try
            sp.BaudRate = "9600"
            sp.PortName = "COM5"
            sp.Open()
            If sp.IsOpen Then
                btnConnect.Visible = False
                btnDisconnect.Visible = True
            End If
        Catch
            sp.Close()
        End Try
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        If sp.IsOpen() Then
            MessageBox.Show("Disconnect before closing")
            e.Cancel = True
        End If
    End Sub

    Delegate Sub myMethodDelegate1(ByVal [text] As String)
    Dim myDelegate1 As New myMethodDelegate1(AddressOf ProcessReading1)

    Private Sub SerialPort_DataReceived1(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
        Dim str1 As String = sp.ReadLine()
        Dim firststr As String = Mid(str1, 1, 4)
        Invoke(myDelegate1, firststr)
    End Sub

    Sub ProcessReading1(ByVal input As String)
        If IsNumeric(input) Then
            temperature1 = CDbl(input)
            lblLeft.Text = CDbl(temperature1)
            pictherm1.Refresh()
        End If
    End Sub

    Delegate Sub myMethodDelegate2(ByVal [text] As String)
    Dim myDelegate2 As New myMethodDelegate2(AddressOf ProcessReading2)

    Private Sub SerialPort_DataReceived2(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles sp.DataReceived
        Dim str2 As String = sp.ReadLine()
        Dim secondstr As String = Mid(str2, 6, 5)
        Invoke(myDelegate2, secondstr)
    End Sub

    Sub ProcessReading2(ByVal input As String)
        If IsNumeric(input) Then
            temperature2 = CDbl(input)
            lblRight.Text = CDbl(temperature2)
            lblTotal.Text = CDbl(temperature1 + temperature2)
            lblDiff.Text = CDbl(temperature1 - temperature2)
            pictherm1.Refresh()
        End If
    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        Try
            sp.Close()
            btnConnect.Visible = True
            btnDisconnect.Visible = False
            Exit Sub
        Catch
            MessageBox.Show("Some kind of problem.")
        End Try
    End Sub

End Class

AnswerRe: How do I Rectify the Closure of a Form Pin
Bernhard Hiller9-Apr-14 22:11
Bernhard Hiller9-Apr-14 22:11 
GeneralRe: How do I Rectify the Closure of a Form Pin
Member 107017019-Apr-14 23:21
Member 107017019-Apr-14 23:21 
AnswerRe: How do I Rectify the Closure of a Form Pin
Alan N10-Apr-14 2:07
Alan N10-Apr-14 2:07 
GeneralRe: How do I Rectify the Closure of a Form Pin
Member 1070170110-Apr-14 2:49
Member 1070170110-Apr-14 2:49 
Question[vb2008] Chart from CSV file Pin
Member 107354929-Apr-14 5:40
Member 107354929-Apr-14 5:40 
AnswerRe: [vb2008] Chart from CSV file Pin
Richard MacCutchan9-Apr-14 6:29
mveRichard MacCutchan9-Apr-14 6:29 
AnswerRe: [vb2008] Chart from CSV file Pin
Eddy Vluggen9-Apr-14 6:31
professionalEddy Vluggen9-Apr-14 6:31 
QuestionGetting error 'AddressOf' expression cannot be converted to 'Integer' Pin
Ankush Seth7-Apr-14 23:27
Ankush Seth7-Apr-14 23:27 
AnswerRe: Getting error 'AddressOf' expression cannot be converted to 'Integer' Pin
Richard Deeming8-Apr-14 2:25
mveRichard Deeming8-Apr-14 2:25 
QuestionError : Property is readonly Pin
dilkonika6-Apr-14 8:42
dilkonika6-Apr-14 8:42 
AnswerRe: Error : Property is readonly Pin
Bernhard Hiller6-Apr-14 21:24
Bernhard Hiller6-Apr-14 21:24 
GeneralRe: Error : Property is readonly Pin
dilkonika7-Apr-14 2:50
dilkonika7-Apr-14 2:50 
GeneralRe: Error : Property is readonly Pin
Eddy Vluggen7-Apr-14 7:22
professionalEddy Vluggen7-Apr-14 7:22 
GeneralRe: Error : Property is readonly Pin
dilkonika7-Apr-14 7:49
dilkonika7-Apr-14 7:49 
GeneralRe: Error : Property is readonly Pin
Eddy Vluggen7-Apr-14 8:53
professionalEddy Vluggen7-Apr-14 8:53 
GeneralRe: Error : Property is readonly Pin
dilkonika7-Apr-14 9:44
dilkonika7-Apr-14 9:44 
GeneralRe: Error : Property is readonly Pin
Eddy Vluggen8-Apr-14 8:01
professionalEddy Vluggen8-Apr-14 8:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.