I'm trying to add a number to a listbox that a user enters into a textbox. The program crashes when I press the button.

Public Class Form1

    Dim numbers() As Integer
    Dim numNumbers As Integer = 0

    Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = NumbersOnly(e.KeyChar, TextBox1)
    End Sub
    Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
        'validate the entry for a textbox limiting it to only numeric values and the decimal point
        If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
        If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
            Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
        End If
        Return False 'for backspace
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If TextBox1.Text <> "" Then
            numNumbers = numNumbers + 1
            numbers(numNumbers) = TextBox1.Text
            ListBox1.Items.Add(numbers(numNumbers).ToString())
        Else
            numNumbers = numNumbers + 0
        End If
    End Sub
End Class

Edited 5 Years Ago by SolidSora: n/a

Not quite clear what you are trying to do with the numbers() and numNumbers, but using the ReDim Statement should help.

Private numbers() As Double '// changed to Double since TextBox allows ".".
    Private numNumbers As Integer = 0

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Not TextBox1.Text = "" Then
            ListBox1.Items.Add(TextBox1.Text)
            ReDim Preserve numbers(numNumbers)
            numbers(numNumbers) = CDbl(TextBox1.Text)
            numNumbers += 1
        End If
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        '// For testing.
        MsgBox(numbers(ListBox1.SelectedIndex).ToString) '// display the number added to your numbers Array.
    End Sub

Edited 5 Years Ago by codeorder: n/a

If i understood you correctly, all you have to do with the addButton_clicked event is this

Private Sub addButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles addButton.Click

        ListBox1.Items.Add(TextBox1.Text)

    End Sub

Then for the numeric validation for the textbox, simply check with an if statement, example:

If Not IsNumeric(textbox1.Text) Then
             MessageBox.Show("Non-numeric value entered. Please try again.", "User Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
            
             textbox1.Focus()
            
              Exit Sub
         
           End If

I dont know if this is what you're looking for. Hope it helps anyway.

Edited 5 Years Ago by e_ayt: n/a

This article has been dead for over six months. Start a new discussion instead.