Click here to Skip to main content
15,890,506 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to capture the current back color of text box on GotFocus of text box . then change the back color to another and when LostFocus put back the original color back to text box in my Inherited TextBox class. Tried to do it but having errors. Can anyone help.

Errors
Cannot implicitly convert type 'System.Drawing.Color' to 'string'

The name 'BckClr' does not exist in the current context

Thanks

What I have tried:

class TextBoxGreen : TextBox
    {

        protected override void OnGotFocus(EventArgs e)
        {
            base.OnGotFocus(e);

            string BckClr = this.BackColor;// capturing current back color

           
                this.BackColor = Color.Lime;
           
        }

        protected override void OnLostFocus(EventArgs e)
        {
            base.OnLostFocus(e);
            this.BackColor = BckClr; // setting original back color on lost focus
        }
            
                      
    }
Posted
Updated 31-Mar-20 16:10pm

string BckClr = this.BackColor;// capturing current back color

The error is telling you that you are trying to take a Color and store it into a string variable. You cannot do that. this.BackColor is not a string.

So, instead of declaring BckClr as a string, declare it as a System.Drawing.Color. Then you can store it.
 
Share this answer
 
Comments
BillWoodruff 31-Mar-20 17:18pm    
just changing the Type is not going to fix this mess :)
ZurdoDev 31-Mar-20 17:20pm    
Baby steps. :)
a. are you creating a design-time component for a WinForm project that you want to appear in the ToolBox ?

b. why do you think you can use a 'string for a variable to hold a 'Color value ?

c. why do you think a variable created in a Method can be accessed outside the scope of that Method ?

Your question suggests to me that you need to do some study of the basic semantics, and structure, of C# and .NET.

I, and other people here, can help you get focused.

Do you have a good introductory book ? What are you doing now to learn ?
 
Share this answer
 
v2
Comments
pravin9455 2-Apr-20 16:58pm    
Hi
I am accountant and programming is my hobby and develop small tools to assist in office work. I have been working in VB.net for long time and recently moving to C#. I wish to develop accounting software and hope c# will or is appropriate programming language to move on?

Regarding my question I have created class to inherit text box and wherever i use this control i make the back color to lime to high lite when user set focus on this text box and change the color back to any hardcore color when loses focus.
I wanted to set the color back to one which was set at design time on lost focus and not hardcore in class which i used to do in VB.net.

As far as learning i look for google search and don't have any introductory book.
BillWoodruff 3-Apr-20 19:19pm    
"As far as learning i look for google search and don't have any introductory book." Then, you have not looked carefully: search CodeProject. You'll find there are recommendations for books (some free) in several places.

There's nothing wrong about being a beginner: we all were, once :) But, to not recognize you need to learn some basics that are fundamental to even starting to learn a complex new "language," is ... not wise.

"rahi gulzar to phool kilenge:" Kabir
This SO answer should help you achieve your goal:

WinForms - Change textbox BackColor on focus in a Windows application[^]

/ravi
 
Share this answer
 
v2

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