.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> Windows Form Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Validate a Usercontrol textbox in C#.net

Posted By:Narayanan       Posted Date: January 05, 2012    Points: 40    Category: Windows Form Controls    URL: http://www.dotnetspark.com  

Today, we will discuss about create a user control in C#.net.It is a powerful and Dynamic control in .NET. It is a flexible control.
 

Now we will go to our class:

 Step: 1

  The following steps are the Creation of Usercontrol.

   Create Project and Right Click on the Project >> Add>>New Items , Click User control in the window and give a name of the User Control .

Eg: testUsercontrol

 

Step: 2

     Now, Select a Label, Textbox Error Provider in the toolbox.

Step: 3

 

  Use following codes in the User Control Code section.

 public string txtChangges
    {

        get { return textBox1.Text; }

        set { textBox1.Text = value; }

    }



    public System.Drawing.Color txtBackColor
    {

        get { return textBox1.BackColor; }

        set { textBox1.BackColor = value; }



    }



    public System.Drawing.Color txtForeColor
    {

        get { return textBox1.ForeColor; }

        set { textBox1.ForeColor = value; }



    }



    public bool txtReadOnly
    {

        get { return textBox1.ReadOnly; }

        set { textBox1.ReadOnly = value; }



    }



    public bool multipline
    {

        get { return textBox1.Multiline; }

        set { textBox1.Multiline = value; }

    }

    public int maxlength
    {

        get { return textBox1.MaxLength; }

        set { textBox1.MaxLength = value; }

    }

    public string lblText
    {

        get { return label1.Text; }

        set { label1.Text = value; }

    }

    public void textValidate()
    {

        if (textBox1.Text == "")
        {

            string error_Message = "Enter your " + " " + label1.Text;

            errorProvider1.SetError(textBox1, error_Message);

            textBox1.Focus();

        }

        else
        {

            errorProvider1.SetError(textBox1, "");

        }

    }



    private void textBox1_Validating(object sender, CancelEventArgs e)
    {

        textValidate();

    }



    private void textBox1_TextChanged(object sender, EventArgs e)
    {

        textValidate();

    }

Step: 4

 Rebuild and Run your Project.

Step: 5

Select a user control from your Tools box.

Drap and Drop of testUsercontrol into your Project.

Step: 6

testUsercontrol1.txtBackColor = Color.Wheat;

testUsercontrol1.txtForeColor = Color.Green;

testUsercontrol1.Visible = true;

testUsercontrol1.multipline = true;

testUsercontrol1.maxlength = 5;

testUsercontrol1.lblText = "Customer Name";

testUsercontrol1 - User control name.

Mouse focus on image. User will get "Enter your Customer Name".


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend