.net 订单验证
我在VB.Net Windows窗体上有4个下拉列表:Subpriority1、Subpriority2、Subpriority3和Subpriority4.net 订单验证,.net,vb.net,validation,.net,Vb.net,Validation,我在VB.Net Windows窗体上有4个下拉列表:Subpriority1、Subpriority2、Subpriority3和Subpriority4 如果未输入Subpriority1和Subpriority2的值,用户无法输入Subpriority3。现在我需要一种方法在VB中验证这一点,而不必使用嵌套的IF语句。有人帮忙吗?这里!此方法最多可用于10个控件: 您需要做的只是: 确保集合中的每个控件都有相同的名称,除了最后一个数字 确保控件连续编号(不管它们是从0还是1开始) 在集合中
如果未输入Subpriority1和Subpriority2的值,用户无法输入Subpriority3。现在我需要一种方法在VB中验证这一点,而不必使用嵌套的IF语句。有人帮忙吗?这里!此方法最多可用于10个控件: 您需要做的只是:
“”“确保用户可以按顺序使用一组控件。
''由控件的更改事件提供的发送方参数
''控件的更改事件提供的EventArgs参数
'''
''要使此工作正常,所有参与的控件必须具有相同的名称,并附加一个连续的索引。例如,MyControl1、MyControl2、MyControl3。
''在每个控件的TextChanged事件中添加对EnforceSequence(sender,e)的调用。
'''
私有子EnforceSequence(ByVal发送方作为对象,ByVal e作为System.EventArgs)
'引发事件的控件
Dim ValidatingControl作为System.Windows.Forms.Control=CType(发件人,System.Windows.Forms.Control)
'获取下拉列表集的名称
Dim controlName As String=validatingContol.Name.Substring(0,validatingContol.Name.Length-1)
'获取控件的索引(即名称末尾的数字)
Dim validatingControlIndex为Integer=Integer.Parse(validatingContol.Name.Substring(validatingContol.Name.Length-1))
'检查序列中是否有另一个同名控件。
如果不是Me.Controls(controlName&validatingControlIndex+1)则什么都不是
'如果此控件为空,请将以下所有控件设置为空并禁用。
Me.Controls(controlName&validatingControlIndex+1).Enabled=未验证控件。Text=“”
'请下一个控件执行相同的检查
EnforceSequence(Me.Controls(controlName&validatingControlIndex+1),Nothing)
如果结束
端接头
如果您有10个以上的控件,则只需更改子字符串以获取最后两位数字,但您必须使用两位数字命名所有控件,例如ComboBox01,您应该清除除第一位以外的所有下拉列表。然后,在.SelectedIndexChanged事件中,加载第二个下拉列表的数据。对下拉列表3重复此操作,以加载第四个。我们需要更多信息。。。输入是什么?表单上的文本框?你是说“必须使用嵌套的IF语句”还是“不必使用嵌套的IF语句”?它们是以guid为值的下拉列表,是的,它们在我的add表单上。我的意思是不必使用嵌套的IF语句。谢谢为什么不禁用除第一个控件之外的所有控件,然后在选择时继续启用下一个?并且在启用下一个组合框时,不要在下拉列表中包含以前选择的值
''' <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