Vorleak Chy's Blog
I have passion for technologies
Change background color of TextBox or ComboBox in Windows Forms
After one of the applications is done successfully completed with automatic unit tests and send to users, what else I need to do is that waiting to get feedback from users. You know what the user compliant is user interface (I am wondering how I can test user interface to meet the user requirement beforehand, maybe no way for now). So I add codes to my application but not much is to set background color to be Kaki of TextBox or ComboBox if the cursor is in. Also, if you press enter key the cursor will focus to next control it depends on Tab Order.
Here is the example with User Information form as one of them:

Implementation
Create one class Utility
using System;
using System.Drawing;
using System.Windows.Forms;
public static class Utility
{
public static void AddEventHandler(Control panel)
{
foreach (Control control in panel.Controls)
{
if (control is TextBox || control is ComboBox)
{
control.KeyDown += control_KeyDown;
control.Enter += control_Enter;
control.Leave += control_Leave;
}
if (control.HasChildren)
{
AddEventHandler(control);
}
}
}
private static void control_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData != Keys.Return) return;
e.Handled = false;
SendKeys.Send("{TAB}");
}
private static void control_Enter(object sender, EventArgs e)
{
((Control) sender).BackColor = Color.Khaki;
}
private static void control_Leave(object sender, EventArgs e)
{
((Control) sender).BackColor = Color.FromKnownColor(KnownColor.Window);
}
}
Call AddEventHandler(this) method in the form load event
private void frmUserInformation_Load(object sender, EventArgs e)
{
Utility.AddEventHandler(this);
}
Subscribe-
Search-
Tags-
Categories-
Recent Comments-
- Using UUID as Primary Key in Ruby on Rails Thanks @Chamnap, I have...
- Using UUID as Primary Key in Ruby on Rails Not working. You need to...
- Installing GeoServer on Ubuntu Thanks for that! You saved me a lot...
- Change background color of TextBox or ComboBox in Windows Forms Hi....
- SQL Server 2005 Update Trigger Effect Multiple Rows Its really helpful...
- Copyright 2010 Vorleak Chy's Blog. All Rights Reserved. Powered by Wordpress | Theme designed by Chris Murphy
- Back To Top
- Home


Leave a Comment-