I am trying to populate a run-time created Form.
First I create my text-properties that's supposed to each button
public static List<string> GetDialogs()
{
dialogs = new List<string>();
var tempString = "";
for (int i = 1; i <= FormSize.getHorizontalButtonCount(); i++)
{
for (int j = 1; j <= FormSize.getVerticalButtonCount(); j++)
{
//fx
tempString = $"{i}x{j*multiplier}";
dialogs.Add(tempString);
}
}
return dialogs;
}
and my list ends up like "1x5", "1x10", "1x15", "2x5" etc
Then I create all my Radiobuttons
public static List<RadioButton> CreateRadioButtons()
{
List<RadioButton> radioButtons = new List<RadioButton>();
for (int i = 0; i < FormSize.getRadioButtonCount(); i++)
{
var tempName = $"btn{i}";
radioButtons.Add(new RadioButton());
radioButtons[i].Name = tempName;
}
return radioButtons;
}
Which just creates a lot of buttons with some names like btn1 etc
Then I populate my Radiobutton-list with my dialog-list
private List<RadioButton> PopulateRadiobuttons()
{
dialogs = FormDialogs.GetDialogs();
List<RadioButton> tempRadioButtons = RadioButtonCreator.CreateRadioButtons();
for (int i = 0; i < FormSize.getRadioButtonCount(); i++)
{
tempRadioButtons[i].Text = dialogs[i];
}
return tempRadioButtons;
}
Now my Radiobuttons both contain a name like btn20 and a text like 4x30
Then I first populate a GroupBox with all my RadioButton elements before lastly populating my form with the groupbox.
public Form PopulateForm()
{
_box = new GroupBox();
radioButtons = PopulateRadiobuttons();
for (int i = 0; i < radioButtons.Count; i++)
{
_box.Controls.Add(radioButtons[i]);
}
_form.Controls.Add(_box);
return _form;
}
Besides all this, when I call this method to create a form it's only the first radiobutton that appears, the 1x5 one.
How could I go on about including all my buttons in my Form?
Here's what I do so far
private void Bt1_Click(object sender, Microsoft.Office.Tools.Ribbon.RibbonControlEventArgs e)
{
FormCreator fm = new FormCreator();
f1 = fm.PopulateForm();
f1.Show();
}
Related
I'm working on a WinForms application in which I have one static TabControl with a tab on which I need to add multiple levels of additional tabs. The number of these tabs will change depending on the data being loaded to the form.
I can add the first line of dynamic tabs tp the static tab like this for example:
TabControl tabControlWafers = new TabControl();
tabControlWafers.Dock = DockStyle.Fill;
int numwafers = wafers.Count();
for (int m = 0; m < numwafers; m++)
{
TabPage tabPage = new TabPage()
{
Name = wafers[m]
};
tabPage.Text = wafers[m].ToString();
tabControlWafers.TabPages.Add(tabPage);
}
tabPage1.Controls.Add(tabControlWafers);
My problem is that now I need to add another level of dynamically created tabs to each of the pages created above. After creating the next tabs like before:
TabControl tabControlStructure = new TabControl();
tabControlStructure.Dock = DockStyle.Fill;
int numstruct = structures.Count();
for (int n = 0; n < numstruct; n++)
{
TabPage tabPagestruct = new TabPage()
{
Name = structures[n]
};
tabPagestruct.Text = structures[n].ToString();
tabControlStructure.Controls.Add(tabPagestruct);
}
How do I get the tabs created here onto each of the first three tabs?
You should be able to accomplish what you need by iterating over the TabPageCollection in tabControlWafers.TabPages, then creating and adding one of your tabControlStructure objects at each iteration. See below for an example of how this could be done. Note the example assumes tabControlWafers has already been created.
foreach (TabPage tp in tabControlWafers.TabPages)
{
TabControl tabControlStructure = new TabControl()
{
Dock = DockStyle.Fill
};
int numstruct = structures.Count();
for (int i = 0; i < numstruct; i++)
{
TabPage tabPagestruct = new TabPage()
{
Name = structures[i],
Text = structures[i]
};
tabControlStructure.TabPages.Add(tabPagestruct);
}
tp.Controls.Add(tabControlStructure);
}
Edit:
Below is a generic example of the method by which I would generate the net nested TabPage structure. Note that if this were real, production code I would pull the addition of subpages off into its own method (something like addSubPages(TapPage parent, String[] names). This is nothing but a simple, paste and run example to give a better picture of what I am describing.
public Form1()
{
InitializeComponent();
TabControl tc1 = new TabControl()
{
Dock = DockStyle.Fill
};
for (int i = 0; i < 5; i++)
{
tc1.TabPages.Add(i.ToString());
}
foreach (TabPage tp in tc1.TabPages)
{
TabControl tc2 = new TabControl
{
Dock = DockStyle.Fill
};
for (int i = 0; i < 5; i++)
{
tc2.TabPages.Add(tp.Text + "." + i.ToString());
}
tp.Controls.Add(tc2);
foreach (TabPage tp2 in tc2.TabPages)
{
TabControl tc3 = new TabControl
{
Dock = DockStyle.Fill
};
for (int i = 0; i < 5; i++)
{
tc3.TabPages.Add(tp2.Text + "." + i.ToString());
}
tp2.Controls.Add(tc3);
}
}
this.Controls.Add(tc1);
}
The above example represents the constructor of an otherwise blank form that looks like the following:
Im trying to edit a listview row's items names.
This is the function i use to add the items to list view
public static void AddRow(this ListView lvw, int image_index,
string item_title, params string[] subitem_titles)
{
// Make the item.
ListViewItem new_item = lvw.Items.Add(item_title, 1);
// Set the item's image index.
new_item.ImageIndex = image_index;
// Make the sub-items.
for (int i = subitem_titles.GetLowerBound(0);
i <= subitem_titles.GetUpperBound(0);
i++)
{
new_item.SubItems.Add(subitem_titles[i]);
}
}
This is what i have tried so far, Trying to edit the text or even add new items
public static void EditRow(this ListView lvw, int image_index,
string item_title, params string[] subitem_titles)
{
//ListViewItem lvi = lvw.Items[image_index];
for (int i = 0; i < lvw.Items[image_index].SubItems.Count; i++)
{
lvw.Items[image_index].SubItems[i].Text = subitem_titles[i];
}
}
.
Update:
Think i have worked it out
public static void EditRow(this ListView lvw, int image_index,
string item_title, params string[] subitem_titles)
{
if (lvw.InvokeRequired)
lvw.Invoke(new MethodInvoker(delegate
{
for (int i = subitem_titles.GetLowerBound(0); i <= subitem_titles.GetUpperBound(0); i++)
lvw.Items[image_index].SubItems[i + 1].Text = subitem_titles[i];
}));
else
{
for (int i = subitem_titles.GetLowerBound(0);i <= subitem_titles.GetUpperBound(0);i++)
lvw.Items[image_index].SubItems[i + 1].Text = subitem_titles[i];
}
}
I am working on a Tic Tac Toe simulator for a class and have run into an issue.
I created a 2-dimensional array to simulate the board and populate it with either 0 or 1 in all the boxes.
The issue I am having is getting those numbers to apply to the labels I have created (a1, a2, a3, b1, b2, etcetera).
Is there a way that my nested for loops can have each element in the array apply to a new label? I can't seem to find anything in my book or online about this.
Here is my related code:
private void newBTN_Click(object sender, EventArgs e)
{
Random rand = new Random();
const int ROWS = 3;
const int COLS = 3;
int [,] board = new int[ROWS, COLS];
for (int row = 0; row < ROWS; row++)
{
for (int col = 0; col < COLS; col++)
{
board[row, col] = rand.Next(1);
}
}
}
What are the names of the labels? I assumed below that the labels are Label0_0, Label0_1, Label1_1 and so on... This way you can find them using the row and column values.
You want to find the Label control on your form dynamically, because you don't know the name in advance while coding.
If you know the name in advance you just say: label1.Text = "1";.
But in your case, you are trying to find a particular control in each iteration of the loop. So you need to have a name for the labels so you can find them using Form.Controls.Find(string, bool) like this:
var row = 4;
var col = 6;
var l = this.Controls.Find("Label" + row.ToString() + "_" + col.ToString(), false).FirstOrDefault() as Label;
if (l == null)
{
//problem... create label?
l = new Label() { Text = "X or O" }; //the position of this need to be set (the default is 0,0)
this.Controls.Add(l);
}
else
{
l.Text = "X or O";
}
Your board stores integers, which is an internal representation of your game state. You can create a UniformGrid that holds Label for your game GUI. The code below returns a grid based on your current board. You need to add this returned grid to your MainWindow (or whatever you use) to see it.
private UniformGrid fillLabels(int[,] board)
{
int numRow = board.GetLength(0);
int numCol = board.GetLength(1);
UniformGrid g = new UniformGrid() { Rows = numRow, Columns = numCol };
for (int i = 0; i < numRow; i++)
{
for (int j = 0; j < numCol; j++)
{
Label l = new Label();
l.Content = (board[i, j] == 0) ? "O" : "X";
Grid.SetRow(l, i);
Grid.SetColumn(l, j);
g.Children.Add(l);
}
}
return g;
}
First, do not re-create (and re-initialize) Random each time you need it: it makes generated sequences skewed badly:
private static Random s_Rand = new Random();
Try not implement algorithm in the button enent directly, it's a bad practice:
private void CreateField() { ... }
private void newBTN_Click(object sender, EventArgs e) {
CreateField();
}
putting all together:
private static Random s_Rand = new Random();
private void ApplyLabelText(String name, String text, Control parent = null) {
if (null == parent)
parent = this;
Label lb = parent as Label;
if ((lb != null) && (String.Equals(name, lb.Name))) {
lb.Text = text;
return;
}
foreach(Control ctrl in parent.Controls)
ApplyLabelText(name, text, ctrl);
}
private void CreateField() {
for (Char row = 'a'; row <= 'c'; ++row)
for (int col = 1; col <= 3; ++col)
ApplyLabelText(row.ToString() + col.ToString(), s_Rand.Next(1) == 0 ? "O" : "X");
}
private void newBTN_Click(object sender, EventArgs e) {
CreateField();
}
How about you skip the INTEGER board and go directly to a Label array?
You can then do the following to loop trough all of them:
Label[,] listOfLabels; // Do also initialize this.
foreach(Label current in listOfLabels)
{
current.Text = _rand.Next(2) == 0 ? "0" : "X";
}
I am try to implement the Nested TableLayoutPanel. I am try to dynamically Create/load the child TableLayoutPanel inside parent TableLayoutPanel.
for this I take the parent TableLayoutPanel and draw it from visual studio toolbox.
one DropDownList for dynamically to create child TableLayoutPanel I assign some values to dropdownlist such as 2*2,2*3,3*3,4*4 when the selected index change is fire is draws the child TableLayoutPanel.
My code is below
private void cmbRowsColumns_SelectedIndexChanged(object sender, EventArgs e)
{
var selectedPair = new KeyValuePair<string, string>();
selectedPair = (KeyValuePair<string, string>)cmbRowsColumns.SelectedItem;
string[] rowcolumn = selectedPair.Value.Split('*');
string strRowCount = rowcolumn[0];
int rowCount = Convert.ToInt32(strRowCount);
string strColumnCount = rowcolumn[1];
int columnCount = Convert.ToInt32(strColumnCount);
DynamicallyGenerateColumn(rowCount, columnCount);
}
private void DynamicallyGenerateColumn(int rowCount, int columnCount)
{
parentTableLayoutPanel.Controls.Clear();
parentTableLayoutPanel.ColumnStyles.Clear();
parentTableLayoutPanel.RowStyles.Clear();
parentTableLayoutPanel.ColumnCount = columnCount;
parentTableLayoutPanel.RowCount = rowCount;
for (int i = 0; i < columnCount; i++)
{
parentTableLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
for (int j = 0; j < rowCount; j++)
{
if (i == 0)
{
parentTableLayoutPanel.RowStyles.Add(new RowStyle(SizeType.AutoSize));
}
TableLayoutPanel objTableLayoutPanel = new TableLayoutPanel();
parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);
}
}
}
but actually problem is when I create child TableLayoutPanel the formatting is not properly
I guess you want to fill each child panels So you need to add objTableLayoutPanel.Dock=DockStyle.Fill;
TableLayoutPanel objTableLayoutPanel = new TableLayoutPanel();
objTableLayoutPanel.Dock = DockStyle.Fill;
parentTableLayoutPanel.Controls.Add(objTableLayoutPanel, i, j);
I want to create dynamically 10 Labels inside a for loop
string labelName;
for(int i = 0; i < 10; i++)
{
labeName = "Label" & i;
// Creata & Instanciate the label here, How ?
}
How would you create a bunch of objects which weren't UI elements? Use a collection:
List<Label> labels = new List<Label>();
for (int i = 0; i < 10; i++)
{
Label label = new Label();
// Set properties here
labels.Add(label);
}
You'll presumably want to add these labels to a form or page or whatever too...
List<string> labelName = new List<string>();
for(int i = 0; i < 10; i++)
{
labeName.Add(string.Concat("Label", i));
}