locked
how i can do this !? Can anyone Help . ? RRS feed

  • Question

  • Hi,Everyone ..

    i want to make a programe when the user enter a number the programe translate this number to textboxes . for example if the user enterted "20" i want the programe to make 20 textbox (but not just an empty textboxes i want to write a code in each on of these textboxes) . so what is the best way to do this ..
    and sorry for my bad english .
     
    thanks, Abd EL Rahman .
    ---------------



     
    Sunday, January 3, 2010 6:28 AM

Answers

  • void AddTextBoxesToForm(Form f, int numberOfTextBoxes)
    {
        TextBox[] tbs = new TextBox[numberOfTextBoxes];
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i] = new TextBox();
            tbs[i].Text = "TextBox #" + (i + 1);
        }
        for (int i = 1; i < tbs.Length; i++)
        {
            tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
        }
        f.Controls.AddRange(tbs);
    }
    

    With best regards, Yasser Zamani
    Sunday, January 3, 2010 6:41 AM
  • but how i can do this .. ( in code) ..

    In addition of Yasser.Zamani.Iran answer....  here is the code that implements what u need,

    private void Form2_Click(object sender, EventArgs e)
    {
        AddTextBoxesToForm(this, 5);
    }
    
    void AddTextBoxesToForm(Form f, int numberOfTextBoxes)
    {
        TextBox[] tbs = new TextBox[numberOfTextBoxes];
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i] = new TextBox();
            // the below code binds a piece of method at runtime control, u can have any code over there
            tbs[i].KeyPress += new System.Windows.Forms.KeyPressEventHandler(Pr_NumericValidation);
        }
    
        for (int i = 1; i < tbs.Length; i++)
        {
            tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
        }
    
        f.Controls.AddRange(tbs);
    }
    
    private void Pr_NumericValidation(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8)
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
    }

    Pr_NumericValidation - this validates so it can take only numeric values inside the runtime textbox what we have created now..

    hope this helped....



    Narayanan Dayalan -------- Please "Mark As Answer", if my answer works well with ur Query
    Monday, January 4, 2010 4:47 AM
  • You're welcome. i update my suggestion to return to you added array. so you can access to text-boxes via that array. see following:
    class Form1 : Form
    {
    TextBox[] tbs;
    
    //Load event handler
    Form1_Load()
    {
       myUtilities.AddTextBoxesToForm(this, 10, out tbs);
    }
    
    //Shown event handler
    Form1_Shown()
    {
          //accessing TextBox#7
          tbs[6].Text="Hi, i am TextBox#7";
    }
    }
    static class myUtilities
    {
    public static void AddTextBoxesToForm(Form f, int numberOfTextBoxes,out TextBox[] tbs)
    {
        tbs = new TextBox[numberOfTextBoxes];
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i] = new TextBox();
            tbs[i].Name = "txt" + (i + 1);
        }
        for (int i = 1; i < tbs.Length; i++)
        {
            tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
        }
        f.Controls.AddRange(tbs);
    }
    }

    With best regards, Yasser Zamani
    Wednesday, January 6, 2010 6:48 AM
  • You're welcome ;)
    It works but you can't see labels because you set Text property of them to empty string i.e. "". check following:
    public void addlabels(Form f, int no)
     {
     LinkLabel[] tbs = new LinkLabel[no];
     for (int i = 0; i < tbs.Length; i++)
     {
     tbs[i] = new LinkLabel();
     tbs[i].Text = "I am label #" + (i+1) + ", You see me NOW";
     }
     for (int i = 1; i < tbs.Length; i++)
     {
     tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
     }
     f.Controls.AddRange(tbs);
     }

    With best regards, Yasser Zamani
    Friday, January 8, 2010 6:49 AM

