row0 of tableLayoutPanel keeps getting large - c#

Good day!
We have a tableLayoutPanel, that is being use to add combo boxes and labels in runtime.
When the user clicks the button, what our code does is that, it creates the controls in runtime, and then it adds it to tableLayoutPanel's container.. When we click the 'Add' button once, we should see one cell created just like below
Now, when we add multiple controls, we notice that row 0 accumulates a large space. How do we remove that?
notice that program1 has large space, but program 2,3,4 have evenly space.. Why is that so? Thanks
Here is our code when the user clicks the 'Add' button
int cLeft = 1;
public System.Windows.Forms.ComboBox AddNewComboBox()
{
System.Windows.Forms.Label lab = new System.Windows.Forms.Label();
System.Windows.Forms.ComboBox com = new System.Windows.Forms.ComboBox();
tableLayoutPanel1.Controls.Add(lab,0,cLeft-1);
lab.Text = "Program " + cLeft.ToString() + ":";
lab.Name = "label" + cLeft.ToString();
tableLayoutPanel1.Controls.Add(com,1,cLeft-1);
com.Width = 220;
com.Name = "comboBox " + cLeft.ToString();
cLeft = cLeft + 1;
return com;
}

I encountered a similar problem once.
I think the problem is that the first row you insert is auto sized. If you add a blank row at the beginning that is auto sized and all the controls afterwards with a fixed size it should work.
Alternatively you could create an own UserControl wherein you place your label and combobox and add this with a maxheight to your tablelayout panel.

Related

Adding controls to a Panel inside a for loop only adds the first iteration of control

I'm trying to dynamically create a list of panels containing a label and a textbox and put those panels into a bigger panel.
My problem is that only the first child panel gets added to the parent panel.
I've tried adding the panels directly into controls as well as this addrange method.
Also I'm aware of another question with this problem but it is six years old and the solution did not work for me.
the Panel pnl is being populated by a Panel containing a label and textbox that is already on the form. that panel is then deleted from the controls and pnl remains
EDIT FOR CLARIFICATION:
The code will loop multiple times but when it reaches panel1.Controls.AddRange(controls); the total count of panel1.controls is 1 regardless of the length of array controls
Panel pnl;
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Control[] controls = new Control[8];
for (int i = 0; i < int.Parse(ddlPlayers.SelectedItem.ToString()); i++)
{
pnl.Name = "pnl " + i.ToString();
if (i!= 0)
pnl.Location = new Point(pnl.Location.X, controls[i - 1].Location.Y + Height);
pnl.Show();
controls[i] = pnl;
}
panel1.Controls.AddRange(controls);
}
The problem was that I wasn't declaring a new panel, I was just using the same control. I thought by putting the controls into an array it meant they would be different but I was wrong.
check this line ---> int.Parse(ddlPlayers.SelectedItem.ToString())

Dynamically Create Checkbox With Scrolling

