如果文本框是某种颜色,则更改它们的背景颜色 [英] Change backcolor of textboxes if they are a certain color

查看:86
本文介绍了如果文本框是某种颜色,则更改它们的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在面板中有40个文本框,分布在tabcontrol中的4个tabpages中,我需要能够单击我的KennelStatus按钮,如果文本框已被用户更改为橙色,则它将更改为CadetBlue。



我已经尝试了附加的代码,我认为应该可以使用,但没有任何反应。是因为文本框可能在面板中吗?



我知道我可以单独完成每个文本框,并且它可以工作,但必须有一个比分别为40个文本框写出来。

任何帮助都将不胜感激



Diane



我尝试过:



I have 40 textboxes inside panels, spread over 4 tabpages in a tabcontrol, I need to be able to click my KennelStatus button and if the textbox has been changed to orange by the user, then it changes to CadetBlue.

I have tried the attached code, which I thought should work, but nothing happens. Is it because the textboxes are in panels maybe??

I know that I can do each textbox seperately, and it works, but there has to be an easier way than writing it out separately for 40 textboxes.
Any help would be much appreciated

Diane

What I have tried:

Private Sub KennelStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KennelStatus.Click

Dim a As Control

        For Each a In Me.TabControl1.Controls

            If TypeOf a Is TextBox Then
                If a.BackColor = Color.Orange Then
                    a.BackColor = SystemColors.CadetBlue
                End If

            End If


        Next

End Sub



我知道以下每个文本框的工作原理单独:


I know the following works for each textbox separately:

Private Sub KennelStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles KennelStatus.Click

If Kennel1StatusTbox.BackColor = Color.Orange Then
            Kennel1StatusTbox.BackColor = SystemColors.CadetBlue
        End If
End sub

推荐答案

如果您的文本框位于任何其他控件内 - 小组计数 - 然后外部控制充当一个容器,并且它有自己独立的控件集合,你需要搜索。

最简单的解决方案就是递归:

If your textboxes are inside any other control - and a Panel counts - then the "outer control" acts as a Container, and has it's own separate Controls collection that you need to search as well.
The simplest solution is just to recurse:
Private Sub ScanContainer(ByVal controls As Control.ControlCollection)
    For Each c As Control In controls
        If TypeOf c Is TextBox Then
            ...
        End If

        If c.Controls IsNot Nothing AndAlso c.Controls.Count > 0 Then
            ScanContainer(c.Controls)
        End If
    Next
End Sub


这篇关于如果文本框是某种颜色,则更改它们的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