Click here to Skip to main content
13,251,998 members (60,208 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I have an application containing class which also contains a method that returns IEnumerable<string>
Now, I can get the data from IEnumerable<string>. Now, I have a problem that I have to assign these data into several TextBoxes. Suppose, I have 10 TextBoxes named txtSomething_1,txtSomething_2 and like this txtSomething_10. I have to assign first data get from IEnumerable<string> collection to first textbox(txtSomething_1), second data to second TextBox and so on. How to do this? Help me out please. Thanks a lot.
Posted 28-Jun-12 7:14am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

First of all, you have made a mistake in first place by creating so many text boxes using the designer. You should understand that the designer is for simple ad-hoc totally manual "programming"; if you need some repeating pattern like 10 text boxes, adding them all is a manual monkey job.

Now, about the IEmumerable. First of all, you need to use only the generic interface System.Collections.Generic.IEnumerable<T>:[^].

What to use for T? Let's assume you have something which provides the text for all your text boxes. This "something" shall also have the property Text:
interface ITextProvider {
    string Text { get; set; }
} //interface ITextProvider
//some implementation:
class MyTextProvider : SomeBaseClass, ITextProvider {
    string ITextProvider.Text { get { return text; } set { text = value; } }
    string text;
    // probably, it does something else
} //interface ITextProvider
IEnumerable<ITextProvider> textPopulator = //... generic list with the same generic type or something like that

Now, how to work with text boxes?
There are two solutions. First one is dumb, in the case you don't want to fix your (really bad) designer-made mistake. Here is the work-around:
TextBox[] textBoxes = new TextBox[] { txtSomething_1, txtSomething_2, /* ... */};

Even the names of those text boxes are unacceptable, violate (good) Microsoft naming conventions. Yes, the designer violates them. You always should rename all the auto-generated names to something semantic. But in this case, you should not use the designer. OK, later about it; now, let's see how to populate it. It's trivial:
int index = 0;
foreach(ITextProvider textProvider in textPopulator) {
   if (textBoxes.Length < index + 1) break; //deal with non-matching lengths somehow to guard against de-referencing of non existent element
   textBox[index].Text = textProvider.Text;

Now, if you really want to write correct code, you should create your text boxes properly in first place. Let's assume you put them in some container control like a panel:
Control parent = SomePanel;
int left = //...
int verticalGap = //...
int count = textPopulator.Length; //if textPopulator is not created yet, you should know the count anyway
TextBox[] textBoxes = new TextBox[count];
for (int index; index < count; ++index) {
    TextBox textBox = new TextBox();
    textBox.Text = textPopulator[index].Text; //defer it if textPopulator is not created yet
    textBox.Left = left;
    textBox.Top = index * (textBox.Height + gap); //let's arrange them vertically
    textBox.Parent = parent; //same as parent.Controls.Add(textBox);
    textBoxes[index] = textBox;

Something like this.

Jyoti Choudhury 28-Jun-12 13:06pm
Thanks a lot Brother. It solved my problem and I did the second solution, you provided not to create so many text boxes in designer mode...Thanks...:)
Sergey Alexandrovich Kryukov 28-Jun-12 13:09pm
Great! this is the right thing to do.
And you are very welcome.
Good luck, call again.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171114.1 | Last Updated 28 Jun 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100