2009-08-06 31 views

回答

3

下面是你可以在VB.NET中做什麼。

首先,您需要一個返回Form(或ContainerControl)中的所有TextBox控件的函數。其原因將變得清晰,我將有函數實際上返回一個字典,每一個文本框作爲關鍵,像這樣的名字:

Private Function getAllTextBoxes(ByVal container As ContainerControl) As Dictionary(Of String, TextBox) 
    Dim allTextBoxes As New Dictionary(Of String, TextBox) 

    For Each ctrl As Control In container.Controls 
     If TypeOf ctrl Is TextBox Then allTextBoxes.Add(ctrl.Name, ctrl) 
    Next 

    Return allTextBoxes 
End Function 

接下來,你需要一個函數返回一個字典提供每一個文本框中值,因此可以判斷哪些值發生了變化:

Private Function getTextBoxValues(ByVal textBoxDefs As IDictionary(Of String, TextBox)) As Dictionary(Of String, String) 
    Dim textBoxValues As New Dictionary(Of String, String) 

    For Each tbxDef As KeyValuePair(Of String, TextBox) In textBoxDefs 
     Dim tbx As TextBox = tbxDef.Value 

     Dim name As String = tbx.Name 
     Dim value As String = tbx.Text 

     textBoxValues.Add(name, value) 
    Next 

    Return textBoxValues 
End Function 

最後,如果我正確地理解你的問題,你想要的功能要經過每一個文本框,其值與先前已記錄的值,如果所有值都已更改,則返回True。這將這樣的伎倆:

Private Function getAllTextBoxValuesChanged() As Boolean 
    Dim allTextBoxes As Dictionary(Of String, TextBox) = getAllTextBoxes(Me) 

    Static allTextBoxPreviousValues As Dictionary(Of String, String) = getTextBoxValues(allTextBoxes) 
    Dim allTextBoxCurrentValues As Dictionary(Of String, String) = getTextBoxValues(allTextBoxes) 

    Dim numTextBoxes As Integer = allTextBoxes.Count 
    Dim numChangedValues As Integer = 0 

    Dim modifications As New Dictionary(Of String, String) 

    For Each tbxDef As KeyValuePair(Of String, String) In allTextBoxCurrentValues 
     Dim name As String = tbxDef.Key 
     Dim currentValue As String = tbxDef.Value 

     Dim previousValue As String = allTextBoxPreviousValues(name) 

     If currentValue <> previousValue Then 
      numChangedValues += 1 
      modifications.Add(name, currentValue) 
     End If 
    Next 

    For Each modificationDef As KeyValuePair(Of String, String) In modifications 
     allTextBoxPreviousValues(modificationDef.Key) = modificationDef.Value 
    Next 

    Return (numChangedValues >= numTextBoxes) 
End Function 

記住,因爲上面的函數使用一個靜態變量,它只會如果上次你給它最後一次的所有值已經改變返回true。另外,第一次調用該函數時,它將返回false。 (但這種行爲可以很容易地如果需要改變。)

有了這些功能,你要檢查是否所有的值已經改變(因爲你上次檢查)任何時候,你可以這樣寫:

Dim allTextBoxValuesChanged As Boolean = getAllTextBoxValuesChanged() 

If allTextBoxValuesChanged Then 
    DoSomething() 
End If 
+0

wowowowowowo非常感謝 – 2009-08-06 18:36:49

1

沒有簡單的解決方案。你可以做的是寫一個方法,當它遍歷容器控件時,根據它處理的文本框的值返回一個布爾值,檢查控件是否是文本框,以及是否檢查它是否有值。

+0

你能給我一個循環容器控件的例子嗎? – 2009-08-06 16:17:27

1

那麼,你會將每個文本框的.text值控制窗體初始加載時的形式(可能存儲在一個數組中),然後執行if評估(如你所提到的),比較當前的.text值每個文本框對陣列。這會適合你的場景嗎?

+0

如果我有1000個文本框,該怎麼辦? – 2009-08-06 16:32:18

+0

你在窗體上有1000個文本框?讓人驚訝。那麼,你當然可以將我的解決方案應用到1000個文本框中,但我無法想象一個1000個文本框的表單。 – OneNerd 2009-08-06 16:39:38

+1

如果您有1000個文本框,也許該查找另一個解決方案了,如數據網格。 – 2009-08-06 16:42:48

1

如果表單代表你的應用的「設置」

  • 創建一個設置類
  • 創建一個返回設置對象
  • 創建Settings.Equals功能比較兩個設置對象
  • 一個GetSettingsFromInterface功能
  • 如果CurrentSettings.Equals(formSettings)沒有改變。
+0

可以請你寫一個例子 – 2009-08-06 17:55:56

0

或者您可以在每個複選框上放置一個JavaScript「onchange」事件,並使用它來設置隱藏字段「aCheckboxChanged()」或其他內容,將隱藏變量從零設置爲1。

相關問題