2011-04-14 99 views
0

我有一个VB.Net Windows窗体上的4个下拉列表:Subpriority1,Subpriority2,Subpriority3和Subpriority4。订单验证

如果未输入Subpriority1和Subpriority2的值,用户将无法输入Subpriority3。现在我需要一种方法来在VB中验证它,而不必使用嵌套的IF语句。任何帮助家伙?

+0

我们需要更多信息...输入是什么?文本框ona窗体?你的意思是“不得不使用嵌套的IF语句”,或者“不必使用嵌套的IF语句”? – 2011-04-14 23:19:20

+0

他们是下拉列表Guid作为价值,是的,他们在我的添加形式。我的意思是不必使用嵌套的IF语句。谢谢! – 2011-04-14 23:44:41

+0

为什么不禁用除第一个以外的所有控件,然后继续启用下一个选择? – 2011-04-15 03:04:16

回答

0

这里!此方法适用于多达10个控制工作:

所有你需要做的是:

  1. 确保在您所设定的每个窗口都具有相同的名称,除了最后一位。

  2. 确保控制连续编号(不要紧,无论他们开始为0或1)

  3. 添加EnforceSequence(发件人,E)在组中的每个控制的方法框TextChanged。

  4. 添加EnforceSequence(NameOfYourFirstControl,为Nothing)到Form_Load事件,或将启用为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位数字命名所有控件,例如ComboBox01

1

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

+0

并且启用下一个组合框时,请勿在下拉列表中包含先前选定的值 – Beth 2011-04-15 15:43:04