0

I am creating several text boxes depending on user choice (1-5).How can I access value of programmatic textbox when text changes.

class starts{
    int i=0;

    .....

    TextBox txtb4 = new TextBox();
    txtb4.Name = "textname" + Convert.ToString(i);
    ArrayText.Add(txtb4);
    System.Drawing.Point p5 = new System.Drawing.Point(120, 15);
    txtb4.Location = p5;
    txtb4.Size = new System.Drawing.Size(80, 30);
    txtb4.Text = stringMy;
    grBox1.Controls.Add(txtb4);
    i++;
}

I can access initial textbox text using code below, but I can't access it after value is changed.

label15.Text = grBox1.Controls["textname0"].Text;
chaliasos
  • 9,659
  • 7
  • 50
  • 87
  • 2
    Do you mean you need to handle the TextChanged event or the control is not found? What is the error? What doesn't work? – MarioDS May 02 '12 at 17:18
  • [SO similar question](http://stackoverflow.com/questions/4953853/onkeyup-event-asp-net) The answer with upvotes provides you with the information you need but you should search before post. Its been answered several times. – Pedro Ferreira May 02 '12 at 17:18
  • As to many box's you can do a foreach Control of the grBox1 – Pedro Ferreira May 02 '12 at 17:19
  • 1
    @PedroFerreira This is not an asp.net textbox, that question does not apply. – asawyer May 02 '12 at 17:20
  • I need to handle TextChanged event but I cannot access textbox name after initialization. – GuyWhoReadsStockoverflow May 02 '12 at 17:22
  • @user1253379 You have several options: 1. use the 'sender' argument 2. make the textbox an instance field (may or may not work in your case) 3. use a lambda for the text changed event and capture the textbox as a closure, rather than using another method. #1 is simple enough, and it's done in the answer currently with the most upvotes so I won't bother providing any implementations myself. – Servy May 02 '12 at 17:27
  • 1
    Add the event handler during the initialization, as in `txtb4.TextChanged += textbox_TextChanged;` – saluce May 02 '12 at 17:30

2 Answers2

4

So, something like...

TextBox txtb4 = new TextBox();
txtb4.Name = "textname" + Convert.ToString(i);
txtb4.TextChanged += textbox_TextChanged;
ArrayText.Add(txtb4);

// ...

void textbox_TextChanged(object sender, EventArgs e)
{
    var textbox = (TextBox)sender;
    // work with textbox
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • Any idea how I can access the textbox outside initialization method, from anywhere in the class? – GuyWhoReadsStockoverflow May 02 '12 at 17:35
  • @user1253379: You will need to declare it outside the method or (better), maintain a collection of these controls. You can use a list or a dictionary if you need to access them via some key. I would advise against using the name of the control as the key. Think of a relationship that is more robust and meaningful (hard for me to say what that is as I don't know why you are creating these text boxes.) – Ed S. May 02 '12 at 17:36
  • Since I would have around 96 textboxes I guess list is the solution. Do I just declare them in the list and refer back to them when I need to take text? – GuyWhoReadsStockoverflow May 02 '12 at 17:38
  • @user1253379: Well, you may want to rethink your overall design if you are planning on presenting your user(s) with 96 textboxes. That is a very cluttered and hard to use UI. However, if that's the way you are going then yes, create them, add them to the list or dictionary, and look them up later when needed. – Ed S. May 02 '12 at 18:00
  • How would I about adding them I was just trying do do test: TextBox textname0; at beginning of class but it doesn't pick up any value. – GuyWhoReadsStockoverflow May 02 '12 at 18:08
2

Add an event handler

txtb4.TextChanged += Txtb4_TextChanged;

Declare the handler like this

static void Txtb4_TextChanged(object sender, EventArgs e)
{
    string s = txtb4.Text;
    ...
}    

You create textboxes dynamically; however your code looks not very dynamic. Try this

List<TextBox> _textBoxes = new List<TextBox>();
int _nextTextBoxTop = 15;

private void AddTextBox(string initialText)
{
    var tb = new TextBox();
    tb.Name = "tb" + _textBoxes.Count;
    _textBoxes.Add(tb);
    tb.Location = new Point(120, _nextTextBoxTop);
    _nextTextBoxTop += 36;
    tb.Size = new Size(80, 30);
    tb.Text = initialText;
    tb.TextChanged += TextBox_TextChanged
    grBox1.Controls.Add(tb);
}

static void TextBox_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    string s = tb.Text;
    ...
}    

And I would not access textboxes through grBox1.Controls["textname0"].Text;. The textbox list is a better choice, as you can access it through a numeric index instead of a control name

string s = _textBoxes[i].Text;
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188