I'm trying to build a form application in Visual Studio 2010 using C#.
The program will be reading a excel file that contains a list of filenames, and will dynamically generate textbox for each filename.
Below is my code, just for clarification. I wanted to make the label a link to the file, that's why I didn't use checkboxes[i].Text = filename
CheckBox[] checkboxes = new CheckBox[fileCount];
Label[] labels = new Label[fileCount];
for (int i = 0; i < fileCount; i++ )
{
//creating a checkbox
checkboxes[i] = new CheckBox();
checkboxes[i].Location = new Point(360, (145 + i * 30));
checkboxes[i].Name = String.Format("checkbox{0}", i.ToString());
this.Controls.Add(checkboxes[i]);
//creating filename label
labels[i] = new Label();
labels[i].Location = new Point(20, (150 + i * 30));
labels[i].Text = existingFiles[i];
labels[i].Width = 330;
this.Controls.Add(labels[i]);
}
Say if fileCount equals to 100, it will make the form really big/long and won't be able to fit properly on most monitors.
Is there a way to make all dynamically generated checkboxes and labels all grouped in a area and just have the user be able to scroll? Something like a panel with scrolling? I don't know if there's anything like that.
I thought about using CheckedListBox, but doing that way I won't be able to make the filename a link. I want the user be able to click on the label and the file will be opened automatically, instead of selecting it.
Any help is appreciated!
Most controls have the AutoScroll property. Set this to true, and the control will automatically add a scrollbar when necessary. You can use the Panel control and add all of your links/checkboxes to that (if you don't want your whole form to scroll).

Column Styles not working on TableLayoutPanel

I have a TableLayoutPanel that has a dynamic amount of columns and rows determined by the user. I want the buttons inside to be square and the same size, but whenever I use a loop to set the column/rows styles, they never turn out to be the size I want them to be.
How can I get the column/row styles to set the appropriate widths and height os the container elements?
Here is the loop method of the code that handles setting the width size of the table (I use a similar method for rows)
void FormatTableWidth(ref TableLayoutPanel container)
{
TableLayoutColumnStyleCollection columnStyles = container.ColumnStyles;
foreach (ColumnStyle style in columnStyles)
{
style.SizeType = SizeType.Absolute;
style.Width = 60;
}
}
You can do it like....
public void AddButtontControls()
{
tblPanel.SuspendLayout();
tblPanel.Controls.Clear();
tblPanel.GrowStyle = TableLayoutPanelGrowStyle.FixedSize;//.AddColumns;
tblPanel.ColumnStyles.Clear();
for (int i = 0; i < tblPanel.ColumnCount; i++)
{
ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / tblPanel.ColumnCount);
tblPanel.ColumnStyles.Add(cs);
//Add Button
Button a = new Button();
a.Text = "Button " + i + 1;
tblPanel.Controls.Add(a);
}
tblPanel.ResumeLayout();
}
Sorry to tell you, but you don't use the right control.
You definitely must use FlowLayoutPanel control, and you can add as many controls you want in it, you can tell which direction will fill the control, if it wrap content or not, and many others.
And the most important - It Will Not Flickering like TableLayoutPanel does :)

Adding buttons programmatically to a TabControl (TabPage)

I read this topic (Adding buttons to a TabControl Tab in C#) but I don't figure out why my code below add one button only to the tabpage.
I've obviously debugged that the foreach works properly.
foreach (string line in File.ReadAllLines(#"C:\quicklauncher.ini"))
{
TabPage page = new TabPage(foldername);
DirectoryInfo d = new DirectoryInfo(line);
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button();
button.Text = file.Name;
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
}
tabControl.TabPages.Add(page); //add our tab page to the tab control
}
Thanks,
Steve
You thought it added only 1 button for you but in fact it did not, it added all the buttons for you but those buttons had the same Location (which is (0,0) by default). That's why you did think there was only 1 button (because you saw only 1 last button on top of others).
You added buttons automatically to your tabpage, so you should have some rule to locate them, I'm not sure what that rule is but I suppose you want to line them up vertically (just an example), I'm going to correct your code to achieve such a thing, at least you will see it work, and in fact all the buttons are added normally:
//you need some variable to save the next Top for each new button:
//let's call it nextTop:
int nextTop = 0;
foreach (FileInfo file in d.GetFiles("*.*"))
{
Button button = new Button { Top = nextTop,
Text = file.Name };
button.Click += new EventHandler(button_Click);
page.Controls.Add(button);
nextTop += button.Height + 5; //it's up to you on the
//Height and vertical spacing
}
//...
You can also try using some layout control like FlowLayoutPanel and TableLayoutPanel to contain all the buttons, they can help arrange your buttons in some way you may want, just try it.

Measuring a string vertically

I have a C# Winform application. In my form, I have a panel and in my panel, I have a label. The label gets created dynamically. I have the following code:
Label label1 = new Label();
label1.MaximumSize = new Size(400, 0);
label1.Location = new Point(posX, posY);
label1.Text = myText;
label1.AutoSize = true;
posY += 15;
Okay, everything is working. The text of the label automatically wraps after 400 pixels. The problem is, I need to create a second label, but how do I know what to set the the Location to? This new label need to be placed just below the first label and the first label might be 1 line long or 5 lines long. any help would be appreciated.
try to place your label within FlowLayoutPanel, set the FlowDirection to Top Down.
I would support the answer which provided by Int3, and another solution is to read the Height of label1 before set the Top of label2.
For example:
label2.Top = label1.Top + label1.Height + 10;
A GridLayout with some rows might be a solution

Categories

Resources