Sully Sully - 3 months ago 14
C# Question

Populate TextBoxes from a List

I am trying to populate TextBoxes from a list. I have been able to populate ComboBoxes with comboList:

var comboList = new System.Windows.Forms.ComboBox[4];

comboList[0] = cmbSite1Asset;
comboList[1] = cmbSite2Asset;
comboList[2] = cmbSite3Asset;
comboList[3] = cmbSite4Asset;

List<CRCS.CAsset> assets = _rcs.Assets;
foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;

for (int i = 0; i < 4; ++i)
{
comboList[i].Items.Add(id);
}
}


But when I try and apply the same principle to TextBoxes

var aosList = new System.Windows.Forms.TextBox[8];

aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;

foreach (CRCS.CAsset asset in assets)
{
string id = asset.ID;

for (int n = 0; n < 8; ++n)
{
aosList[n].Items.Add(id);
}
}


TextBox does not like Items.Add ( aosList[n]Items.Add(id); )
I am looking fore a reference or guidance resolving this issue. Thanks!

Answer

A Combobox is a collection of items, and so has an Items property from which you can add/remove to change it's contents. A Textbox is just a control that displays some text value, so it has a Text property which you can set/get, and which denotes the string that is displayed.

System.Windows.Forms.TextBox[] aosList = new System.Windows.Forms.TextBox[8];

aosList[0] = txtAsset1;
aosList[1] = txtAsset2;
aosList[2] = txtAsset3;
aosList[3] = txtAsset4;
aosList[4] = txtAsset5;
aosList[5] = txtAsset6;
aosList[6] = txtAsset7;
aosList[7] = txtAsset8;

for (int n = 0; n < 8; ++n)
{
    aosList[n].Text = assets[n].ID; // make sure you have 8 assets also!
}
Comments