I'm trying to create multiple ListBoxes with different id's.
I want to do something like this:
int count = 0
for(int i = 0; i < 10; i++){
ListBox count = new ListBox();
count++;
}
The question is: How to create create multiple ListBoxes?
A Listbox is a control that should be added to the Controls collection of its container. I suppose that this is your form and you will call this code inside some kind of event of your form (like Form_Load for example) or better inside the constructor of the form after the call to InitializeComponents()
for (int i = 0; i < 10; i++)
{
// Create the listbox
ListBox lb = new ListBox();
// Give it a unique name
lb.Name = "ListBox" + i.ToString();
// Try to define a position on the form where the listbox will be displayed
lb.Location = new Point(i * 50,0);
// Try to define a size for the listbox
lb.Size = new Size(50, 100);
// Add it to the Form controls collection
// this is the reference to your form where code is executing
this.Controls.Add(lb);
}
// Arrange a size of your form to be sure your listboxes are visible
this.Size = new Size(600, 200);
You've mixed up the int and ListBox types, and as for ID's, Name would be sensible choices:
So how about something like this:
for (int i = 0; i < 10; i++)
{
ListBox listBox = new ListBox();
listBox.Name = i.ToString();
// do something with this listBox object...
}
Related
I'm trying to make dynamicly created radiobuttons in my dynamicly created panel, but I'm not recieving what I'm trying to accomplish.
Here is my code:
private void Form1_Load(object sender, EventArgs e)
{
//Creating 3 panels
int counTer = 3;
for (int x = 0; x <= counTer; x++)
{
Panel panel = new Panel();
panel.Name = "panel" + x;
panel.Location = new Point(10 * (5 * x), 10);
panel.Size = new Size(150, 275);
//panel.BackColor = Color.Black; <-- Only for checking if they exist
panel.Controls.Add(panel);
//Creating 10 RadioButtons
int hoeveelHeid = 10;
for (int i = 0; i <= hoeveelHeid; i++)
{
RadioButton iets= new RadioButton();
iets.Name = "Waarde" + i;
iets.Text = "Waarde " + i;
iets.Location = new Point(5, 20 * i);
panel.Controls.Add(iets);
}
}
}
I'm not recieving any panels nor radiobuttons, does anyone see the mistake i made?
Thanks.
You are trying to add the panel you created to it's OWN control collection:
panel.Controls.Add(panel);
which means add the panel to the panel.
To add the panel to the form use:
this.Controls.Add (panel);
or even just:
Controls.Add (panel);
As suggested by Sinatr, you have to add the panel to your form like that:
this.Controls.Add (panel);
Otherwise your panel does exist, but it's not on your form.
For anyone who wants to hate with the reason that I only want to gain reputation, this answer's marked as community wiki.
I am looking for optymalize code with displaying 2dimensional array of values on buttons.
I have created grid of buttons like that:
http://screenshot.sh/m2eZscO4i0fXq
and I am actually displaying values of array on this buttons using this code:
button1.Text = board.gameBoard[0, 0].getValue().ToString();
button2.Text = board.gameBoard[0, 1].getValue().ToString();
button3.Text = board.gameBoard[0, 2].getValue().ToString();
button4.Text = board.gameBoard[0, 3].getValue().ToString();
button5.Text = board.gameBoard[1, 0].getValue().ToString();
...
button15.Text = board.gameBoard[3, 2].getValue().ToString();
button16.Text = board.gameBoard[3, 3].getValue().ToString();
Is there easier way to do that? It's working now (http://screenshot.sh/mMDP9pvcC7WOk), but it isn't the best way to do this thing I think. Can somebody show me how do that better?
You can create your buttons dynamically and during creating put the text what you want to Button.Text.
It will be something like:
// array of your buttons (it's not necessary)
var buttons = new Button[4,4];
void SomeMethod()
{
for(var x = 0; x < 4; x++)
{
for(var y = 0; y < 4; y++)
{
var newButton = new Button();
// put your text into the button
newButton.Text = board.gameBoard[x, y].getValue().ToString();
// set the coordinates for your button
newButton.Location = new Point(someCoordinateX, someCoordinateY);
// store just created button to the array
buttons[x, y] = newButton;
// add just created button to the form
this.Controls.Add(newButton).
}
}
}
then, use this method somewhere on initialization step to create and initialize your buttons. The buttons array could be used lately if you will need to modify your buttons somehow.
Hope it will help.
I am trying to add a list of Strings to be used as the text component of labels on a windows form. Below is the code i am using to do this. I have it generating a message box to show me what is being created, but when i add them to the form, only the first string is ever shown on the form, despite a message box popping up for each string indicating the list is populated correctly. Any help would be great.
List<Label> labelList;
public void ShowDialog(List<String> columns)
{
labelList = new List<Label>();
Form updateDialog = new Form();
updateDialog.Width = 500;
updateDialog.Height = 500;
for (int i = 0; i < columns.Count(); i++ )
{
//Label label = new Label() {Text=columns[i].ToString() };
labelList.Add(new Label() {Text=columns[i].ToString()});
}
for (int j = 0; j < labelList.Count(); j++ )
{
updateDialog.Controls.Add(labelList[j]);
MessageBox.Show(labelList[j].Text.ToString());
}
You need to set the location of the created labels. They are positioned on top of each other at location (0, 0).
Controls are being added to the form, but they are not visible to you. Just set the different location for each Label and you'll see them.
You can also precise your code by using 1 loop instead:
int yAxis = 10;
for (int i = 0; i < columns.Count(); i++ )
{
//create label
Label newLbl = new Label() {Text=columns[i].ToString()};
newLbl.Location = new Point(10, yAxis * i); //will create a column of all labels, you can use your oown logic too
//add to list
labelList.Add(newLbl);
//add to form
updateDialog.Controls.Add(newLbl);
//show on msg box
MessageBox.Show(newLbl.Text.ToString());
}
I'm new to Visual Studio 2010 C# and I'm creating an application where the user will select the number of textboxes will be shown in a form. For example, if the user will select "2" automatically there will be 2 boxes will be shown in the form.
This is the screenshots that I want to create.
I guess what you need to know is dynamic creation of controls.
To do what you want here you need to:
Create a control
Add control to form
Set control location, size and anything else you need
It would go something like this:
Texbox texbox = new Textbox();
Controls.Add(textbox);
textbox.Top = 20;
textbox.Left = 200;
textbox.Width = 200;
textbox.Name = "textbox1";
So that there's something left for you to do, you should repeat steps above in a loop, and calculate location of each textbox so that they're not stacked up.
comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int i = 0;
int y = 0;
while (i < int.Parse(comboBox1.SelectedItem.ToString()))
{
System.Windows.Forms.TextBox tt = new System.Windows.Forms.TextBox();
y = y + 30;
tt.Location = new System.Drawing.Point(0, y);
this.Controls.Add(tt);
i++;
}
}
Hope this helps
for (int i = 0; i < 200; i++)
{
Control control = new Control();
control = new CheckBox();
Size size = control.Size;
Point point = new Point(20, 22);
control.Location = point;
int width = size.Width + 5;
i += width;
list.Add(control);
}
foreach(Control c in list)
{
}
how do I create a new instance of checkbox? Because this way I am getting just one checkbox each time. I want to get three checkbox in each row.
Is this winforms? A first point: you don't need the new Control() each time (you simly discard it anyway when you new CheckBox(). How exactly do you want the layout to appear? Can you describe it a bit more please?
I imagine TableLayoutPanel might be a reasonable start...
[STAThread]
static void Main() {
Application.EnableVisualStyles();
Form form = new Form();
TableLayoutPanel layout = new TableLayoutPanel();
layout.Dock = DockStyle.Fill;
form.Controls.Add(layout);
layout.AutoScroll = true;
layout.ColumnCount = 3;
// size the columns (choice just to show options, not to be pretty)
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Absolute, 200));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 50));
layout.ColumnStyles.Add(new ColumnStyle(SizeType.AutoSize));
layout.GrowStyle = TableLayoutPanelGrowStyle.AddRows;
for (int i = 0; i < 200; i++)
{
CheckBox chk = new CheckBox();
chk.Text = "item " + i;
layout.Controls.Add(chk);
}
Application.Run(form);
}
Otherwise, you'll need to manually set the Location (or Top and Left) of each; not simple.
Your code has problems. Let's work from sample code rather than a lesson. I'll create a Panel first, nice if you want to remove the checkboxes you created. You'd probably be interested in the user clicking a checkbox so lets add an event for that. Start a new WF project and drop a button on the form. Double click it, then paste this code:
private void button1_Click(object sender, EventArgs e) {
// Give the 3 checkboxes a decent spacing
int height = this.Font.Height * 3 / 2;
// Create the panel first, add it to the form
Panel pnl = new Panel();
pnl.Size = new Size(100, 3 * height);
pnl.Location = new Point(10, 5);
this.Controls.Add(pnl);
// Make three checkboxes now
for (int ix = 0; ix < 3; ++ix) {
CheckBox box = new CheckBox();
box.Size = new Size(100, height);
// As pointed out, avoid overlapping them
box.Location = new Point(0, ix * height);
box.Text = "Option #" + (ix + 1).ToString();
box.Tag = ix;
// We want to know when the user checked it
box.CheckedChanged += new EventHandler(box_CheckedChanged);
// The panel is the container
pnl.Controls.Add(box);
}
}
void box_CheckedChanged(object sender, EventArgs e) {
// "sender" tells you which checkbox was checked
CheckBox box = sender as CheckBox;
// I used the Tag property to store contextual info, just the index here
int index = (int)box.Tag;
// Do something more interesting here...
if (box.Checked) {
MessageBox.Show(string.Format("You checked option #{0}", index + 1));
}
}
It looks like you get your 200 instances, all placed at the same point.
Instantiate 3 new checkboxes inside your loop body, set their properties accordingly and add each of them to the list. After the code above is complete, you will have 600 checkboxes.
list.Add(Control1);
list.Add(Control2);
list.Add(Control3);
I am not sure about what you are trying to do, but I cleaned up your code a bit:
for (int i = 0; i < 200; i++)
{
Control control = new CheckBox();
control.Location = new Point(20, 22);
i += control.Size.Width + 5;
list.Add(control);
}
You should not add a new instance to the list if you want to add the control you just made.
Also:
Control control = new Control();
control = new CheckBox();
Is a bit redundant. Also to not get one control at the same spot multiple times you should alter the point. Hope this helps