Vb.Net - 动态改变文本框BackColor的类 [英] Vb.Net - Class to Change Textbox BackColor Dynamically

查看:543
本文介绍了Vb.Net - 动态改变文本框BackColor的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何创建一个类来更改表单中的每个文本框BackColor。
要更具体:


  1. 当文本框为空时,文本框BackColor等于白色。

  2. 当文本框获取焦点时,文本框BackColor更改。

  3. 当文本框有任何文本时,文本框BackColor更改。文本框失去焦点,文本框BackColor更改。

现在,我是这样做的。

 私有子tb_Login_Enter(sender As Object,e as EventArgs)句柄tb_Login.Enter 
tb_Login.BackColor = Color.LightCyan
End Sub

Private Sub tb_Login_Leave(sender As Object,e As EventArgs)句柄tb_Login.Leave
如果tb_Login.Text<> then
tb_Login.BackColor = Color.LightGreen
Else
tb_Login.BackColor = Color.White
如果


>感谢

解决方案

所有你需要做的就是从TextBox控件继承。

  Public Class TextBoxEx 
Inherits TextBox

Private Sub TextBoxEx_Enter(sender As object,e作为EventArgs)处理Me.Enter
Me.BackColor = Color.LightCyan
End Sub

Private Sub TextBoxEx_Leave(sender As Object,e as EventArgs)Handles Me.Leave
如果Me.Text<> then
Me.BackColor = Color.LightGreen
否则
Me.BackColor = Color.White
结束如果
结束子
结束类

创建项目,然后用新的TextBoxEx控件替换TextBox控件。


I'd like to know how to create a Class to change each textbox BackColor inside a Form. To be more Specific:

  1. When the textbox Is Empty, the textbox BackColor equals White.
  2. When the textbox Get focus, the textbox BackColor change.
  3. When the textbox have any text, the textbox BackColor change.
  4. When the textbox Lost focus, the textbox BackColor change.

At the moment, I'm doing it this way.

Private Sub tb_Login_Enter(sender As Object, e As EventArgs) Handles tb_Login.Enter
    tb_Login.BackColor = Color.LightCyan
End Sub

Private Sub tb_Login_Leave(sender As Object, e As EventArgs) Handles tb_Login.Leave
    If tb_Login.Text <> "" Then
        tb_Login.BackColor = Color.LightGreen
    Else
        tb_Login.BackColor = Color.White
    End If

But, I have many TextBox in my from, so, how can I create a Class for it?

Thanks

解决方案

All you need to do is inherit from the TextBox control.

Public Class TextBoxEx
    Inherits TextBox

    Private Sub TextBoxEx_Enter(sender As Object, e As EventArgs) Handles Me.Enter
        Me.BackColor = Color.LightCyan
    End Sub

    Private Sub TextBoxEx_Leave(sender As Object, e As EventArgs) Handles Me.Leave
        If Me.Text <> "" Then
            Me.BackColor = Color.LightGreen
        Else
            Me.BackColor = Color.White
        End If
    End Sub
End Class

Build your project and then replace your TextBox controls with the new TextBoxEx control.

这篇关于Vb.Net - 动态改变文本框BackColor的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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