Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. validations for the controls that are dynamically created at runtime

validations for the controls that are dynamically created at runtime

Scheduled Pinned Locked Moved C#
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vasavi p
    wrote on last edited by
    #1

    i need to validate the Textboxes that are created at runtime. The text should not be blank and it should allow only numbers.

    OriginalGriffO A 2 Replies Last reply
    0
    • V vasavi p

      i need to validate the Textboxes that are created at runtime. The text should not be blank and it should allow only numbers.

      OriginalGriffO Offline
      OriginalGriffO Offline
      OriginalGriff
      wrote on last edited by
      #2

      Use a MaskedTextBox[^] instead.

      No trees were harmed in the sending of this message; however, a significant number of electrons were slightly inconvenienced. This message is made of fully recyclable Zeros and Ones

      "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
      "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

      1 Reply Last reply
      0
      • V vasavi p

        i need to validate the Textboxes that are created at runtime. The text should not be blank and it should allow only numbers.

        A Offline
        A Offline
        annathor
        wrote on last edited by
        #3

        maybe validate against a regex? ^\d+$ you could put it in the TextChange event handler method eks.

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        Regex r=new Regex(@"^\d+$");
        Match m=r.Match(textBox1.text);

                if(!m.Success) MessageBox.Show("Wrong input format");
            }
        

        or you could check the content of the textbox when it is submited, in the same fasion, using Regex and Match classes. one idea is to make a class that inherits from the TextBox class, add a string attribute that can hold the regex expression, and use that class instead of the TextBox and store the regex in that variable, then you could just loop through all the controls when the user subits the data, an check it against the regex stored in that control, like this:

        public class ExtTextBox:TextBox
        {
        public string ValidationRegEx; //you shuld use get/set methods insted of setting the attribute as public, but I am lazy today.
        }

        ....

        //create the control
        ExtTextBox newETB=new ExtTextBox();
        ...
        ...
        newETB.ValidationRegex=@"^\d+$";
        ...
        this.Controls.add(newETB);

        //when user submits data
        foreach(Control c in this.Controls)
        {
        if(c is ExtTextBox)
        {
        ExtTextBox tmpC=(ExtTextBox)c;
        Regex r=new Regex(tmpC.ValidationRegex);
        Match m=r.Match(tmpc.Text);

        if(!m.Success)
        {
        //give feedback to user
        }
        }
        }

        modified on Wednesday, August 19, 2009 5:00 AM

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups