我在VB.Net Windows窗体上有4个下拉列表:Subpriority1,Subpriority2,Subpriority3和Subpriority4。
如果没有输入Subpriority1和Subpriority2的值,用户就无法输入Subpriority3。现在我需要一种方法来在VB中验证这一点,希望不必使用嵌套的IF语句。有帮助吗?
答案 0 :(得分:1)
你应该做的是清除除第一个之外的所有下拉菜单。然后,在.SelectedIndexChanged事件上,加载第二个下拉列表的数据。重复下拉三,加载第四个。
答案 1 :(得分:0)
下面!此方法最多可用于10个控件:
您需要做的就是:
确保集合中的每个控件都具有相同的名称,除了最后一位数字。
确保控件连续编号(无论是从0还是1开始)
在集合中每个控件的TextChanged方法中添加 EnforceSequence(sender,e)。
将 EnforceSequence( NameOfYourFirstControl ,Nothing)添加到Form_Load事件,或者将集合中的所有控件的Enabled设置为False。之一。
将以下方法添加到表单的代码中:
''' <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个控件,那么只需更改SubStrings以获取最后2位数字,但是您必须将所有控件命名为2位数,例如ComboBox01