Dominik Rivard
Dominik Rivard

Reputation: 11

Can't enter more than 9 numbers or remove all entered numbers in a textbox

So i'm making a program for school that's supposed to convert temperatures from Celsius to farenheit and vice-versa. The program works fine but when i try to enter more than 9 numbers in the textbox it crashes. The same problem occurs when i try to backspace and delete numbers previously entered. Can anyone tell me why this is happening?

Private Sub txtEntré_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtEntré.KeyPress
    'Laisse seulement tapper des chiffres dans la boîte de texte
    If cbEntré.SelectedIndex < 0 Then
        txtEntré.ReadOnly = True
    Else
        txtEntré.ReadOnly = False

    End If
    If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
       AndAlso e.KeyChar <> ControlChars.Back Then
        e.Handled = True
    End If


End Sub

Private Sub cb1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cbEntré.SelectedIndexChanged
    If cbEntré.SelectedIndex = 1 Then
        cbSortie.SelectedIndex = 0
    ElseIf cbEntré.SelectedIndex = 0 Then
        cbSortie.SelectedIndex = 1
    End If
End Sub

Private Sub txtEntré_TextChanged(sender As Object, e As EventArgs) Handles txtEntré.TextChanged

    Dim Entré As Integer
    Convert.ToInt64(txtEntré.Text)

    If cbEntré.SelectedIndex = 0 Then
        txtSortie.Text = (Entré * 9 / 5) + 32
    ElseIf cbEntré.SelectedIndex = 1 Then
        txtSortie.Text = (Entré - 32) * 5 / 9
    End If

End Sub

End Class

Upvotes: 1

Views: 44

Answers (2)

Joel Coehoorn
Joel Coehoorn

Reputation: 416149

I'm looking specifically at the method below:

Private Sub txtEntré_TextChanged(sender As Object, e As EventArgs) Handles txtEntré.TextChanged

    Dim Entré As Integer
    Convert.ToInt64(txtEntré.Text)

    If cbEntré.SelectedIndex = 0 Then
        txtSortie.Text = (Entré * 9 / 5) + 32
    ElseIf cbEntré.SelectedIndex = 1 Then
        txtSortie.Text = (Entré - 32) * 5 / 9
    End If

End Sub

First of all, this line:

Dim Entré As Integer

This is a 32-bit value.

But now the next line is trying to create a 64-bit value:

Convert.ToInt64(txtEntré.Text)

Additionally, the result is not assigned anywhere, leaving Entré unchanged from the default value of 0.

Either change Entré to use a Long type, or change this code to use Convert.ToInt32() (or, even better, Integer.TryParse()).

Given you're dealing with temperature values, I recommend the latter option. I doubt you need to worry about temperatures greater than the 2+ billion degrees you can represent with a basic integer. Not even the core of the Sun gets that hot. You can set the MaxLength property of the TextBox to 8 to keep safe.

Either way, don't forget to the assign the result to your variable:

Private Sub txtEntré_TextChanged(sender As Object, e As EventArgs) Handles txtEntré.TextChanged    
    Dim Entré As Integer 
    If Not Integer.TryParse(txtEntré.Text, Entré) Then Exit Sub

    If cbEntré.SelectedIndex = 0 Then
        txtSortie.Text = ((Entré * 9 / 5) + 32).ToString()
    ElseIf cbEntré.SelectedIndex = 1 Then
        txtSortie.Text = ((Entré - 32) * 5 / 9).ToString()
    End If
End Sub

You should also turn on Option Strict.

Upvotes: 1

the_lotus
the_lotus

Reputation: 12748

You code seem to be missing something and you haven't told us the error. I'll assume that you put the data from the textbox into Entré. Since Entré is an integer, it's range of value is -2,147,483,648 to 2,147,483,647. Any number beyond that range will fail. That must be why you can't go beyond 9 character (well.. 1 as the first character should work).

If you change your variable to an Int64, then the range is -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 which is much bigger.

Upvotes: 1

Related Questions