Click here to Skip to main content
15,939,960 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
hi
I my using winform. in winform i have 3 textbox. the textbox default color is lightblue.
but when the textbox is focus the color should change to white and when its lost focus textbox color should come to its original color.


plz reply as soon as possible
Posted
Comments
Dholakiya Ankit 4-Jul-13 0:36am    
I think solution 2 is ok

 
Share this answer
 
Please try the following
Initially assign the default color as Light blue for All three text box
next create two events textBox1_Enter/textBox1_Leave as follows

C#
private void textBox1_Enter(object sender, EventArgs e)
       {
           textBox1.BackColor = Color.White;
       }

       private void textBox1_Leave(object sender, EventArgs e)
       {
           textBox1.BackColor = Color.LightBlue;
       }
 
Share this answer
 
use this for textbox focus change event
C#
private void textBox1_Validating(object sender, CancelEventArgs e)
       {
           textBox1.BackColor = System.Drawing.Color.Red;
       }
       private void textBox1_Enter(object sender, EventArgs e)
       {
           textBox1.BackColor = System.Drawing.Color.Yellow;
       }
 
Share this answer
 
v2
You can use Enter event for this. And also if you want to change the color of first text box to the default color when you go to the next text box, that can be done with leave event.

Use this code.
C#
private void textBox_Enter(object sender, EventArgs e)
       {
           TextBox a = (TextBox)sender;
           a.BackColor = Color.Blue;
       }

       private void textBox_Leave(object sender, EventArgs e)
       {
           TextBox a = (TextBox)sender;
           a.BackColor = SystemColors.Window;
       }

Bind those events to the two text boxes. (You can even do this from the designer without writing code.)
C#
public Form1()
  {
          InitializeComponent();

          this.textBox1.Leave += new System.EventHandler(this.textBox_Leave);
          this.textBox1.Enter += new System.EventHandler(this.textBox_Enter);

          this.textBox2.Leave += new System.EventHandler(this.textBox_Leave);
          this.textBox2.Enter += new System.EventHandler(this.textBox_Enter);
      }


hope it will have your work done.:)
 
Share this answer
 
Comments
Member 13985914 26-Mar-19 9:24am    
your code works fine!
but i have a question can we use this in multiple form by writing in one form and use in multpile forms
Do so lightweight Js and CSS work

function DoBlur(fld)
{
fld.className='normalfld';
}

function DoFocus(fld)
{
fld.className = 'focusfld';
}

.normalfld
{
background-color: #FFFFFF;
}
.focusfld
{
background-color: #FFFFCC;
}

<input type="text" onblur="DoBlur(this);" onfocus="DoFocus(this);" />
 
Share this answer
 
Try this....:)


C#
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
        .onfocus
        {
            border-color:red;
        }
        .onblur
        {
            border-color:;
        }
    </style>
   <script type ="text/javascript">
    function Change(obj, evt)
    {
        if(evt.type=="focus")
            obj.className ="onfocus";
        else if(evt.type=="blur")
           obj.className ="onblur";
    }
   </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:textbox id="TextBox1" runat="server" onfocus="Change(this, event)" onblur="Change(this, event)" xmlns:asp="#unknown"></asp:textbox>
    </form>
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900