user3163534
user3163534

Reputation: 23

Program returns error when using 9+ digits

I've been working on a bar-code like thigny. It writes numbers onto an image using colors. Anyways, everytime I use more than 9 numbers, it says that the OverFlowException was unhandled. Maybe I missed something in my code. Most of it is my code, except for the one about the TextBox1 KeyPress. Here's the code

Public Class Form1

Dim NumberLength As Integer = 1

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles     Button1.Click
    If Button2.Text = "..." Then
        MsgBox("Place select where you want your Color Code to be saved")
    Else
            Dim BMP As New Drawing.Bitmap(19, 10)
            BMP = Create(TextBox1.Text)
            Dim NewBMP As New Drawing.Bitmap(NumberLength, 1)
            NewBMP = BMP
            NewBMP.Save(Button2.Text)
        End If
End Sub

Public Function Create(ByVal Number As Int32) As Bitmap
    Dim StringNumber = Number.ToString
    Dim NumberLength = StringNumber.Length
    Dim ColorCode As New Drawing.Bitmap((NumberLength), 1)
    Dim GFX As Graphics = Graphics.FromImage(ColorCode)

    For i As Integer = 1 To NumberLength
        Dim CurrentNumber As Integer = StringNumber.Substring(i - 1, 1)
        GFX.FillRectangle(New SolidBrush(ReturnRGBValue(CurrentNumber)), i, 0, 1, 1)
        If i = (NumberLength + 1) Then
            Exit For
        End If
    Next

    Return ColorCode
End Function

Public Function ReturnRGBValue(ByVal Number As Integer) As Color
    Dim Color As New Color
    If Number = 1 Then
        Color = Color.FromArgb(255, 255, 255)
    ElseIf Number = 2 Then
        Color = Color.FromArgb(245, 245, 245)
    ElseIf Number = 3 Then
        Color = Color.FromArgb(235, 235, 235)
    ElseIf Number = 4 Then
        Color = Color.FromArgb(225, 225, 225)
    ElseIf Number = 5 Then
        Color = Color.FromArgb(215, 215, 215)
    ElseIf Number = 6 Then
        Color = Color.FromArgb(205, 205, 205)
    ElseIf Number = 7 Then
        Color = Color.FromArgb(195, 195, 195)
    ElseIf Number = 8 Then
        Color = Color.FromArgb(185, 185, 185)
    ElseIf Number = 9 Then
        Color = Color.FromArgb(175, 175, 175)
    ElseIf Number = 0 Then
        Color = Color.FromArgb(165, 165, 165)
    End If

    Return Color
End Function

Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
    Dim Save As New SaveFileDialog
    Save.Filter = "PNG |*.png |JPG |*.jpg |GIF |*.gif"
    Save.ShowDialog()
    Button2.Text = Save.FileName
End Sub

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress

    '97 - 122 = Ascii codes for simple letters
    '65 - 90  = Ascii codes for capital letters
    '48 - 57  = Ascii codes for numbers

    If Asc(e.KeyChar) <> 8 Then
        If Asc(e.KeyChar) < 48 Or Asc(e.KeyChar) > 57 Then
            e.Handled = True
        End If
    End If

End Sub


End Class

Upvotes: 0

Views: 45

Answers (2)

bumbumpaw
bumbumpaw

Reputation: 2528

The valid range of an Integer variable is -2147483648 through +2147483647.
Dim k As Integer
The following statement causes an error because the value is too large.
k = 2147483648

You are using Integer, so if its exist to 9, its overflow, try to use Long data type see tihs.

Upvotes: 2

spencer7593
spencer7593

Reputation: 108480

Your argument is declared Int32

The maximum value that can be stored in an Int32 (according to MSDN) decimal 2,147,483,647 (hexadecimal value 0x7FFFFFFF)

Upvotes: 1

Related Questions