All replies

  • void AddTextBoxesToForm(Form f, int numberOfTextBoxes)
    {
        TextBox[] tbs = new TextBox[numberOfTextBoxes];
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i] = new TextBox();
            tbs[i].Text = "TextBox #" + (i + 1);
        }
        for (int i = 1; i < tbs.Length; i++)
        {
            tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
        }
        f.Controls.AddRange(tbs);
    }
    

    With best regards, Yasser Zamani
    Sunday, January 3, 2010 6:41 AM
  • but how i can write code in each on of these ... 

    Sunday, January 3, 2010 10:30 AM
  • I think, the best way is, when you create TextBox dynamically you add handle to any even you want, but with this way you have one handler for all of your

    textboxs and you should use any condition statment to detemine witch sender fire event and then write any code for each one.
    Soroush Sarabi Microsoft MCP , MCTS
    Sunday, January 3, 2010 3:02 PM
  • but how i can do this .. ( in code) ..

    Sunday, January 3, 2010 5:09 PM
  • but how i can do this .. ( in code) ..

    In addition of Yasser.Zamani.Iran answer....  here is the code that implements what u need,

    private void Form2_Click(object sender, EventArgs e)
    {
        AddTextBoxesToForm(this, 5);
    }
    
    void AddTextBoxesToForm(Form f, int numberOfTextBoxes)
    {
        TextBox[] tbs = new TextBox[numberOfTextBoxes];
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i] = new TextBox();
            // the below code binds a piece of method at runtime control, u can have any code over there
            tbs[i].KeyPress += new System.Windows.Forms.KeyPressEventHandler(Pr_NumericValidation);
        }
    
        for (int i = 1; i < tbs.Length; i++)
        {
            tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
        }
    
        f.Controls.AddRange(tbs);
    }
    
    private void Pr_NumericValidation(object sender, KeyPressEventArgs e)
    {
        if (e.KeyChar >= 48 && e.KeyChar <= 57 || e.KeyChar == 8)
        {
            e.Handled = false;
        }
        else
        {
            e.Handled = true;
        }
    }

    Pr_NumericValidation - this validates so it can take only numeric values inside the runtime textbox what we have created now..

    hope this helped....



    Narayanan Dayalan -------- Please "Mark As Answer", if my answer works well with ur Query
    Monday, January 4, 2010 4:47 AM
  • Sorry, i am confused, What do you mean? writing code in textboxes!!!
    What do you wish to see in textboxes?
    Thanks
    With best regards, Yasser Zamani
    Monday, January 4, 2010 5:49 AM
  • Thank you all guys u really helped me thanks to "Yasser.Zamani.Iran" , "Narayanan Dayalan" & Soroush Sarabi .

    Monday, January 4, 2010 10:58 PM
  • You're welcome Abd El Rahman.tech.;)
    With best regards, Yasser Zamani
    Tuesday, January 5, 2010 5:54 AM
  • ---------------------------------------------------------------------------------------------------------------------

    Sorry i know that i am bothring you with  my questions  .. but how i can access the text-boxes that i created before in the programe

    (i mean to deal with them as am dealing with anything in the program ) i mean to put text on them , remove text , read the vlaues written in them , valdiate somthing whenever i want, not just at the time of creating them . cuz when i try to access them out side of (addtextboxtoform) method i get an error message .

    ----------
    thanks ..  .
    abdelrahman tal'at - egypt .
    Wednesday, January 6, 2010 2:01 AM
  • You're welcome. i update my suggestion to return to you added array. so you can access to text-boxes via that array. see following:
    class Form1 : Form
    {
    TextBox[] tbs;
    
    //Load event handler
    Form1_Load()
    {
       myUtilities.AddTextBoxesToForm(this, 10, out tbs);
    }
    
    //Shown event handler
    Form1_Shown()
    {
          //accessing TextBox#7
          tbs[6].Text="Hi, i am TextBox#7";
    }
    }
    static class myUtilities
    {
    public static void AddTextBoxesToForm(Form f, int numberOfTextBoxes,out TextBox[] tbs)
    {
        tbs = new TextBox[numberOfTextBoxes];
        for (int i = 0; i < tbs.Length; i++)
        {
            tbs[i] = new TextBox();
            tbs[i].Name = "txt" + (i + 1);
        }
        for (int i = 1; i < tbs.Length; i++)
        {
            tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
        }
        f.Controls.AddRange(tbs);
    }
    }

    With best regards, Yasser Zamani
    Wednesday, January 6, 2010 6:48 AM
  • ---------------------

    man u r amazing .. thanks alot .. (hopes you become an mvp)  .

    ----------------------------------------------
    Wednesday, January 6, 2010 10:51 AM
  • Thanks for your kindness.  becoming an MVP is my wish. ;-)
    With best regards, Yasser Zamani
    Wednesday, January 6, 2010 2:57 PM
  • i tried to make another one  for creating link-labels (using the the same method you gave me with some small changes .) but it didn't work ... so how i can fix that ..
    and this is the code ...

     public void addlabels(Form f, int no)
     {
     LinkLabel[] tbs = new LinkLabel[no];
     for (int i = 0; i < tbs.Length; i++)
     {
     tbs[i] = new LinkLabel();
     tbs[i].Text = "";
     }
     for (int i = 1; i < tbs.Length; i++)
     {
     tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
     }
     f.Controls.AddRange(tbs);
     }
    i hope i am not disturbing u with my questions .
    abdelrahman tala'at - egypt .
    Friday, January 8, 2010 12:51 AM
  • You're welcome ;)
    It works but you can't see labels because you set Text property of them to empty string i.e. "". check following:
    public void addlabels(Form f, int no)
     {
     LinkLabel[] tbs = new LinkLabel[no];
     for (int i = 0; i < tbs.Length; i++)
     {
     tbs[i] = new LinkLabel();
     tbs[i].Text = "I am label #" + (i+1) + ", You see me NOW";
     }
     for (int i = 1; i < tbs.Length; i++)
     {
     tbs[i].Location = new Point(tbs[i - 1].Left, tbs[i - 1].Bottom + 10);
     }
     f.Controls.AddRange(tbs);
     }

    With best regards, Yasser Zamani
    Friday, January 8, 2010 6:49 AM
  • Thanks again .. .
    Friday, January 8, 2010 10:26 AM