【问题标题】:Order validation订单验证
【发布时间】:2011-04-14 22:56:10
【问题描述】:

我在 VB.Net Windows 窗体上有 4 个下拉列表:Subpriority1、Subpriority2、Subpriority3 和 Subpriority4。

如果没有为 Subpriority1 和 Subpriority2 输入值,则用户无法输入 Subpriority3。现在我需要一种方法来在 VB 中验证这一点,而不必使用嵌套的 IF 语句。有帮助吗?

【问题讨论】:

  • 我们需要更多信息...输入是什么?表单上的文本框?您的意思是“必须使用嵌套的 IF 语句”还是“不必使用嵌套的 IF 语句”?
  • 它们是下拉列表,以 Guids 作为值,是的,它们在我的添加表单上。我的意思是不必使用嵌套的 IF 语句。谢谢!
  • 为什么不禁用除第一个控件之外的所有控件,然后在选择时继续启用下一个控件?

标签: .net vb.net validation


【解决方案1】:

您应该做的是清除除第一个之外的所有下拉菜单。然后,在 .SelectedIndexChanged 事件中,加载第二个下拉列表的数据。重复下拉三个以加载第四个。

【讨论】:

  • 并且在启用下一个组合框时,不要在下拉列表中包含先前选择的值
【解决方案2】:

这里!此方法最多可用于 10 个控件:

您需要做的就是:

  1. 确保集合中的每个控件都具有相同的名称,除了最后一个数字。

  2. 确保控件是连续编号的(不管它们是从 0 还是从 1 开始)

  3. 在集合中每个控件的TextChanged方法中添加EnforceSequence(sender, e)

  4. EnforceSequence(NameOfYourFirstControl, Nothing) 添加到 Form_Load 事件中,或者将集合中除第一个之外的所有控件的 Enabled 设置为 False一。

  5. 将以下方法添加到表单代码中:


''' <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

如果您有超过 10 个控件,则只需更改子字符串以获取最后 2 个数字,但是您必须用 2 个数字命名所有控件,例如组合框01

【讨论】:

    猜你喜欢
    • 2022-06-27
    • 1970-01-01
    • 2016-05-07
    • 1970-01-01
    • 2012-12-06
    • 2014-06-18
    • 1970-01-01
    • 2015-07-02
    • 1970-01-01
    相关资源
    最近更新 更多