locked
How to create dyanmic textbox with keydown event RRS feed

  • Question

  • Hi,

    Need to create a dynamic textbox with keydown and textchanged events. pls help me.

    Thanks in advance.

     

    • Changed type CoolDadTx Tuesday, May 11, 2010 1:34 PM It's a question
    • Moved by CoolDadTx Tuesday, May 11, 2010 1:34 PM Winforms related (From:Visual C# General)
    Tuesday, May 11, 2010 4:46 AM

Answers

  • Hi.

    Here you go:

          TextBox tb = new TextBox();
          tb.KeyDown += new KeyEventHandler(tb_KeyDown);
          tb.TextChanged += new EventHandler(tb_TextChanged);
    
        void tb_TextChanged(object sender, EventArgs e)
        {
          // DO ACTIONS
        }
    
        void tb_KeyDown(object sender, KeyEventArgs e)
        {
           // DO ACTIONS
        }

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

     

     

    • Proposed as answer by Noam B Tuesday, May 11, 2010 1:54 PM
    • Marked as answer by Helen Zhou Monday, May 17, 2010 8:27 AM
    Tuesday, May 11, 2010 10:57 AM
  • Hi,

    Try to use the code given by Noam. Also you need to add the dynamic textbox control to the Form's Controls collection. For example,

    private void button1_Click(object sender, EventArgs e)
    {
      TextBox tb1 = new TextBox();
      tb1.Left = 100;
      tb1.Top = 100;
      tb1.KeyDown += new KeyEventHandler(TextBox_KeyDown);
      tb1.TextChanged += new EventHandler(TextBox_TextChanged);
      this.Controls.Add(tb1);
    }
    
    
    void TextBox_TextChanged(object sender, EventArgs e)
    {
      // Code to handle TextChanged event goes here
    }
    
    void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
      // Code to handle KeyDown event goes here
    }
    

    • Marked as answer by Helen Zhou Monday, May 17, 2010 8:27 AM
    Tuesday, May 11, 2010 11:14 AM
  • Hi,

    Do you wan to set the same keydown event for all those 5 dynamically added textboxes or each textbox will havt its own keydown event?

    You can use a common keydown event  for all 5 textboxes. You can identify each textbox using sender object. Try this following code.

    private void button1_Click(object sender, EventArgs e)
    {
      TextBox tb1 = new TextBox();
      tb1.Name = "tb1";
      tb1.Left = 100;
      tb1.Top = 100;
      tb1.KeyDown += new KeyEventHandler(TextBox_KeyDown);
      tb1.TextChanged += new EventHandler(TextBox_TextChanged);
      this.Controls.Add(tb1);
    
      TextBox tb2 = new TextBox();
      tb2.Name = "tb2";
      tb2.Left = 100;
      tb2.Top = 150;
      tb2.KeyDown += new KeyEventHandler(TextBox_KeyDown);
      tb2.TextChanged += new EventHandler(TextBox_TextChanged);
      this.Controls.Add(tb2);
    
      //Continue code to add 5 textboxes dynamically in the same way
    }
    
    void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
      // Code to handle KeyDown event goes here
    
      TextBox txt = (TextBox)sender;
      MessageBox.Show(txt.Name);
    
      switch (txt.Name)
      {
        case "tb1":
          //Write code to handle tb1 textbox
          break;
    
        case "tb2":
          //Write code to handle tb2 textbox
          break;
    
        //Write code to handle all the 5 textboxes in a separate case statement
      }
    }
    
    void TextBox_TextChanged(object sender, EventArgs e)
    {
      // Code to handle TextChanged event goes here
      TextBox txt = (TextBox)sender;
      MessageBox.Show(txt.Name);
    
      switch (txt.Name)
      {
        case "tb1":
          //Write code to handle tb1 textbox
          break;
    
        case "tb2":
          //Write code to handle tb2 textbox
          break;
    
        //Write code to handle all the 5 textboxes in separate case statement
      }
    }

     

    Friday, May 14, 2010 6:43 AM

