Focus Color on Textbox controls


I was told to make my forum post an article to know if you (the community) have any better ideas in implementing the following:

To use the focus event of Textbox controls to set the Textbox Backgroundcolor using the Enter() and the Leave() events. I even implemented an Error color advise.

The normal backgroud color of the TextBox control is System.Drawing.Color.White, the focused color is System.Drawing.Color.Yellow and the error sign color is System.Drawing.Color.Red.

The implemented function is called by every Textbox control when occuring Enter and Leave events and setting the appropriate color. Then a Button1 event simulates an error (e.s.: invalid value, unknown username, text not found, etc.) and focuses on textBox1.

The Button2 Click-Event simulates to pass a non textbox object to the TextBox_Enter (Enter event in tetxBox1). This 2nd button (Button2) is to demonstrate how to avoid passing wrong objects to a function (in this case an error will be thrown if you try to pass any other objects than a textbox object).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace tboxfocus
{
    public partial class Form1 : Form
    {
        private System.Drawing.Color m_tbcolorenter = System.Drawing.Color.Yellow;
        private System.Drawing.Color m_tbcolorleave = System.Drawing.Color.White;
        private System.Drawing.Color m_tbcolorerror = System.Drawing.Color.Red;
        private Boolean m_tbcolorerrorflag = false;
 
        private void ThrowErrormessage(object sender)
        {
            if (sender != null)
                MessageBox.Show(String.Concat("Invalid object: ", sender.ToString(),
                ", Only Textbox objects are allowed!"), "Error - invalid object!");
            else
                MessageBox.Show("Unknown error", "Sorry :=(");
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {
 
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void TextBox_Enter(object sender, EventArgs e)
        {
            TextBox tb = null;
            if (sender is TextBox)
            {
                tb = (TextBox)sender;
                if (m_tbcolorerrorflag)
                    tb.BackColor = m_tbcolorerror;
                else
                    tb.BackColor = m_tbcolorenter;
            }
            else ThrowErrormessage(sender);
            return;
                      
        }
 
        private void TextBox_Leave(object sender, EventArgs e)
        {
            TextBox tb = null;
            if (sender is TextBox)
            {
                tb = (TextBox)sender;
                tb.BackColor = m_tbcolorleave;
            }
            else ThrowErrormessage(sender);
            return;
        }

        private void TextBox_Error(object sender, EventArgs e)
        {
            TextBox tb = null;
            if (sender is TextBox)
            {
                tb = (TextBox)sender;
                tb.BackColor = m_tbcolorerror;
                tb.Focus();
                m_tbcolorerrorflag = false;
            }
            else ThrowErrormessage(sender);
            return;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            m_tbcolorerrorflag = true;
            TextBox_Error(textBox1, e);
        }

        private void button2_Click(object sender, EventArgs e)
       
{
            TextBox_Enter(button2, e);
       }

     }
}


If you have any better or additional suggestions I'm glad to it… :=) Good work and code it right!

Up Next
    Ebook Download
    View all
    Learn
    View all