Use rowIndex on TableLayoutPanel

chalupabatman

Member
Joined
Nov 6, 2014
Messages
8
Programming Experience
Beginner
I am dynamically creating text boxes & labels based off of data being pulled from a table. Sometimes the field will have 2 possible answers, so I need each answer to be on the same line as the field
C#:
field ----  answer
EmpID     ID
Phone     Phone1   Phone2
Address  Address

I was attempting to use the rowindex but I still can't get the syntax down so that it displays as I need, like the example above. This is the model, I am working with
C#:
int rowinde = 0;
foreach (string label in c.LabelsNeeded)
{
    //Adding in a label
    Label lbl = new Label();
    lbl.Name = "lbl_" + index;
    lbl.Text = label;
    lbl.AutoSize = true;    
    tableLayoutPanel1.Controls.Add(lbl);
    //Adding in a textbox
    TextBox tbx = new TextBox();
    tbx.Name = "txt_" + index;
    tbx.AutoSize = true;
    //Only increment the index if the label is not null
    //meaning there was a new label added
    if (string.IsNullOrEmpty(lbl.Text) != true) { tableLayoutPanel1.Controls.Add(tbx); }
    else { tableLayoutPanel1.Controls.Add(tbx, rowindex++) } 
    index++
}

EDIT -- I guess a better way of looking at what I want to accomplish is the column/row location on the TLP of where I want to add my data. This is how I want to add my information
C#:
EmpID - add at - column1, row1 -----ID - add at - column2, row1
Phone - add at - column1, row2 -----Phone1 - add at - column2, row2 --- Phone2 - add at - column2, row 2 (Phone2 should be added at the same row, but moved slightly over not on top of Phone1)
Address - add at - column1, row3 ---- Address - add at - column2, row3
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
I already told you what to do and you seem to have completely ignored it. I said that when you call Controls.Add you specify the row and column indexes. Here's where you are calling Controls.Add:
if (string.IsNullOrEmpty(lbl.Text) != true) { tableLayoutPanel1.Controls.Add(tbx); }
I don't see any row or column indexes there.
 

chalupabatman

Member
Joined
Nov 6, 2014
Messages
8
Programming Experience
Beginner
if I follow, what you are instructing me to do, I am still failing :( --- If I use this code it puts my two text boxes on different lines?
C#:
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
Label lbbl = new Label();
lbbl.Name = "Test";
lbbl.Text = "Test";
tableLayoutPanel1.Controls.Add(lbbl, 0, 1);
tb1.Name = "Firefly";
tb1.Text = "Grr";
tableLayoutPanel1.Controls.Add(tb1, 1, 1);
tb2.Name = "Firefly2";
tb2.Text = "Grr2";
tableLayoutPanel1.Controls.Add(tb2, 1, 1);
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
if I follow, what you are instructing me to do, I am still failing :( --- If I use this code it puts my two text boxes on different lines?
C#:
TextBox tb1 = new TextBox();
TextBox tb2 = new TextBox();
Label lbbl = new Label();
lbbl.Name = "Test";
lbbl.Text = "Test";
tableLayoutPanel1.Controls.Add(lbbl, 0, 1);
tb1.Name = "Firefly";
tb1.Text = "Grr";
tableLayoutPanel1.Controls.Add(tb1, 1, 1);
tb2.Name = "Firefly2";
tb2.Text = "Grr2";
tableLayoutPanel1.Controls.Add(tb2, 1, 1);
You can only put one control in each cell of the TLP. That means that two TextBoxes in the same row must be in different columns. Different columns have different indexes. Look at your code for adding the TextBoxes to the TLP. Have you specified different column indexes?
 

chalupabatman

Member
Joined
Nov 6, 2014
Messages
8
Programming Experience
Beginner
You can only put one control in each cell of the TLP
I didn't realize their was a 1 to 1 ratio of control to column. I am sure this could be done, but could I dynamically add columns to the TLP to suit the need for when I need multiple text boxes on one line?

I think it would be something along the lines of this
C#:
tableLayoutPanel1.[COLOR=#2B91AF]RowStyles[/COLOR].[COLOR=#2B91AF]Add[/COLOR]([COLOR=#00008B]new[/COLOR] [COLOR=#2B91AF]System[/COLOR].[COLOR=#2B91AF]Windows[/COLOR].[COLOR=#2B91AF]Forms[/COLOR].[COLOR=#2B91AF]RowStyle[/COLOR]())


EDIT --
So if I am going to possibly be resizing the TLP dynamically, would I be better off 1) creating the TLP straight through code and not use the designer? OR 2) should I just add multiple Columns and if they are not actually needed they will be ignored?
 
Last edited:

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
should I just add multiple Columns and if they are not actually needed they will be ignored?

Unless you have a good reason not, that is what you should do. If you want to, you can call SetColumnSpan to make the TextBoxes in rows that only have one span into the empty column.
 

chalupabatman

Member
Joined
Nov 6, 2014
Messages
8
Programming Experience
Beginner
I have the label & text box side-by-side as I need, but for some reason it is putting a HUGE gap between the first label & text box & the second. If I manually create each label and create each text box individually and add them, it displays fine, but when I try to iterate through my array, this is where the problem ensues. Did I set this up incorrectly?
C#:
int columnIndex = 0;
int rowIndex = 0;
List<string> thingstoadd = new List<string>();

Hashtable lookup = new Hashtable();
OleDbConnection dbcon = new OleDBConnection("Provider = Microsoft.Jet.OLEDB.4.0;DataSource =" + DS);
dbcon.Open()
OleDbCommand dbcom = new OleDbCommand("Select data from tbl_information order by ID ASC", dbcon);
reader = dbcom.ExecuteReader();
while (reader.Read())
{
  thingstoadd.Add(reader[0].ToString());
}
reader.Close();
foreach (string addme in thingstoadd)
{
  Label label1 = new Label();
  TextBox textbox1 = new TextBox();
  label1.Name = "label" + index;
  label1.Text = label; 
  tableLayoutPanel1.Controls.Add(label1, 0, rowIndex);
  textbox1.Name = "text" + index;
  tableLayoutPanel1.Controls.Add(textbox1, columnIndex, rowIndex);
  rowIndex++;
}
 
Top Bottom