Okay I think I have achieved what you were going for Sport, I know WPF better than WinForms, but here is my solution that I just "self-taught" myself.
I'm going to assume there won't be any problems with doing this in your solution.
First create a User Control. I did a Right Click on my WinForm project > Add > User Control. This is where you're going to be adding the content that make up the rows. So it should look like this, and I named my user control RowContent:
Make sure you have your name your controls. So for the check box, I named it stprIndex_chkBox, enable_chkBox and so on.
Now you need to implement the functionality you want for each of the controls using. So, you're going to want to change the value of your stprIndex_chkBox.Text
, and the enable_chkBox.Checked
for starters. You're also going to want to access the Value
s of you numericalUpDowns. So in RowContent.cs I added getters and setters depending on the data I needed from the form. So here is a snippet of the accessors (remember you will want to add more):
public partial class RowContent : UserControl
{
public RowContent()
{
InitializeComponent();
}
public string SetChkBox1Text
{
set { stprIndex_chkBox.Text = value; }
}
public bool IsEnabledChecked
{
get { return enable_chkBox.Checked; }
}
}
Now you see, these will allow you access of the variables outside the RowContent class. Let's move on to the TablePanelLayout control.
I created an additional User Control the same way I created RowContent, but this time I named it ContentCollection. I set AutoSize
of the User Control to true and dropped TableLayoutPanel ( named tableLayoutPanel1 ) onto it.
For the sake of saving time, I added all the controls into the rows dynamically like this:
public partial class ContentCollection : UserControl
{
public ContentCollection()
{
InitializeComponent();
RowContent one = new RowContent();
RowContent two = new RowContent();
RowContent three = new RowContent();
RowContent four = new RowContent();
RowContent five = new RowContent();
RowContent six = new RowContent();
tableLayoutPanel1.Controls.Add(one);
tableLayoutPanel1.Controls.Add(two);
tableLayoutPanel1.Controls.Add(three);
tableLayoutPanel1.Controls.Add(four);
tableLayoutPanel1.Controls.Add(five);
tableLayoutPanel1.Controls.Add(six);
tableLayoutPanel1.SetRow(one, 0);
tableLayoutPanel1.SetRow(two, 1);
tableLayoutPanel1.SetRow(three, 2);
tableLayoutPanel1.SetRow(four, 3);
tableLayoutPanel1.SetRow(five, 4);
tableLayoutPanel1.SetRow(six, 5);
}
}
This gives me:
Now here you can see we are adding these things dynamically. I hope you can picture how to "customize" this User Control when you're using it in your WinForm. In this same file you're going to want to add some more Getters/Setters/functions depending on what you want to do just like the other User Control; so, AddAdditionalRow(RowContext rc)
and so on. I actually cheated and changed the tableLayoutPanel
to be public
to make this quicker in the end.
So finally, you have your ContentCollection that will hold your custom object, now you need to add it to your Form.
Go to your Form, open up your Toolbox and scroll to the top to see your Form there! Drag and drop it on your main form and vio'la. Now, to iterate through the RowContent, it is fairly easily. Since I drag and dropped my User Control onto the Form, I was able to name it (userControl12) and start accessing the controls right away. Check out how I add check marks to every other checkbox and dynamically change the Stepper Index:
public partial class Form1 : Form
{
static int i = 0;
public Form1()
{
InitializeComponent();
foreach (RowContent ctrl in userControl11.tableLayoutPanel1.Controls)
{
ctrl.SetChkBox1Text = i.ToString();
if (!ctrl.IsEnabledChecked && i % 2 == 0)
ctrl.IsEnabledChecked = true;
i++;
}
foreach (RowContent ctrl in userControl12.tableLayoutPanel1.Controls)
{
ctrl.SetChkBox1Text = i.ToString();
i++;
}
}
}
I'm not claiming this to be the best design out there... because it isnt, but it illustrates how to do such a thing. Let me know if you have any questions.