ARTICLE

Focus Color on Textbox controls

Posted by ANDY Nordlage Articles | Windows Controls C# September 01, 2010
In this article you will learn how to use Focus Color on Textbox controls in C#.
Reader Level:

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!

Login to add your contents and source code to this article
comments
COMMENT USING
PREMIUM SPONSORS
Infragistics is experts in technology and design, and passionate about helping you build highly performant and stylish applications that solve problems, deliver inspiration, and maximize results.
HTML 5 + JQUERY CONTROLS
SPONSORED BY
Nevron Diagram