开发者

Order validation

开发者 https://www.devze.com 2023-02-25 03:36 出处:网络
I have 4 drop-down lists on a VB.Net Windows Form: Subpriority1, Subpriority2, Subpriority3, and Subpriority4.

I have 4 drop-down lists on a VB.Net Windows Form: Subpriority1, Subpriority2, Subpriority3, and Subpriority4.

The user can't input Subpriority3 without having entered a value for Subpriority1 and Subpriority2.开发者_开发技巧 Now I need a way to validate this in VB hopefully without having to use nested IF statements. Any help guys?


What you should do is clear all dropdowns except the first. Then, on the .SelectedIndexChanged event, load the data for the second dropdown. Repeat for drop down three to load the fourth.


Here! This method will work for up to 10 controls:

All you need to do is:

  1. Make sure each control in your set has the same name, apart from the last digit.

  2. Make sure the controls are numbered consecutively (doesn't matter whether they start at 0 or 1)

  3. Add EnforceSequence(sender, e) in the TextChanged method of each control in the set.

  4. Add EnforceSequence(NameOfYourFirstControl, Nothing) to the Form_Load event, OR set the Enabled to False for all of the controls in the set apart from the first one.

  5. Add the following method to your form's code:


''' <summary>Ensure that a set of controls are available to the user sequentially.</summary>
''' <param name="sender">The Sender parameter as provided by the Control's change event</param>
''' <param name="e">The EventArgs parameter as provided by the Control's change event</param>
''' <remarks>
''' To make this work, All of the participating controls must have the same name, with a consecutive index appended.  E.g.  MyControl1, MyControl2, MyControl3.
''' Add a call to EnforceSequence(sender, e) in the TextChanged event of each of the controls.
''' </remarks>
Private Sub EnforceSequence(ByVal sender As Object, ByVal e As System.EventArgs)

    'The control that raised the event
    Dim validatingContol As System.Windows.Forms.Control = CType(sender, System.Windows.Forms.Control)

    'Get the name of the DropDown set
    Dim controlName As String = validatingContol.Name.Substring(0, validatingContol.Name.Length - 1)

    'Get the index of the control (i.e. the number at the end of the name)
    Dim validatingControlIndex As Integer = Integer.Parse(validatingContol.Name.Substring(validatingContol.Name.Length - 1))

    'Check to see if there's another control with the same name in the sequence.
    If Not Me.Controls(controlName & validatingControlIndex + 1) Is Nothing Then
        'If this control is empty, set all the following controls to empty and disabled.
        Me.Controls(controlName & validatingControlIndex + 1).Enabled = Not validatingContol.Text = ""
        'Ask the next control to do the same check
        EnforceSequence(Me.Controls(controlName & validatingControlIndex + 1), Nothing)
    End If

End Sub

If you have more than 10 controls then just change the SubStrings to grab the 2 last digits, but then you have to name all of your controls with 2 digits, e.g. ComboBox01

0

精彩评论

暂无评论...
验证码 换一张
取 消