如何在vb.net中设置所有DevExpress TextEdit控件的背景颜色

时间:2015-01-24 02:26:03

标签: vb.net devexpress

我有10个Textboxs然后我尝试将背景颜色设置为红色到所有Textbox这是空白的叶子,这是有效的,但它没有按照第一个{{1}的顺序设置背景颜色}到第十Textbox,我也尝试设置Textbox。但它仍然无效。

TabIndex

2 个答案:

答案 0 :(得分:0)

如果您重命名它们,或者在表单周围剪切/粘贴它们,它们可能会“乱序”。

为了保证顺序,要么创建一个显式数组并迭代它:

Private Function pf_validate_ok()
    Dim TBs() As TextBox = {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
    For Each tb In TBs
        If TypeOf tb Is DevExpress.XtraEditors.TextEdit Then
            If tb.Text = String.Empty Then

                tb.BackColor = Color.Red
                tb.ForeColor = Color.White

                Return False
            Else
                tb.BackColor = Color.White
                tb.ForeColor = Color.Black
            End If
        End If
    Next tb
    Return True
End Function

或者通过循环名称搜索它们:

Private Function pf_validate_ok()
    For i As Integer = 1 To 5
        Dim matches() As Control = Me.Controls.Find("TextBox" & i, True)
        If matches.Length > 0 AndAlso TypeOf matches(0) Is DevExpress.XtraEditors.TextEdit Then
            Dim tb As DevExpress.XtraEditors.TextEdit = DirectCast(matches(0), DevExpress.XtraEditors.TextEdit)
            If tb.Text = String.Empty Then

                tb.BackColor = Color.Red
                tb.ForeColor = Color.White

                Return False
            Else
                tb.BackColor = Color.White
                tb.ForeColor = Color.Black
            End If
        End If
    Next
    Return True
End Function

您需要更改数组或Controls.Find()调用中的名称以匹配您的特定方案。

答案 1 :(得分:0)

您可以按TextBox'sTextBox.Top属性对TextBox.Left进行排序,以获得正确的订单。
以下是LINQ的示例:

Dim textBoxs = From textBox In Panel1.Controls
               Where TypeOf textBox Is DevExpress.XtraEditors.TextEdit
               Order By textBox.Top, textBox.Left
               Select DirectCast(textBox, DevExpress.XtraEditors.TextEdit)

For Each textBox In textBoxs
    If textBox.Text = String.Empty Then
        textBox.BackColor = Color.Red
        textBox.ForeColor = Color.White

        Return False
    Else
        textBox.BackColor = Color.White
        textBox.ForeColor = Color.Black
    End If
Next textBox