Home > Software engineering >  How to only accept certain values inside a textbox
How to only accept certain values inside a textbox

Time:11-10

I made a flashcard application where the user can change the difficulty by entering a number inside a textbox.

Sub UpdateDifficultyLevel(front As String, difficulty As Integer)
    'The parameters are represented by question marks in the query
    Dim sql = "UPDATE flashcards SET difficulty = ?   
            WHERE Front = ?"
    'This using statement The Using statement makes sure that any "unmanaged resources" are released after they've been used.
    Using conn As New OleDbConnection("provider=microsoft.ACE.OLEDB.12.0;Data Source=flashcard login.accdb"), 'Establish connection
       cmd As New OleDbCommand(sql, conn)


        cmd.Parameters.Add("@difficulty", OleDbType.Integer).Value = difficulty 'Updates database with parameters
        cmd.Parameters.Add("@front", OleDbType.VarWChar).Value = front 'Updates database with parameters


        conn.Open() 'Opens connection
        cmd.ExecuteNonQuery() 'Executes

    End Using

End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim difficulty As Integer 'Sets difficulty as integer

    If Integer.TryParse(TxtDifficulty.Text, difficulty) Then
        Dim front = txtFront.Text 'Defines front as variable which is equal to txtfront.text
        UpdateDifficultyLevel(front, difficulty) 'Calls subroutine
    Else
        MsgBox("Please enter a number between 1 and 3") ' tells user that the difficulty must be a number
    End If
End Sub

This works where the user can only enter an integer but how would I make it so they can only enter an integer between 1 and 3

CodePudding user response:

You can utilize ErrorProvider to feedback to the user that the input is wrong. And reuse the validate function to get the parsed integer when used (so the logic is only in one place)

Private errorMessage As String = "Please enter a number between 1 and 3"

Private Function validateInput(ByRef difficulty As Integer) As Boolean
    Return Integer.TryParse(TxtDifficulty.Text, difficulty) AndAlso difficulty >= 1 AndAlso difficulty <= 3
End Function

Private Sub TxtDifficulty_TextChanged(sender As Object, e As EventArgs) Handles TxtDifficulty.TextChanged
    If Not validateInput(Nothing) Then
        ErrorProvider1.SetError(TxtDifficulty, errorMessage)
        TxtDifficulty.SelectAll()
    Else
        ErrorProvider1.SetError(TxtDifficulty, "")
    End If
End Sub

Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Dim difficulty As Integer 'Sets difficulty as integer
    If validateInput(difficulty) Then
        Dim front = txtFront.Text 'Defines front as variable which is equal to txtfront.text
        UpdateDifficultyLevel(front, difficulty) 'Calls subroutine
    Else
        MsgBox(errorMessage) ' tells user that the difficulty must be a number
    End If
End Sub

enter image description here

There are many other, possibly better, ways to do this however, but this sticks with your current design.

CodePudding user response:

Use a NumericUpDown control instead, with its Increment set to 1, MinValue set to 1 and MaxValue set to 3. It looks just like a textbox and, bonus, you can change the number within using the arrow keys

You can also consider a MaskedTextBox, RadioButtons, ComboBox, ListBox, even 3 different buttons to start an Easy, Medium or Hard game..

A big part of effective UI design is in using tools designed for purpose and not bothering the user with an endless succession of error messages that they have to read, understand and apply corrections for. A great example of that gone wrong is a cellphone with a Symbian OS; they were renown for bothering the user with incessant messages.

If you have an opportunity to design a UI so the user simply can't get it wrong, rather than shouting at them when they do, take it; your iPhone doesn't present a Qwerty keyboard when you're dialling a number..

  • Related