All replies

  • Hi.

    Here you go:

          TextBox tb = new TextBox();
          tb.KeyDown += new KeyEventHandler(tb_KeyDown);
          tb.TextChanged += new EventHandler(tb_TextChanged);
    
        void tb_TextChanged(object sender, EventArgs e)
        {
          // DO ACTIONS
        }
    
        void tb_KeyDown(object sender, KeyEventArgs e)
        {
           // DO ACTIONS
        }

    Noam B
    _________________________________________________________

    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...

     

     

    • Proposed as answer by Noam B Tuesday, May 11, 2010 1:54 PM
    • Marked as answer by Helen Zhou Monday, May 17, 2010 8:27 AM
    Tuesday, May 11, 2010 10:57 AM
  • Hi,

    Try to use the code given by Noam. Also you need to add the dynamic textbox control to the Form's Controls collection. For example,

    private void button1_Click(object sender, EventArgs e)
    {
      TextBox tb1 = new TextBox();
      tb1.Left = 100;
      tb1.Top = 100;
      tb1.KeyDown += new KeyEventHandler(TextBox_KeyDown);
      tb1.TextChanged += new EventHandler(TextBox_TextChanged);
      this.Controls.Add(tb1);
    }
    
    
    void TextBox_TextChanged(object sender, EventArgs e)
    {
      // Code to handle TextChanged event goes here
    }
    
    void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
      // Code to handle KeyDown event goes here
    }
    

    • Marked as answer by Helen Zhou Monday, May 17, 2010 8:27 AM
    Tuesday, May 11, 2010 11:14 AM
  • Hi,

    My doubt is if i added 5 dynamic textboxes then how to set keydown event for those 5 textboxes.

    thanks in advance.

    Friday, May 14, 2010 6:22 AM
  • Hi,

    Do you wan to set the same keydown event for all those 5 dynamically added textboxes or each textbox will havt its own keydown event?

    You can use a common keydown event  for all 5 textboxes. You can identify each textbox using sender object. Try this following code.

    private void button1_Click(object sender, EventArgs e)
    {
      TextBox tb1 = new TextBox();
      tb1.Name = "tb1";
      tb1.Left = 100;
      tb1.Top = 100;
      tb1.KeyDown += new KeyEventHandler(TextBox_KeyDown);
      tb1.TextChanged += new EventHandler(TextBox_TextChanged);
      this.Controls.Add(tb1);
    
      TextBox tb2 = new TextBox();
      tb2.Name = "tb2";
      tb2.Left = 100;
      tb2.Top = 150;
      tb2.KeyDown += new KeyEventHandler(TextBox_KeyDown);
      tb2.TextChanged += new EventHandler(TextBox_TextChanged);
      this.Controls.Add(tb2);
    
      //Continue code to add 5 textboxes dynamically in the same way
    }
    
    void TextBox_KeyDown(object sender, KeyEventArgs e)
    {
      // Code to handle KeyDown event goes here
    
      TextBox txt = (TextBox)sender;
      MessageBox.Show(txt.Name);
    
      switch (txt.Name)
      {
        case "tb1":
          //Write code to handle tb1 textbox
          break;
    
        case "tb2":
          //Write code to handle tb2 textbox
          break;
    
        //Write code to handle all the 5 textboxes in a separate case statement
      }
    }
    
    void TextBox_TextChanged(object sender, EventArgs e)
    {
      // Code to handle TextChanged event goes here
      TextBox txt = (TextBox)sender;
      MessageBox.Show(txt.Name);
    
      switch (txt.Name)
      {
        case "tb1":
          //Write code to handle tb1 textbox
          break;
    
        case "tb2":
          //Write code to handle tb2 textbox
          break;
    
        //Write code to handle all the 5 textboxes in separate case statement
      }
    }

     

    Friday, May 14, 2010 6:43 AM
  • Hi,

     

     i tried to add dynamic textboxes in a user control and how to set keydown event for textbox from my main project.

    thanks in advance

    Wednesday, May 26, 2010 6:31 AM