Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using VB22 to write an Amateur Radio Logging program. I have a Textbox to enter the Callsigns in. The Callsigns contains only one Number at the second or third character(ie: A1XXX or BB2XXX).

What I have tried:

I'm trying to switch a Boolean when a Number is Pressed or when the string "Contains" one. I'm currently trying "Select Case" for each character. I've tried "Try... Catch" and "If... Then" with no luck. I've searched everywhere and my brain is stuck in a loop! Any help would be appreciated. Thanks in advance!

I tried using a MaskedTextBox with the same results. It works fine on LL# but not L#L. I don't believe I'm handling the e.handled event correctly. I've tried with checking the Mask, setting a Boolean and Integer with the same outcome. Here's the Masked Code switching the Mask as it's entered...

Private Sub MaskedTextBox1_KeyPress(sender As Object, e As KeyPressEventArgs) Handles MaskedTextBox1.KeyPress
        intKBcount = tbxContact.Text.Length

        Select Case intKBcount
            Case 0
                If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) Then
                    e.Handled = True
                    If e.Handled = True Then
                        MaskedTextBox1.Mask = "L#"
                    End If
                End If
            Case 1
                If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) And Not Char.IsNumber(e.KeyChar) Then
                    e.Handled = True
                    If e.Handled = True And MaskedTextBox1.Text.Contains(0 - 9) Then
                        MaskedTextBox1.Mask = "L#L"
                    Else
                        MaskedTextBox1.Mask = "LL#"
                    End If
                End If
            Case 2
                'blnCkConNumb = False
                If MaskedTextBox1.Mask = "L#L" Then
                    If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) Then
                        e.Handled = True
                    End If
                    If e.Handled = True Then
                        MaskedTextBox1.Mask = "L#L???"
                    End If
                ElseIf MaskedTextBox1.Mask = "LL#" Then
                    If Not Char.IsControl(e.KeyChar) And Not Char.IsNumber(e.KeyChar) Then
                        e.Handled = True
                    End If
                    If e.Handled = True Then
                        MaskedTextBox1.Mask = "LL#???"
                    End If
                End If
            Case Else
                If Not Char.IsControl(e.KeyChar) And Not Char.IsLetter(e.KeyChar) Then
                    e.Handled = True
                End If
        End Select
End Sub
Posted
Updated 6-Feb-23 2:25am
v3
Comments
Richard MacCutchan 5-Feb-23 9:39am    
Sorry, we cannot guess what your code is doing. Please use the Improve question link above, and add complete details in the question.

Why reinvent the wheel? There is a control that helps you do this - the MaskedTextBox Class (System.Windows.Forms)[^]

The link includes sample code.
 
Share this answer
 
v2
You could try using a regex (See below)

VB
Dim regex As Regex = New Regex("^[A-Za-z]{1,2}\d[A-Za-z]{3}$")

Dim match As Match

match = regex.Match("A1XXX")

If (match.Success) Then
    Console.WriteLine("Matched")
End If

match = regex.Match("AA1XXX")

If (match.Success) Then
    Console.WriteLine("Matched")
End If
 
Share this answer
 
My solution was to just write my own Validation Sub after Enter is submitted instead of validating each character. Thanks for the input. I've learned a few new things since I started out on VB6 in 1997. I guess I'm just a little rusting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900