Hi,
I am generating multiple comboboxes in tablelayoutpanel at runtime. I am taking datasource of combobox from one dimensional array and hence, all the comboboxes are changing when i change just one. So, the solution is to take two dimensional matrix where a given row number is combobox number and column values are the datasource for that combobox, but I do not know the syntax for this....please help.
existing code:
this.tableLayoutPanel1.ColumnCount=2;
for (int j = 0; j < this.tableLayoutPanel1.ColumnCount; j++)
{
for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++)
{
if (j < 1)
{
Label lb = new Label();
lb.Text = string.Format(textFormat, i);
this.tableLayoutPanel1.Controls.Add(lb, j, i);
lb.AutoSize = true;
}
else
{
ComboBox cb = new ComboBox();
cb.Name = string.Format(nameFormat, i,j);
this.tableLayoutPanel1.Controls.Add(cb, j, i);
cb.AutoSize = true;
//one dimensional array is taken as datasource below
cb.DataSource = matN;
}
}
If i define a new array as int [,] matNnew;
how do I write the following
cb.DataSource = matNN[i,];
//i cant leave a blank in above........what should be the syntax here......?
Note that the contents for each combobox are the same.....
Thank you. | | gulti Monday, July 14, 2008 6:10 AM | This is roughly what I meant...
Code Snippet
public Form1() { InitializeComponent();
this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.RowCount = 5; string nameFormat = "cmb_{0}_{1}"; string textFormat = "text_{0}";
Dictionary<string, string[]> dic = new Dictionary<string, string[]>(); // create all dataSources for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++) { for (int j = 0; j < this.tableLayoutPanel1.ColumnCount; j++) { if (j == 0) { Label lbl = new Label(); lbl.Name = "label_" + i.ToString(); lbl.Text = string.Format(textFormat, i); this.tableLayoutPanel1.Controls.Add(lbl, j, i); } else { string[] arr = CreateDataSource(i,j); string name = string.Format(nameFormat, i, j); dic.Add(name, arr); ComboBox cmb = new ComboBox(); cmb.Name = name; cmb.DataSource = dic[name]; this.tableLayoutPanel1.Controls.Add(cmb, j, i); } } }
}
private string[] CreateDataSource(int row, int col) { return new string[] { "asdasd", "dgfsdfs", "cbcvbcvbc" }; } note that the method CreateDataSource creates the DataSource for the specific combo... hth, Lior. | | Lior Salem Tuesday, July 15, 2008 8:06 AM | Instead of useing a two dimentional array, you can use a List of single dimention array.
Code Snippet
List<string[]> lst = new List<string[]>(); and get the values as lst .
Alternatively, you can use a Dictionary<string, string[]> where the key would be the name of the comboBox and you will be able to do this:
Code Snippet
Dictionary<string, string[]> dic = new Dictionary<string, string[]>(); ... cb.DataSource = dic[cb.Name]; hth, Lior.
| | Lior Salem Monday, July 14, 2008 12:33 PM | Thank you very much!
I tried getting checkboxes for each question (my last question which you helped), but the system takes a lot of time to generate a formif number of questions/checkboxes is large (also is is not very visually nice when the size is huge)............So, i decided to do it by comboboxes.....
I am learning ......
Thanks again! | | gulti Tuesday, July 15, 2008 1:41 AM | I tried this, why doesnt thiswork (same problem, if i change one combobox, all will change)....Thanks!
string [] arrN = new string[numN];
string[] arrP = new string[numP];
for (int i = 1; i < numN+1; i++)
{
arrN[i-1] = Convert.ToString(i);
}
Dictionary<string, string[]> dic = new Dictionary<string, string[]>();
for (int i = 1; i < numP + 1; i++)
{
arrP[i - 1] = Convert.ToString(i);
dic.Add(arrP[i - 1], arrN);
}
// Add controls to the tableLayoutPanel
for (int j = 0; j < this.tableLayoutPanel1.ColumnCount; j++)
{
for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++)
{
if (j < 1)
{
Label lb = new Label();
lb.Text = string.Format(textFormat, i + 1);
// add the control to the TableLayoutPanel
this.tableLayoutPanel1.Controls.Add(lb, j, i);
lb.AutoSize = true;
}
else
{
ComboBox cb = new ComboBox();
cb.Name = string.Format(nameFormat, j, i);
// add the control to the TableLayoutPanel
this.tableLayoutPanel1.Controls.Add(cb, j, i);
cb.AutoSize = true;
cb.DataSource = dic[arrP ];
}
}
| | gulti Tuesday, July 15, 2008 6:56 AM | This is roughly what I meant...
Code Snippet
public Form1() { InitializeComponent();
this.tableLayoutPanel1.ColumnCount = 2; this.tableLayoutPanel1.RowCount = 5; string nameFormat = "cmb_{0}_{1}"; string textFormat = "text_{0}";
Dictionary<string, string[]> dic = new Dictionary<string, string[]>(); // create all dataSources for (int i = 0; i < this.tableLayoutPanel1.RowCount; i++) { for (int j = 0; j < this.tableLayoutPanel1.ColumnCount; j++) { if (j == 0) { Label lbl = new Label(); lbl.Name = "label_" + i.ToString(); lbl.Text = string.Format(textFormat, i); this.tableLayoutPanel1.Controls.Add(lbl, j, i); } else { string[] arr = CreateDataSource(i,j); string name = string.Format(nameFormat, i, j); dic.Add(name, arr); ComboBox cmb = new ComboBox(); cmb.Name = name; cmb.DataSource = dic[name]; this.tableLayoutPanel1.Controls.Add(cmb, j, i); } } }
}
private string[] CreateDataSource(int row, int col) { return new string[] { "asdasd", "dgfsdfs", "cbcvbcvbc" }; } note that the method CreateDataSource creates the DataSource for the specific combo... hth, Lior. | | Lior Salem Tuesday, July 15, 2008 8:06 AM | Thank you very much Lior. Have a nice day.
| | gulti Tuesday, July 15, 2008 8:35 AM |
|