将值添加到“多个文本框”或“清除多个文本框”

时间:2018-12-31 21:26:41

标签: vb.net

我有这段代码,我仍然想添加一些代码,但是根本没有出来。因此这是代码的工作原理,但在这里有所帮助-Dim txtBox As TextBox =(CType(Me.Controls(“ TextBox”&intToTest.ToString),TextBox))                     Integer.TryParse(txtBox.Text,i)                     txtBox.Text =(i + 1).ToString

Private Sub OPCode()
    Dim numberArray() As Integer = Enumerable.Range(1, 19).ToArray
    Dim lstDrawTextBoxes As New List(Of TextBox)
    Dim intToTest As Integer
    Dim lstNumberExists As New List(Of Integer)
    For i = 1 To 4
        lstDrawTextBoxes.Add(CType(Me.Controls("TxtIntDraw" & i.ToString), TextBox))
    Next
    For Each t As TextBox In lstDrawTextBoxes
        Dim textArray() As String = t.Text.Split(","c)
        For index As Integer = 0 To textArray.Length - 1
            If Integer.TryParse(textArray(index).Trim, intToTest) Then
                If numberArray.Contains(intToTest) Then
                    lstNumberExists.Add(intToTest)
                    'here I want to delete the value of Num (Number). Num.clear (Num is a Textbox)
                    MessageBox.Show(intToTest.ToString)
                    Dim txtBox As TextBox = (CType(Me.Controls("TextBox" & intToTest.ToString), TextBox))
                    txtBox.Clear()
                Else
                    'Here I want to +1 the value of Num (Number). That Is, if Num (Number) = 0, then it will become +1, ie Num (Number).text = 1 (Num Is a Textbox)
                    Dim i As Integer
                    Dim txtBox As TextBox = (CType(Me.Controls("TextBox" & intToTest.ToString), TextBox))
                    Integer.TryParse(txtBox.Text, i)
                    txtBox.Text = (i + 1).ToString
                End If
            End If
        Next
    Next
End Sub

1 个答案:

答案 0 :(得分:0)

Private Sub OPCode()
    Dim numberArray() As Integer = Enumerable.Range(1, 80).ToArray
    Dim lstDrawTextBoxes As New List(Of TextBox)
    Dim intToTest As Integer
    Dim lstNumberExists As New List(Of Integer)
    For i = 1 To 60
        lstDrawTextBoxes.Add(DirectCast(Controls.Find("txtIntDraw" & i.ToString, True)(0), TextBox))
    Next
    For Each t As TextBox In lstDrawTextBoxes
        Dim textArray() As String = t.Text.Split(","c)
        For index As Integer = 0 To textArray.Length - 1
            If Integer.TryParse(textArray(index).Trim, intToTest) Then
                If numberArray.Contains(intToTest) Then
                    lstNumberExists.Add(intToTest)
                    'here I want to delete the value of Num (Number). Num.clear (Num is a Textbox)
                    MessageBox.Show(intToTest.ToString)
                    Dim txtBox As TextBox = DirectCast(Controls.Find("Num" & intToTest.ToString, True)(0), TextBox)
                    MessageBox.Show(txtBox.Name)
                    txtBox.Clear()
                Else
                    'Here I want to +1 the value of Num (Number). That Is, if Num (Number) = 0, then it will become +1, ie Num (Number).text = 1 (Num Is a Textbox)
                    Dim i As Integer
                    Dim txtBox As TextBox = DirectCast(Controls.Find("Num" & intToTest.ToString, True)(0), TextBox)
                    Integer.TryParse(txtBox.Text, i)
                    txtBox.Text = (i + 1).ToString
                End If
            End If
        Next
    Next
End Sub

这只是一个简单而通用的代码。但是,让我在这里解释局限性。首先,我在循环中使用条件。如果使用调试器,他们可以简单地注意到If语句花费了最多的执行时间。因此,如果可能,请尝试使用IfAndAndAlso运算符将两个Or语句合并为一个。

此外,您似乎正在填充TextBox的庞大列表,但是为什么呢? TextBox是从Windows.Froms.Controls类派生的类(如果您使用的是WinForms),并且确实具有图形视图。因此,如果文本框不会被添加到UI / Window,为什么要使用它们呢?相反,您可以简单地创建String的列表。但是我想您是将TextBox添加到用户界面中,但您刚刚发布了示例代码,但仍然添加了此建议,以供将来参考。

现在,回到文本框。如果要在表单中添加大量文本框,则UI会冻结,并且应用程序将变得无法使用。在某些情况下,Windows甚至会将其标记为Suspended,因为它不会对任何鼠标/单击/轻击/按钮事件做出反应并抛出**** has stopped working。在这种情况下,您可以使用异步方法(了解有关异步编程here的更多信息)。

这是一个示例:

Private Asyn Sub OPCode()
'''Your code goes here

Await Application.Current.Dispatcher.Invoke(CType(Sub()FillWithTextboxes(lstDrawTextBoxes)) End Sub, Action)
End Sub

Sub FillWithTextboxes(List(of TextBox))
  For each tb as TextBox in lstDrawTextBoxes
    Me.Controls.Add(tb)
  Next
End Sub

这将防止UI冻结并将控件异步添加到表单中。 请注意,我正在使用Dispatcher.Invoke。此方法将在Dispatcher与UI线程关联的线程上同步执行指定的委托。当您要向用户显示带有一些等待动画的等待屏幕时,此方法很有用。

希望这对您有所帮助。

请参阅原始问题Check All Numbers 1 to 80 in every 60 Textbox