1

The Situation is like this: I have multiple textboxes. On the occurrence of textChanged event the textbox should be stored in the array so that I can use it in further functions.

private void txt_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox t;
        t = (TextBox)sender;
    }

Now I have the textbox which was responsible for the event. Now I have to store this and more to come in an array so that these can be accessed elsewhere in another function.

3
  • Why do you need the textbox itself, and not only the value? Also, what is not working? Commented Apr 19, 2012 at 20:10
  • You probably already have private fields that can be used to access your controls (assuming Windows Forms, their declarations should reside in a Form.Designer.cs file). It'd also help if you clarified what you want to do with these UI controls later. Commented Apr 19, 2012 at 20:12
  • 3
    I suspect you'd be better off using a List<TextBox> rather than a TextBox[] array. But as Msonic implied, it's hard to propose a solution without knowing the underlying problem you want to solve. Commented Apr 19, 2012 at 20:12

3 Answers 3

4

You could throw it in a list if ya like. Not sure why you would really want to do this though...

List<TextBox> txtbxList = new List<TextBox>();

private void txt_TextChanged(object sender, TextChangedEventArgs e)
    {
        TextBox t;
        t = (TextBox)sender;
        txtbxList.Add(t);
    }
Sign up to request clarification or add additional context in comments.

5 Comments

I would like to access the textboxes in a loop and compare the text of the textboxes to a series of values according to the index.
@Slump: This code adds the same textbox to the list again and again every time its text changes.
@AnkitYadav: Use a HashSet<TextBox> to add your text boxes to, then you can later enumerate the set and retrieve your text boxes to do the comparisons. However, there is almost always a better way to do what you want; care to add an even larger context (what you really want to achieve)?
@Alan: Thanks for telling that. But I can handle that because once the value is read the textbox is disabled so there would be no duplicate value
@AnkitYadav: OK, fine. Not meaning to be bashful, but this was a crucial bit of info that you did not include in the question. Under normal circumstances (e.g. text boxes that are not disabled immediately upon reading their contents), the accepted solution would clearly lead you down a wrong path.
1

Description

I dont know why you need to store your TextBoxes in a List or Array but you can use the generic List for that.

Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.

Sample

List<TextBox> myTextBoxes = new List<TextBox>();
// Add a TextBox
myTextBoxes.Add(myTextBox);
// get a TextBox by Name
TextBox t = myTextBoxes.Where(x => x.Name == "TextBoxName").FirstOrDefault();

More Information

Comments

0

Assuming you want to store the text from the TextBoxes, you can use a dictionary like this:

private Dictionary<string, string> dictionary = new Dictionary<string, string>();

private void txt_TextChanged(object sender, TextChangedEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    string key = textBox.Name;
    string value = textBox.Text;
    if (!dictionary.ContainsKey(key))
    {
        dictionary.Add(key, value);
    }
    else
    {
        dictionary[key] = value;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.