Resolved Reading Data from Textboxes

ravoll

Well-known member
Joined
Aug 26, 2010
Messages
50
Programming Experience
Beginner
Hi All ,
I'm still at it.Have a new delima

I have a form with 5 textboxes.1 thru 4 are read only ,and display various data recieved during my process.
#5 is a user input,and accepts only numbers.

View attachment Top Speed.bmp

Basicly if I enter 33 into textbox5,then "recieving" should stop when textbox 4 displays 33 KmH,but it don't for some reason.If I type it manually,(like I have with the frequency in the textbox1 event) it works.
Right now I stop my program when the Fx 's match but nee it to function over the KmH.
VB.NET:
Public Class Form1


    Dim RPM As Double
    Dim KmH As Double
    Dim Recieving As Boolean = False
    Dim cycles As Double  'cycles are impulses/6
    Dim impulse As Integer = 1
    Dim imp_count As Integer = 0
    Dim updatetext As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If SerialPort1.IsOpen = False Then
            SerialPort1.Open() 'open the port for access'  
            Button1.Text = "Stop"
            Button2.Enabled = True
        Else
            SerialPort1.Close()
            Button1.Text = "Start"
        End If
    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        If impulse > 0 Then
        End If
        If Recieving = True Then
            imp_count = imp_count + 1

            updatetext = imp_count
        End If
        Me.Invoke(New EventHandler(AddressOf TextUpdate))


    End Sub
    Private Sub TextUpdate(ByVal sender As Object, ByVal e As EventArgs)
        TextBox1.Text = updatetext
        TextBox2.Text = updatetext
        TextBox3.Text = updatetext
        TextBox4.Text = updatetext

    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        TextBox1.Text = imp_count
        If TextBox1.Text < 350 Then
            Recieving = True
        Else
            Recieving = False
        End If
    End Sub

    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        cycles = (imp_count / 6)
        TextBox2.Text = cycles


    End Sub
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        RPM = (cycles * 60)
        TextBox3.Text = RPM
    End Sub


    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        KmH = 5.1 * 3.141592654 * RPM / 100 * 0.06
        TextBox4.Text = KmH              'Displays speed

    End Sub


    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
    End Sub

    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        If Not IsNumeric(TextBox5.Text) Or TextBox5.Text = "" Then
            TextBox5.Text = "" 'prevents input other the number' 
            Button1.Enabled = False ' No input, disable button...' 
        Else
            Button1.Enabled = True ' Valid input, enable button...' 
        End If
    End Sub
End Class

Oh yeah and another thing.How can I manually clear the textboxes 1 thru 4 afterwards.The code I'm trying doesn't work,where as It has worked on other form's.
 
Never mind ,
I got it figured out.

still need help clearing the textboxes manually after wards.
 
There's 2 ways of doing that:
TextBox.Clear()
TextBox.Text = String.Empty

Thanks,
Neither of those work.I have used "TextBox.Clear()" in other forms before and it worked but not here:confused:

Could it be, because I have this in the update .text window?

Private Sub TextUpdate(ByVal sender As Object, ByVal e As EventArgs)
TextBox1.Text = updatetext
TextBox2.Text = updatetext
TextBox3.Text = updatetext
TextBox4.Text = updatetext

End Sub

If so how can I override it to accomplish this?

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
TextBox1.Clear()
TextBox2.Clear()
TextBox3.Clear()
TextBox4.Clear()
End Sub
 
TextBoxes don't have a TextUpdate event, there is a TextChanged event which would fire whenever you clear the text which would cause the old text to simply re-appear.
 
In the sub that handles when the text changes put this in:
VB.NET:
If TextBox.Text <> String.Empty Then TextBox.Text = updatetext
For each of the TextBoxes
 
In the sub that handles when the text changes put this in:
VB.NET:
If TextBox.Text <> String.Empty Then TextBox.Text = updatetext
For each of the TextBoxes

No luck,
If I do it like this:


VB.NET:
 Private Sub TextUpdate(ByVal sender As Object, ByVal e As EventArgs)
        TextBox1.Text = updatetext
        TextBox2.Text = updatetext
        TextBox3.Text = updatetext
        TextBox4.Text = updatetext
        If TextBox1.Text <> String.Empty Then
            TextBox1.Text = updatetext
        End If
        If TextBox2.Text <> String.Empty Then
            TextBox2.Text = updatetext
        End If
        If TextBox3.Text <> String.Empty Then
            TextBox3.Text = updatetext
        End If
        If TextBox4.Text <> String.Empty Then
            TextBox4.Text = updatetext
        End If
Have no change when I click button2

If I do this:
VB.NET:
 Private Sub TextUpdate(ByVal sender As Object, ByVal e As EventArgs)
        
        If TextBox1.Text <> String.Empty Then
            TextBox1.Text = updatetext
        End If
        If TextBox2.Text <> String.Empty Then
            TextBox2.Text = updatetext
        End If
        If TextBox3.Text <> String.Empty Then
            TextBox3.Text = updatetext
        End If
        If TextBox4.Text <> String.Empty Then
            TextBox4.Text = updatetext
        End If

    End Sub

