Salman Khan
Salman Khan

Reputation: 77

Code is not providing the result in order of empty controls

I have just started to learn visual basic and i am using visual studio 2015.

I have an employee database form in which the user has to enter all the details of employee. If there is any field left empty, message box will prompt.

I am attaching the picture of my form also.

enter image description here

Here is my code:

Sub blankdataentry()
    'Declaring the Variables
    Dim ctrl As Control

    'Enable the Data Entry Controls
    For Each ctrl In Me.Controls
        If TypeOf ctrl Is ComboBox Then
            If ctrl.Text = "" Then
                MsgBox("The Field " & ctrl.Name + " is Empty." & vbNewLine & "Please fill in the Relevant Information.", vbInformation, "Missing Details")
                ctrl.Focus()
                Exit Sub
            End If
        ElseIf TypeOf ctrl Is MetroFramework.Controls.MetroTextBox Then
            If ctrl.Text = "" Then
                MsgBox("The Field " & ctrl.Name + " is Empty." & vbNewLine & "Please fill in the Relevant Information.", vbInformation, "Missing Details")
                ctrl.Focus()
                Exit Sub
            End If
        End If
    Next
End Sub

I am calling this function on my save button. When i click the save button, it is ignoring the order of my textboxes and randomly checking textboxes and display the message with its name.

For example, if i donot enter any information and press the save button, it should prompt me to enter the Employee ID but it takes me to "Fuel" textbox.

Please review and advise how can i resolve this.

Thanks.

Upvotes: 0

Views: 67

Answers (3)

Mary
Mary

Reputation: 15101

You can directly change the order of the controls in the Controls collection by accessing the generated code that the designer creates. Right click on Form1.Designer.vb and choose View Code. Scroll down to where the controls are added to the Controls collection. Now cut and paste to the desired order. Be very careful because you really are not supposed to mess with this.

Me.Controls.Add(Me.TextBox1)
 Me.Controls.Add(Me.Button1)
 Me.Controls.Add(Me.TextBox4)
 Me.Controls.Add(Me.TextBox3)
 Me.Controls.Add(Me.TextBox2)

Upvotes: 1

41686d6564
41686d6564

Reputation: 19661

You can set the TabIndex property of your controls in an ascending order [0, 1, 2,.. etc.] which helps you set the tab order of the controls. Now, since you want to check the controls in order, it makes perfect sense to iterate through them using the tab order.

Then you can simply replace your loop with the following:

For Each ctrl In Me.Controls.OfType(Of Control).
                             OrderBy(Function(c) c.TabIndex)
    ' Do whatever you want with the controls as they
    ' are now in the same order of the TabIndex property.
Next

Hope that helps.

Upvotes: 2

Ctznkane525
Ctznkane525

Reputation: 7465

Make a list of the messages so they see all of them.

This will only select one control, but the user will know all of them require editing at the same time.

Function ValidDataEntry() as Boolean
    'Declaring the Variables
    Dim messages as New List(Of String)

    'Enable the Data Entry Controls
    For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is ComboBox Then
            If ctrl.Text = "" Then
                messages.Add("The Field " & ctrl.Name + " is Empty.")
                If messages.Count = 1 Then ctrl.Focus()
                'Exit Sub
            End If
        ElseIf TypeOf ctrl Is MetroFramework.Controls.MetroTextBox Then
            If ctrl.Text = "" Then
                messages.Add("The Field " & ctrl.Name + " is Empty.")
                If messages.Count = 1 Then ctrl.Focus()
                'Exit Sub
            End If
        End If
    Next

    If messages.Count > 0 Then


        System.Windows.Forms.MessageBox.Show( _
            String.Join(Environment.NewLine, _
            messages.ToArray()), "Please fill in the Relevant Information.") 
     End If

     Return messages.Count = 0
End Function

Upvotes: 0

Related Questions