Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
4.00/5 (1 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

1 solution

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>:
http://msdn.microsoft.com/en-us/library/9eekhta0.aspx[^].

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:
C#
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:
C#
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:
C#
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;
   ++index;
}


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:
C#
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.

—SA
 
Share this answer
 
v5
Comments
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.
--SA

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900