我的表單上有 80 個標簽,全部命名為“lbl”,之后是一個從 1 到 80 的數字。我想遍歷它們并將它們的 Text 屬性設定為連續模式,例如 1、2、3 ... 80。
Dim nr As Integer
For nr = 1 To 80
lbl(nr).Text = nr
Next nr
我現在有這個,但它不起作用。有誰知道類似的代碼可以滿足我的需要?
uj5u.com熱心網友回復:
改變這個:
lbl(nr).Text = nr
對此:
Controls("lbl" & nr).Text = nr
這假設Labels直接在表單上。如果它們位于Panel某個容器或其他容器上,請使用它的Controls集合而不是表單的集合。
uj5u.com熱心網友回復:
'in WPF
Dim i As Integer = 1
For Each c In Me.gridMain.Children
If c.GetType() Is GetType(Label) AndAlso c.Name Like "lbl*" Then
Dim _label As Label = c
_label.Content = "label" & i
i = 1
End If
Next
'in Winform
Dim i As Integer = 1
For Each c In Me.Controls
If c.GetType() Is GetType(Label) AndAlso c.Name Like "lbl*" Then
Dim _label As Label = c
_label.Text = "label" & i
i = 1
End If
Next
uj5u.com熱心網友回復:
將執行此操作的代碼放在自己的方法中會使事情保持整潔。然后,您可能有多個要在其中設定標簽文本的容器(例如 Panels),因此您可以將該容器作為引數傳遞給該方法。
由于您想在某個前綴后使用標簽名稱中的數字,您可以檢查該前綴,然后將名稱設定為前綴后的所有內容,如下所示:
Sub SetLabelsText(container As Control)
Const prefix = "lbl"
For Each ctrl In container.Controls.OfType(Of Label)
If ctrl.Name.StartsWith(prefix) Then
ctrl.Text = ctrl.Name.Substring(prefix.Length)
End If
Next
End Sub
然后,如果容器被命名為“Panel1”,您將使用
SetLabelsText(Panel1)
或者如果標簽直接在表單上而不是在表單上的容器中:
SetLabelsText(Me)
使用這樣的方法,您還可以將前綴作為引數傳遞,以使其更加通用:
Sub SetLabelsText(container As Control, prefix As String)
For Each ctrl In container.Controls.OfType(Of Label)
If ctrl.Name.StartsWith(prefix) Then
ctrl.Text = ctrl.Name.Substring(prefix.Length)
End If
Next
End Sub
并稱它為
SetLabelsText(Me, "lbl")
但是,如果標簽以您可以使用一兩個 for 回圈創建的某種模式布局,那么以編程方式創建它們并同時設定文本可能會更容易一些。
uj5u.com熱心網友回復:
聽起來您的控制元件編號為 1-80,并且您想回圈訪問它們。
在運行回圈之前,您需要將它們實際放入一個陣列中。如果您有任何嵌套控制元件,這將需要在表單上找到所有文本框。
我寫了一個小測驗應用程式,這就是我想出的:
首先,一個包含我們要處理的所有文本框的串列,以及一個填充它的子串列。
Public numberTextBoxes As New List(Of TextBox)
' A function that finds all the desired textboxes and saves them
Public Sub InitializeTextBoxes()
Dim controls = GetAllTextBoxes(Me)
For Each formTextBox As TextBox In controls
Dim textBoxName As String = formTextBox.Name
If textBoxName.StartsWith("TextBox") Then
Dim textBoxNumber As Integer
' verify that this has a number after the TextBox part
If Integer.TryParse(textBoxName.Substring(7), textBoxNumber) Then
numberTextBoxes.Add(formTextBox)
End If
End If
Next formTextBox
End Sub
GetAllTextBoxes(Me)是一個回傳控制元件中包含的所有文本框的函式。如果您只查看表單上的所有控制元件而不查看子控制元件,您將錯過嵌套項。
我會在下面填寫。
接下來,一個從名稱末尾獲取數字的函式,以及一個實際更改文本框的函式:
' extracts the number from the end of a textbox's string
Public Function GetTbNum(textBoxName As String) As Integer
If (textBoxName.Length > 2) Then
GetTbNum = CInt(textBoxName.Substring("TextBox".Length))
End If
End Function
' a function to change the names on the textboxes
Public Sub FillTextBoxes()
' Note that Arrays and Lists are usually 0-based
For i = 0 To numberTextBoxes.Count - 1
Dim tb As TextBox = numberTextBoxes(i)
' if you want to set the text to the number at the end of the name
numberTextBoxes(i).Text = GetTbNum(numberTextBoxes(i).Name)
Next i
End Sub
最后,該函式將表單上的所有文本框作為串列回傳。如果您知道您的表單只會包含最頂層的文本框,您可以回圈MyForm.Controls并找到這些文本框,但這適用于您布局表單的任何方式。
Function GetAllTextBoxes(control As Control, Optional textBoxList As List(Of TextBox) = Nothing) As List(Of TextBox)
If textBoxList Is Nothing Then
textBoxList = New List(Of TextBox)
GetAllTextBoxes(control, textBoxList)
GetAllTextBoxes = textBoxList
Else
If (TypeOf control Is TextBox) Then
textBoxList.Add(control)
End If
For Each item In control.Controls
GetAllTextBoxes(item, textBoxList)
Next item
End If
End Function
現在,在表單的初始化程式中,您可以呼叫函式:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
InitializeTextBoxes()
FillTextBoxes()
End Sub
有很多事情可以在這里做不同的事情,但希望這是你需要做這樣的事情的一個不錯的開始。
當然,您可以為要查看的文本框的名稱添加一個變數,或者更改您設定文本的方式;此外,如果您最終讓您的控制元件亂序等,您可以對串列進行排序。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/484745.html
標籤:VB.net
下一篇:收集集合中的文本框并驗證
