Click here to Skip to main content
Licence 
First Posted 25 Mar 2005
Views 49,758
Bookmarked 11 times

Dynamic Control Validation at Runtime using a Container Control

By | 25 Mar 2005 | Article
Dynamic control validation (TextBox, ComboBox) at runtime using a container control.

Introduction

Child controls (textbox, combo etc.) in a .NET container controls (like a Form), can be accessed and their properties set at runtime dynamically by accessing the control array.

Using the Code

The following example deals with runtime validation (which checks for compulsory data input) of Windows Forms controls like TextBox, RichTextBox and ComboBox but can be expanded to handle validation and appearance of all types of Windows Forms controls.

To implement this example, you must create a database table which would hold the configuration for all the controls in the forms. You can also use an XML file instead of a database table.

I have attached the SQL to create the MS SQL 2000 database as well as the database backup. You have to create and configure the database at your end. After you restore the database, you will see that the table 'Ctlvalidation' has the form name, the control name, message and compulsory flag fields.

ID Form CtlName Msg Compulsory
1 Form1 textBox1 Name 0
2 Form1 comboBox1 Age 1
3 Form1 richTextBox1 Comments 0

Our application will read this table and depending upon the flag set in the Compulsory field, will make the control compulsory or non compulsory.

In our application, we create the CompulsoryLogic class, which we can then call from any form in the application. In this class, we first create a database connection and fill the DataSet ds.

string source = "server=CHAYAN;uid=mine;pwd=mine;database=CtlValidation";
string select = 
   "Select CtlName,Msg from CtlValidation where Compulsory>0 and Form ='"+
   frm.Name.ToString()+"'";
SqlConnection conn = new SqlConnection(source);
SqlDataAdapter da = new SqlDataAdapter(select,conn);
DataSet ds = new DataSet();
da.Fill(ds,"CtlValidation");

Then we loop through each DataRow in the DataSet and for each control in the form, check the validation for the required control (textbox, rich textbox or combo).

foreach(DataRow dr in ds.Tables[0].Rows)
{
  foreach (Control c in frm.Controls)
  {
    if (c.Name == dr[0].ToString())
    { 
      //check to see if the control is of type textbox or a richtextbox
      if (c.GetType().ToString().EndsWith("TextBox"))
      {
        //check to see if the box is empty
        if (c.Text =="")
        {
          MessageBox.Show(dr[1].ToString()+" is a compulsory field",
               "Compulsory Field",0);
          c.BackColor = Color.AliceBlue;
          c.Focus();
        }
      }
    //check to see if the control is of type combo
    // More code here
  }
}

Once this is done, we can call the CompulsoryLogic class from any form where a control validation is required.

private void button1_Click(object sender, EventArgs e)
{

  CompulsoryLogic.ValidateCtl(this);

}

A very similar concept can be used in providing menu based security to user groups in a desktop application but we will discuss that in a separate article.

Prerequisites

  1. MS SQL 2000
  2. VS.NET 2003 (C#)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Chayan

Web Developer

India India

Member

Chayan Ray has been working as a Technical Consultant in a CMM level 5 company in India. His technical domain includes ASP.NET, C#, PHP, Perl, Cold Fusion, MySQL and MSSQL 2000.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 4 Pinmemberfamily52117:50 13 Feb '12  
QuestionWill it work for web application? PinmemberGanesan S3:43 14 Aug '07  
QuestionAttachments? PinmemberPablo123417:34 31 Oct '05  
AnswerRe: Attachments? PinmemberChayan7:04 7 Nov '05  
GeneralDon't need reflection for that. PinmemberDanielJC23:40 25 Mar '05  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 26 Mar 2005
Article Copyright 2005 by Chayan
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid