2009-08-06 59 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。

相关问题