All TextBoxes stay blank.
Or do you mean for me to put this in the serialport sub? That's where the "Me.Invoke(New EventHandler(AddressOf TextUpdate))"
is.
And by the way,all my textboxes are "read only" on form1.Is that the problem maybe?
 
Ok, so let me get this straight, when data comes in, the TextUpdate gets ran and the textboxes show that data. But you want to clear the textboxes too which isnt working (or always works) with the code I've given you, which is because data is still coming in and that sub still gets ran, right?

Conceptually you have a flaw if that above is correct because clearing a textbox when it's still showing data coming in is pointless, which means you should have your program not have data coming in & then clear them if you want those textboxes cleared. Or use a boolean to determine if those textboxes should show data or not.
 
It's like this.

I add into TextBox5 a speed,say 33 kMh,button2 is enabled.Press button 2 and recieving= true.Run the vehicle.The impulses from the dyno are counted,and translated to cycles,RPM,and Kmh,while being shown "real time" in the corresponding textboxes
(1 thru 4).when the Kmh (Textbox4) reaches 33 then recieving= false and everything stops.



Now all the textboxes are showing the last "counts" .Thats not a problem cause I need those numbers to work with,but when I want to run the program again ,it won't cause kmh is already at 33. I need to manually clear the textboxes of there values. first
 

Attachments

  • Top Speed.bmp
    820.7 KB · Views: 40
O.K. Got it to work like this:cool:

VB.NET:
Public Class Form1

    Dim resetValues As Boolean = False
    Dim RPM As Double
    Dim KmH As Double
    Dim Recieving As Boolean = False
    Dim cycles As Double  'cycles are impulses/6
    Dim impulse As Integer = 1
    Dim imp_count As Integer = 0
    Dim updatetext As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
       
    End Sub
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        resetValues = True
        imp_count = 0
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        resetValues = False
        Button2.Enabled = False
        Button1.Enabled = True
        TextBox5.ReadOnly = False
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If SerialPort1.IsOpen = False Then
            SerialPort1.Open() 'open the port for access'  
            Button1.Text = "Stop"
            Button2.Enabled = False
            TextBox5.ReadOnly = True
            Recieving = True
        Else
            SerialPort1.Close()
            Button2.Enabled = True
            Button1.Text = "Start"
            Button1.Enabled = False

        End If
    End Sub
    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
        If Recieving = True Then
            imp_count = imp_count + 1
        End If
        Me.Invoke(New EventHandler(AddressOf TextUpdate))
    End Sub
    Private Sub TextUpdate(ByVal sender As Object, ByVal e As EventArgs)
        TextBox1.Text = updatetext
        TextBox2.Text = updatetext
        TextBox3.Text = updatetext
        TextBox4.Text = updatetext
    End Sub
    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        If resetValues = False Then
            TextBox1.Text = imp_count
        End If
    End Sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
        If resetValues = False Then
            cycles = (imp_count / 6)
            TextBox2.Text = cycles
        End If
    End Sub
    Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
        If resetValues = False Then
            RPM = (cycles * 60)
            TextBox3.Text = RPM
        End If
    End Sub
    Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
        If resetValues = False Then
            KmH = 5.1 * 3.141592654 * RPM / 100 * 0.06
            TextBox4.Text = KmH              'Displays speed
        End If

        If KmH > TextBox5.Text Then
            Recieving = False

        End If
    End Sub
    Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
        If Not IsNumeric(TextBox5.Text) Or TextBox5.Text = "" Then
            TextBox5.Text = "" 'prevents input other the number' 
            Button1.Enabled = False ' No input, disable button...' 
            Button2.Enabled = False

        Else
            Button1.Enabled = True ' Valid input, enable button...' 
        End If
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.Close()
    End Sub
End Class

It may deviate from the normal programming practices a bit,but it works like a charm;)

How can I mark this thread as resolved?
 
To mark a thread as resolved you have to edit the first post (go to the advanced editor) and select the "resolved" option from the prefix drop down.

And it's good to hear that you have it working now.
 
Seem to have another teeny tiny problem.
I have things set up so that the program should start recording ,only after the first impulse is registered.
"Textbox1 > 0" then triggers a timer that samples textbox1 once pre interval and writes the value to a listbox.

Basicly ,when I start my vehicle on the dyno and impulse_count is > 0 it will start.
Thing is sometimes it starts right away, like I want it to,and sometimes it lags a fraction of a second.
When it lags there are already 5 to 10 pulses gone by before it starts logging 1,2,3,...and so on.

(On the upside as soon as a "1" is displayed in textbox1 the timer starts like it's supposed to).

I have my form setup so I can run "sets". Run one "set"...save...reset...run another "set" ...and compare.It's always the first "set" thats off.If I run multiple "sets" the first one is completly "whacked", and the rest are consistent.

Any ideas what cause this?

Think this could be SerialPort problem
 
Back
Top