Results 1 to 5 of 5

Thread: Click event of button not working continually

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2023
    Posts
    1

    Click event of button not working continually

    Hello there,
    I am trying to do as below.
    On the click event of Button, if the button is already zero it will be 1 and will pass the string of 1.
    Now if I press the button again it will be zero and pass the string of 0.

    I have written as below but it's not working accurately and continually.
    Please help me with this.

    Try

    If TextBox5.Text = "Stop" Then
    TextBox5.Text = "Start"

    Frm_MDI.Timer_drum.Stop()
    If (Frm_MDI.SerialPort1.IsOpen) Then
    Frm_MDI.SerialPort1.Close()
    End If

    ':0110-00ED-0001-02-xxxx-xx-<CR><LF>
    SerialPort1.Open()
    SerialPort1.Write(":011000EC00010200013E" & vbCrLf)
    SerialPort1.Close()

    TextBox5.Text = "Start"

    Frm_MDI.Timer_drum.Start()

    ElseIf TextBox5.Text = "Start" Then
    TextBox5.Text = "Stop"

    Frm_MDI.Timer_drum.Stop()
    If (Frm_MDI.SerialPort1.IsOpen) Then
    Frm_MDI.SerialPort1.Close()
    End If

    SerialPort1.Open()
    SerialPort1.Write(":011000EC00010200003F" & vbCrLf)
    SerialPort1.Close()

    TextBox5.Text = "Stop"
    Frm_MDI.Timer_drum.Start()
    End If

    Catch ex As Exception
    MsgBox(ex.ToString, vbInformation, "DMSCADA_SPV315A")
    End Try

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Click event of button not working continually

    Please don't post unformatted code snippets. They are too hard to read, most notably because indenting is lost. Format your code properly, for readability.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Click event of button not working continually

    Here's your code formatted properly:
    Code:
            Try
    
                If TextBox5.Text = "Stop" Then
                    TextBox5.Text = "Start"
    
                    Frm_MDI.Timer_drum.Stop()
                    If (Frm_MDI.SerialPort1.IsOpen) Then
                        Frm_MDI.SerialPort1.Close()
                    End If
    
                    ':0110-00ED-0001-02-xxxx-xx-<CR><LF>
                    SerialPort1.Open()
                    SerialPort1.Write(":011000EC00010200013E" & vbCrLf)
                    SerialPort1.Close()
    
                    TextBox5.Text = "Start"
    
                    Frm_MDI.Timer_drum.Start()
    
                ElseIf TextBox5.Text = "Start" Then
                    TextBox5.Text = "Stop"
    
                    Frm_MDI.Timer_drum.Stop()
                    If (Frm_MDI.SerialPort1.IsOpen) Then
                        Frm_MDI.SerialPort1.Close()
                    End If
    
                    SerialPort1.Open()
                    SerialPort1.Write(":011000EC00010200003F" & vbCrLf)
                    SerialPort1.Close()
    
                    TextBox5.Text = "Stop"
                    Frm_MDI.Timer_drum.Start()
                End If
    
            Catch ex As Exception
                MsgBox(ex.ToString, vbInformation, "DMSCADA_SPV315A")
            End Try

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Click event of button not working continually

    Quote Originally Posted by Riya_vb View Post
    On the click event of Button, if the button is already zero it will be 1 and will pass the string of 1.
    Now if I press the button again it will be zero and pass the string of 0.
    Buttons are not numbers so they can't be 0 or 1 and I see nothing in that code that relates to any Button at all. Please provide a FULL and CLEAR explanation of the problem and provide ALL and ONLY the relevant information.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,302

    Re: Click event of button not working continually

    If you want to do two different things alternately on the Click of a Button then it is generally better to use a CheckBox instead. You can set the Appearance to Button and then the control looks exactly like a Button. When you click it, instead of depressing and releasing each time, as a Button does, the control will stay depressed on the first click and release on the second. You can then handle the CheckedChanged event and act based on the Checked property.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width