Click here to Skip to main content
15,894,343 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am creating class to inherit TextBox for Date.

1. on lost focus i am validating the text to check if it is valid date and if not display message and set focus to same text box to prevent user moving out without correcting date.

below line in code gets underline with red

this.Focus;


Error CS0201 Only assignment, call, increment, decrement, await, and new object expressions can be used as a statement

What I have tried:

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

           if (this.Text == "")
           {

           }
           else
           {



               if (DateTime.TryParseExact(this.Text, "dd/mm/yyyy",
                                   System.Globalization.CultureInfo.InvariantCulture,
                                   System.Globalization.DateTimeStyles.None,
                                   out DateTime theDate))
               {


               }
               else
               {
                 MessageBox.Show("Invalid Date : Date should be in 'DD/MM/YYYY' format");

                   this.Focus;
               }

           }
       }
Posted
Updated 13-Mar-20 22:27pm
Comments
Richard MacCutchan 14-Mar-20 5:24am    
Why not use a DatePicker instead of a TextBox?

1 solution

C#
this.Focus();

Focus is a method of Control class; as such, its calls have to be made with brackets. If you omit the brackets, compiler will search for a property with this name, which it won't find.
 
Share this answer
 
Comments
pravin9455 14-Mar-20 5:11am    
Thanks

i have problem with changing back color also ongotfocus

in below code line Color get underline with red

this.BackColor = Color.red;

Error CS0103 The name 'Color' does not exist in the current context


protected override void OnGotFocus(EventArgs e)
{
base.OnGotFocus(e);
if (this.ReadOnly == true)
{
this.BackColor = Color.red;


}

else
{
this.BackColor = Color.SeaGreen;
}



}
phil.o 14-Mar-20 5:18am    
You are missing a using System.Drawing; directive on top of your code file.
pravin9455 14-Mar-20 5:30am    
thanks
Got it

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