Textbox Enabled True False

时间:2016-08-04 05:17:45

标签: vb.net

这是我试图解决的代码

        Dim i As Integer
    i = ListBox1.SelectedIndex
    If ListBox1.SelectedIndex = 0 Then
        TextBox10.Enabled = False
        TextBox25.Enabled = False
        TextBox30.Enabled = False
        TextBox40.Enabled = False
        TextBox55.Enabled = False
        TextBox65.Enabled = False
        TextBox73.Enabled = False
        TextBox84.Enabled = False
        TextBox95.Enabled = False
        TextBox100.Enabled = False
        TextBox185.Enabled = False
    Else
        TextBox(??).Enabled = True

    End If

如何让其他文本框启用True? 我的意思是 10,25,30,40 ... 185是假的,所有其他textboes都启用了吗?

3 个答案:

答案 0 :(得分:0)

您可以找到文本框类型的控件并指定默认启用= true ,然后编写代码以设置enabled = false。

For Each c As Control In Me.Controls
If c.GetType Is GetType(TextBox) Then
    c.Enabled=true
End If
Next

OR  

Dim allTextBox As New List(Of Control)
For Each c As TextBox In FindControl(allTextBox, Me,GetType(TextBox))
    c.Enabled=true
Next

Public Shared Function FindControl(ByVal list As List(Of Control), ByVal parent As Control, ByVal ctrlType As System.Type) As List(Of Control)
    If parent Is Nothing Then Return list
    If parent.GetType Is ctrlType Then
        list.Add(parent)
    End If
    For Each child As Control In parent.Controls
        FindControl(list, child, ctrlType)
    Next
    Return list
End Function

答案 1 :(得分:0)

您可以遍历窗体控件集合。类似的东西:

Dim i As Integer
i = ListBox1.SelectedIndex
for each c in me.controls
        if TypeOf c is TextBox then
            dim Textbox as textbox = DirectCast(c, TextBox)
            'Determine state of your textbox here.
            'Perhaps use the TAG property to assign a selectedindex to each textbox
            Textbox.enabled = (Textbox.Tag.ToString() <> i.ToString())
        end if
next

答案 2 :(得分:0)

我会选择两个列表:

var falseList = new List<ListBox> { TextBox10, TextBox25, TextBox30, ... };
var trueList = new List<ListBox> { TextBox1, TextBox2, TextBox3, ... };

然后在每个条件块中 if 枚举每个列表并设置为false:

foreach(var box in falseList) box.Enabled = false;

foreach(var box in trueList) box.Enabled = true;

另一种选择是设置一个带有两个布尔值的DataSource,并将每个 true ListBox上的DataBinding设置为其中一个属性,另一个设置为 false 属性,然后在一个语句中为所有数据源设置找到的属性。

注意:对不起,它是c#语法,但它非常接近VB.NET。我只是不知道确切的VB语法