我有一個VB.Net Windows窗體上的4個下拉列表:Subpriority1,Subpriority2,Subpriority3和Subpriority4。訂單驗證
如果未輸入Subpriority1和Subpriority2的值,用戶將無法輸入Subpriority3。現在我需要一種方法來在VB中驗證它,而不必使用嵌套的IF語句。任何幫助傢伙?
我有一個VB.Net Windows窗體上的4個下拉列表:Subpriority1,Subpriority2,Subpriority3和Subpriority4。訂單驗證
如果未輸入Subpriority1和Subpriority2的值,用戶將無法輸入Subpriority3。現在我需要一種方法來在VB中驗證它,而不必使用嵌套的IF語句。任何幫助傢伙?
這裏!此方法適用於多達10個控制工作:
所有你需要做的是:
確保在您所設定的每個窗口都具有相同的名稱,除了最後一位。
確保控制連續編號(不要緊,無論他們開始爲0或1)
添加EnforceSequence(發件人,E)在組中的每個控制的方法框TextChanged。
添加EnforceSequence(NameOfYourFirstControl,爲Nothing)到Form_Load事件,或將啓用爲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個控件,然後只需更改子搶最後2位數字,但你必須以2位數字命名所有控件,例如ComboBox01
你應該做的是清除除第一個以外的所有下拉菜單。然後,在.SelectedIndexChanged事件上,加載第二個下拉列表的數據。重複下拉三下載第四個。
並且啓用下一個組合框時,請勿在下拉列表中包含先前選定的值 – Beth 2011-04-15 15:43:04
我們需要更多信息...輸入是什麼?文本框ona窗體?你的意思是「不得不使用嵌套的IF語句」,或者「不必使用嵌套的IF語句」? – 2011-04-14 23:19:20
他們是下拉列表Guid作爲價值,是的,他們在我的添加形式。我的意思是不必使用嵌套的IF語句。謝謝! – 2011-04-14 23:44:41
爲什麼不禁用除第一個以外的所有控件,然後繼續啓用下一個選擇? – 2011-04-15 03:04:16