TableLayoutPanel only shows last row - C# - c#

I've a TableLayoutPanel that has 10 rows and 2 columns.
My problem is, when I add a label in the first column and a button in the other, everything is fine. But when I add another label and a button on e.g. the next row, the previous rows are empty. It seems that my program only shows to last added row.
Code:
Label lblProjectName = new Label();
lblProjectName.Text = "test";
lblProjectName.Anchor = AnchorStyles.Left;
Button btnProject = new Button();
btnProject.Text = "Fill In";
tlpProject.Controls.Add(lblProjectName, 0, 0);
tlpProject.Controls.Add(btnProject, 1, 0);
tlpProject.Controls.Add(lblProjectName, 0, 1);
tlpProject.Controls.Add(btnProject, 1, 1);
Tnx!

You have to create a new control for each cell in the panel.
Currently, your code is moving the controls to the last column and last row.
Something like this:
for (int i = 0; i < numRows; ++i) {
Button btnProject = new Button();
btnProject.Text = "Fill In";
tlpProject.Controls.Add(btnProject, 1, i);
}
Any event handlers would have to be added here too, where on the handler end, you have to check the "sender" of the object to see which button the user pressed.

It seems like Label and Button can only be children to single control while you are adding them into different cells(thus different controls). If you need all cells filled with Labels and Buttons you will need 10 Labels and 10 Buttons for each column. Look at the code:
for(int i=0;i<10;i++){
var lblProjectName = new Lable();
lblProjectName.Text = "test";
lblProjectName.Anchor = AnchorStyles.Left;
var btnProject = new Button();
btnProject.Text = "Fill In";
tlpProject.Controls.Add(lblProjectName, 0, i);
tlpProject.Controls.Add(btnProject, 1, i);
}

Related

Dynamic button to fit the area size

I am trying to create a system that has X amount of buttons, like a POS system. Number of buttons will be different based on the number of items user input. My question is if I should create it dynamically like the following:
private void createButton(int numOfBtn)
{
for (int i = 0; i < numOfBtn; i++)
{
Button btn = new Button();
btn.Name = "button";
btn.Text = "button";
btn.ForeColor = Color.White;
btn.BackColor = Color.Green;
btn.Font = new Font("Serif", 24, FontStyle.Bold);
btn.Width = 170;
btn.Height = 80;
btn.TextAlign = ContentAlignment.MiddleCenter;
btn.Margin = new Padding(5);
}
}
Should create buttons one by one on the designer, dynamically like the above code or if there is a better way of doing so?
Also, currently I am having the windows set to Maximum size when it launches, so when I am creating the buttons, how can I tell if the number of button has max out the space(flowlayout).
Edit: Is it better to create all the buttons from the designer first and assign value of it after? but this way there will always have a maximum amount of buttons, let says if I create 20 buttons from the designer, at max I can only assign twenty items.. what would the better way of doing such task?

Dynamically created labels and text boxes not overwriting

I am creating some labels and text boxes when a combo box changes. I am using a tableFlowContainer with 2 columns and 5 rows. This works fine the first time the selection is changed but if I make another selection the labels and textboxes are not honoring the assigned location but seem to be inserting at 1 element increments. what should be {0,0} is {0,1} and {0,1} goes to {1,0} and so on.
Is there a way of clearing any labels and text boxes before rewritting the?
Here is my code.
private void cbPI_SelectedIndexChanged(object sender, EventArgs e)
{
//TextBox[] txtBox = new TextBox[5];
// Label[] lbls = new Label[5];
for (int x = 0; x <= 4; x++)
{
txtBoxs[x] = new TextBox();
this.txtBoxs[x].Text = "Text"+x;
this.txtBoxs[x].Name = "txtBoxName"+x;
this.txtBoxs[x].Anchor = System.Windows.Forms.AnchorStyles.Top;
this.Controls.Add(txtBoxs[x]);
lbls[x] = new Label();
this.lbls[x].Text = "label"+x;
this.lbls[x].Name = "lblBoxName1"+x;
this.lbls[x].Anchor = System.Windows.Forms.AnchorStyles.Right;
this.Controls.Add(lbls[x]);
tableLayoutPanel1.Controls.Add((lbls[x]), 0, x);
tableLayoutPanel1.Controls.Add(txtBoxs[(x)], 1, x);
}
}
By breaking at the controls the x variable is correct put the placement in the display is not.

TableLayoutPanel Extra Lines at the Top?

