?
Solved

ordering controls position in a panel C#

Posted on 2010-01-11
6
Medium Priority
?
443 Views
Last Modified: 2013-12-17
So I have a panel, I want to fill the panel with controls that based on the number of the column in my table. So, I code something like this

int i=0;
foreach (DataColumn col in ds.Tables["myTable"].Columns)
{                  
      CheckBox newCheckBox = new CheckBox();
      TextBox newTextBox = new TextBox();
      Panels1.Controls.Add(newCheckBox);
      Panels1.Controls.Add(newTextBox);
      newCheckBox.ID = "cb" + i;
      newCheckBox.Text = col.ToString();
      i++;
}

the result will create checkbox & textbox control in panel1, but it disordered (in terms of newline) because the length of each column is different like,

[]column1[//textbox1][]col2
[//textbox2][]column numb 3
[//textbox3]

how can I achieve something like this

[]column1            [//textbox1]
[]col2            [//textbox2]
[]column numb 3 [//textbox3]

can I inserting something like tabbing and newline in the line, what to do to expect that view?
or maybe we can create new panel + cb + tb on each iteration, but wont it take a bit long?
0
Comment
Question by:rajatop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 26290605
Use TableLayoutPanel instead of panel as your container, then you can put your controls as child controls.
See:
http://msdn.microsoft.com/en-us/library/system.windows.forms.tablelayoutpanel.aspx
http://en.csharp-online.net/TableLayoutPanel
0
 

Author Comment

by:rajatop
ID: 26293329
great, thanks for the solution.

Anyway, to change to row & col count in a loop, it is effective if I do

i=1
tableLayout.ColumnCount=2;
foreach (DataColumn col in ds.Tables["myTable"].Columns)
{                  
      CheckBox newCheckBox = new CheckBox();
      TextBox newTextBox = new TextBox();

      tableLayout.RowCount = i;

      Panels1.Controls.Add(newCheckBox, i-1, 0);
      Panels1.Controls.Add(newTextBox, i-1, 1);

      newCheckBox.ID = "cb" + i;
      newTextBox.ID = "tb" + i;
      newCheckBox.Text = col.ToString();
      i++;
}


or should I count for the column first, set the table layout row/column , and then iterate(adding controls)?
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 26298332
Your code looks OK. Profile it after you finish to see how long does it take.
If you know the size in advance, it is more efficient to create all row and columns before the iteration.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:rajatop
ID: 26303819
I forgot to say that Im use it on Aspx page. Since table layout panel is windows controls and not web controls, I have a problem to add my web control (checknox & textbox) to table layout panel. it said cannot convert web control to windows control. Can i still use table layout panel?
0
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 1500 total points
ID: 26308839
0
 

Author Closing Comment

by:rajatop
ID: 31675913
thanks, though i decided not to use table layout panel ;]
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question