How Do I check if a Masked Text Box is Null or Empty ? (WinForms. VB.Net)

Gary Simpson 471 Reputation points
2023-06-02T10:29:12.3866667+00:00

Hi Good People

How do I check if the Masked Textbox is Null or Empty, As the Masked Textbox has Symbols if no Numbers are typed into the Masked Textbox. I have tried to write code for a empty Masked Textbox to no avail.

The Symbols are setup as:

Number, Number, Space, Symbol (-) Space, Number, Number, Space, Symbol (-) Space, Number, Number

Picture 1 denotes an empty Masked Textbox.

Picture 2 denotes a Filled Masked Textbox.

Mask Text Box1

Mask Text Box2

Kind Regards

Gary

Developer technologies | VB
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2023-06-02T15:17:01.9133333+00:00

    Hi

    Here is a variation. This will stop user from moving to next MTB if current one is not complete, and will auto move to next one if complete.

    ' MaskedTextBoxes
    Option Strict On
    Option Explicit On
    Public Class Form1
    	WithEvents mtb As MaskedTextBox
    	WithEvents tb As New TextBox
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' set up some (4) maskedtextboxes
    		Dim x As Integer = 10
    		Dim y As Integer = 20
    		For i As Integer = 1 To 4
    			mtb = New MaskedTextBox
    			With mtb
    				.Name = "MaskedTextBox" & i.ToString
    				.BackColor = Color.Pink
    				.Tag = i
    				.Mask = "00 - 00 - 00"
    				.Font = New Font(.Font.FontFamily, 18)
    				.TextAlign = HorizontalAlignment.Center
    				.Location = New Point(10, y)
    				.Width = 200
    				y += 44
    				AddHandler .Validated, AddressOf MaskedTextBox_Validated
    				AddHandler .KeyUp, AddressOf MaskedTextBox_KeyUp
    			End With
    			Controls.Add(mtb)
    		Next
    
    		' add a random textbox
    		With tb
    			.Font = New Font(.Font.FontFamily, 18)
    			.Text = "Some random text"
    			.Location = New Point(10, y + 10)
    			.BackColor = Color.WhiteSmoke
    			.Width = 200
    		End With
    		Controls.Add(tb)
    	End Sub
    	Private Sub MaskedTextBox_Validated(sender As Object, e As EventArgs)
    		' checks if current MTB is complete
    		' if not, remain there.
    		Dim mtb As MaskedTextBox = DirectCast(sender, MaskedTextBox)
    		tb.Text = String.Empty
    		If Not mtb.MaskFull Then
    			tb.Text = mtb.Text
    			MessageBox.Show(mtb.Name & " Needs full Sort Code")
    			mtb.Select()
    		End If
    	End Sub
    
    	Private Sub MaskedTextBox_KeyUp(sender As Object, e As KeyEventArgs)
    		' checks if current MTB is full and
    		' if so, moves to next which
    		' ends at the random TextBox
    		Dim mtb As MaskedTextBox = DirectCast(sender, MaskedTextBox)
    		If mtb.MaskFull Then
    			SendKeys.Send(vbTab)
    		End If
    	End Sub
    End Class
    
    

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2023-06-02T12:46:24.7433333+00:00

    Hi

    Here is a test example which demonstrates all the features I think you want. It is set to add multiple MaskedTextBoxes, and to auto jump to next when each one is completed and jump to 'regular' textbox hen all are completed. Just to illustrate use of the MaskedTextBox.MaskCompleted event.

    ' MaskedTextBoxes
    Option Strict On
    Option Explicit On
    Public Class Form1
    	WithEvents mtb As MaskedTextBox
    	WithEvents tb As New TextBox
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' set up some (4) maskedtextboxes
    		Dim x As Integer = 10
    		Dim y As Integer = 20
    		For i As Integer = 1 To 4
    			mtb = New MaskedTextBox
    			With mtb
    				.Name = "MaskedTextBox" & i.ToString
    				.BackColor = Color.Pink
    				.Tag = i
    				.Mask = "00 - 00 - 00"
    				.Font = New Font(.Font.FontFamily, 18)
    				.TextAlign = HorizontalAlignment.Center
    				.Location = New Point(10, y)
    				.Width = 200
    				y += 44
    				AddHandler .TextChanged, AddressOf MaskedTextBox_TextChanged
    			End With
    			Controls.Add(mtb)
    		Next
    
    		' add a random textbox
    		With tb
    			.Font = New Font(.Font.FontFamily, 18)
    			.Text = "Some random text"
    			.Location = New Point(10, y + 10)
    			.BackColor = Color.WhiteSmoke
    			.Width = 200
    		End With
    		Controls.Add(tb)
    	End Sub
    	Private Sub MaskedTextBox_TextChanged(sender As Object, e As EventArgs)
    		Dim mtb As MaskedTextBox = DirectCast(sender, MaskedTextBox)
    		Dim found As Boolean = False
    		If mtb.MaskCompleted Then
    			Console.Beep(1200, 40)
    			Dim ind As Integer = CInt(mtb.Tag) + 1
    			For Each c As Control In Me.Controls
    				If c.GetType = GetType(MaskedTextBox) Then
    					If CInt(c.Tag) = ind Then
    						found = True
    						c.Select()
    					End If
    				End If
    			Next
    			If Not found Then
    				' reached last one - what to do?
    				' here I just activate the textbox
    				tb.Select()
    			End If
    		End If
    	End Sub
    End Class
    
    1 person found this answer helpful.
    0 comments No comments

  2. Rafael da Rocha 5,251 Reputation points
    2023-06-02T11:49:04.1166667+00:00

    Hello Gary,

    not up to date on my VB skills, but if I remember correctly you can user the Textbox.MaskCompleted property to check for true (filled) or false (null/empty).


    If this or any other reply helped solve your question, please remember to upvote and/or "Accept Answer".
    It helps others facing similar issues find the solution.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.