I have made an application to add monthly usages of coffee to a listbox using an array. I have a button which opens up an InputBox to enter the monthly usages. I then click a button to display the usages in a listbox. Howerver, I only want users to be able to add numbers to the listbox so when a user clicks enters a non-numeric value or enters nothing at all and clicks OK, a MsgBox appears. I have tried many different ways but none seem to work. Please help!

Public Class frmCostaLot
Dim MonthlyUsages(11) As Single
Private Sub lstUsageAmount_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstUsageAmount.SelectedIndexChanged

End Sub

Private Sub btnEnterUsages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterUsages.Click
    Dim usages As Integer
    For usages = 0 To 11
        MonthlyUsages(usages) += InputBox("Please enter a monthly usage value")
    Next
End Sub

Private Sub btnShowUsages_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowUsages.Click
    Dim usages As Integer
    For usages = 0 To 11
        lstUsageAmount.Items.Add(MonthlyUsages(usages))
    Next

End Sub

What ways have you tried? It helps is to know everything otherwise we'll just tell you to try the same stuff over and over...

Did you try testing the value entered in the keypress event?

You can use the isnumeric function

do 
   MonthUsage = InputBox("Please enter a monthly usage value")
   if not isnumeric(MonthUsage) then msgbox "Please enter numbers only"
until isNumeric(MonthUsage)
MonthlyUsages(usages) += val(MonthUsage)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.