I have a tablelayout panel in which I am adding rows programmatically. When Adding rows to this container the first time the form is opened (using statement with new ValidationForm()) the table displays fine. Upon the second time, I get added rows at the top for some reason? See the picture here:
This does not affect the number of rows that I use, just displays extra lines. And the number of lines increases each time i close and open the form. Here is the code i use to add rows:
private void insertRow(Label label, dynamic control)
{
// Create Panel
var panel = new Panel();
// Set Object Properties
label.TextAlign = ContentAlignment.MiddleCenter;
label.Dock = DockStyle.Fill;
control.Dock = DockStyle.Fill;
panel.Dock = DockStyle.Fill;
//generate delete button
var delete = new Button();
delete.Text = "X";
delete.ForeColor = System.Drawing.Color.Red;
delete.MaximumSize.Height.Equals(40);
delete.Name = rowCount.ToString();
delete.Dock = DockStyle.Right;
delete.Click += new EventHandler(deleteRow);
// Add Controls to the panel
panel.Controls.Add(control);
panel.Controls.Add(delete);
// add controls
//tableLayoutPanel_Validations.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 48F));
tableLayoutPanel_Validations.RowCount = rowCount + 1;
tableLayoutPanel_Validations.Controls.Add(label, 0, rowCount);
tableLayoutPanel_Validations.Controls.Add(panel, 1, rowCount);
tableLayoutPanel_Validations.RowStyles.Add(new RowStyle(SizeType.AutoSize));
rowCount++;
}
rowCount is a private int that is set to 0 at runtime.
My first instinct is that the entire form would "reset" after the using statement. The form declaration is public partial class ValidationForm: Form {}
Using statement opening the form:
using (AVBuilder.ValidationForm validationBuilder = new AVBuilder.ValidationForm())
{
if (validationBuilder.ShowDialog() == DialogResult.OK)
{
// ADD ITEM TO Validations LIST
ListViewItem result = new ListViewItem(validationBuilder.resultObject + " : " + validationBuilder.resultName);
result.SubItems.Add(validationBuilder.resultJson);
result.ToolTipText = result.Text;
listView_Validations.Items.Add(result);
//MessageBox.Show(validationBuilder.resultJson);
}
}
Make sure the rowCount variable is not declared static, as it will not reset to 0 on a new form instance.

Is there a HTML-RadioButton value equivalent in WinForms?

I am trying to create a series of RadioButtons on a WinForm. It works OK, but in the Click-event I want to capture the product ID and do stuff with that.
I am used to HTML-elements, and assigning data to a label and value for the RadioButton. With WinForms I cannot see the equivalent to the value attribute.
Any good advice on how to pass onn the product ID to the RadioButton Change-event?
var products = new Business.Products().GetAll();
if (!products.Any())
GrpCategories.Controls.Clear();
int y = 2;
int x = 2;
foreach (var product in products)
{
var btn = new RadioButton();
btn.Width = 100;
btn.Height = 20;
if (y >= GrpCategories.Height - btn.Height - 10)
{
x += btn.Width + 2;
y = 2;
}
y += btn.Height + 2;
btn.Appearance = Appearance.Button;
btn.Text = product.Name;
btn.Name = "BtnProduct_" + product.ID;
btn.Location = new Point(x, y);
GrpCategories.Controls.Add(btn);
}
Simply use the Tag-property of the RadioButton. This property can store any .NET object.
There is nothing like RadioButtonList control in winform or else it would have solved your problem like nothing.
But,then also You can group radio buttons by adding them inside a container such as a Panel control, a GroupBox control, or a form.
Drag a group panel inside your form and then add your radio buttons, this will group all the radio buttons inside that group.. as is done here.

Dynamically Adding Checkboxes to a Windows Form Only Shows one Checkbox

I'm sorry if this seems n00bish, but I have been searching for this for a few days now. I am attempting to dynamically add checkboxes to a windows form; however, only one checkbox appears on the form. Here is my code:
for (int i = 0; i < 10; i++)
{
box = new CheckBox();
box.Tag = i.ToString();
box.Text = "a";
box.AutoSize = true;
box.Location = new Point(10, i + 10);
Main.Controls.Add(box);
}
As you can see I am adding the checkboxes via a for loop. I have tried messing with the location and enabling autosize in case they were somehow overlapping. The result is a single checkbox with text "a".
Actually you already created a CheckBox but within the same point.
CheckBox box;
for (int i = 0; i < 10; i++)
{
box = new CheckBox();
box.Tag = i.ToString();
box.Text = "a";
box.AutoSize = true;
box.Location = new Point(10, i * 50); //vertical
//box.Location = new Point(i * 50, 10); //horizontal
this.Controls.Add(box);
}
In this case with help of dynamically assign Name property how to achive checkbox.checked property , in some other action like submit button. how can i get all check box is checked and which is created in loop?
If you have an instance from every button you can make with your button or your event to make something like
CheckBox myCheckedBox = (CheckBox)sender;

Categories

Resources