For example, I would like an array of checkboxes:
CheckBox[] faults = new CheckBox[20];
Now how do I place these in my Form and link them to their array name?
Thanks.
How about that:
YourForm.Controls.AddRange(faults);
You have iterate through each checkboxes in faults, but keep in mind they do not overlap so, you can do like this.
Example:
int top = 0; //used for proper positioning of controls
foreach (CheckBox cb in faults)
{
cb.Location =new Point(0 , top); // fixing cb for distinct position
top +=10;
this.Controls.Add(cb);
}
foreach (CheckBox cb in faults) YourForm.Controls.Add(cb);
Assuming you are using MS Visual Studio: create a small test project, add a checkbox to a form named MyForm using the Visual Studio designer and have a look into the generated method InitializeComponent in the file MyForm.designer.cs. This will help you to find out which properties of your checkboxes you will have to initialize. And, of course, you will see where Visual Studio places the call this.Controls.Add(cb).
Try this:
var faults = new CheckBox[20];
Point startPoint = new Point(20, 10);
for (int i = 0; i < faults.Length; i++)
{
Controls.Add(new CheckBox()
{
Location = new Point(startPoint.X, 20 * i + startPoint.Y),
Text = (i + 1).ToString()
});
}
Good luck!
Related
I'm working on an inventory program and have finished the main functionality as a command line console app. I am now working on a version for winforms. I want to enable it to dynamically generate a Groupbox that holds some textboxes. I'd rather not design 50+ lines of multiple textboxes. Keep in mind I'm rather new to programming, having started with C# a year ago. I know next to nothing on Winforms.
I've tried to use dynamic item = new Groupbox();as a similar method allowed generation of objects at runtime. In the command line app, the way it works is that based on information given, a certain amount of objects are passed into the list _AllItems. I was thinking of generating the Groupboxes by using:
private void InitializeGroupBox()
{
foreach (Product product in Product._AllItems)
{
dynamic Item = new GroupBox();
}
}
But I have the feeling I'm nowhere near the correct method. Thanks to anybody who helps.
You will need to learn a bit more, but here is what I usually do to achieve what you asked.
internal class DynamicForm : Form
{
private FlowLayoutPanel mFlowLayoutPanel;
public DynamicForm()
{
mFlowLayoutPanel = new FlowLayoutPanel();
mFlowLayoutPanel.Dock = DockStyle.Fill;
// Add to this Form
this.Controls.Add(mFlowLayoutPanel);
InitializeGroupBox();
}
private void InitializeGroupBox()
{
mFlowLayoutPanel.SuspendLayout(); // Performance
for (int i = 1; i <= 20; i++) {
var groupBox = new GroupBox();
groupBox.Text = "GroupBox #" + i;
groupBox.Size = new Size(200, 50);
var textBox = new TextBox();
textBox.Dock = DockStyle.Fill;
// Add the TextBox to GroupBox
groupBox.Controls.Add(textBox);
// Add to this Form
mFlowLayoutPanel.Controls.Add(groupBox);
}
mFlowLayoutPanel.ResumeLayout(); // after suspend, resume!
}
}
In my program i'm currently working on programmatically adding a variety of form objects in C#. For example i'm trying to create a certain group of panels, which contain the data I wish to show. When loading the form I have a file which contains all of the data I wish to load in, the loading itself works fine but when loading I need to create a variety of form labels, panels and images to display all of the necessary data and as such I need to make these panels and labels all with a seperate name, but programmatically.
for (int i=0; i<fileLength; i++)
{
Panel pnl = New Panel();
pnl.Name = "pnl1"+i;
//Set the other properties here
}
What i'm trying to do if use an iterator to append the current iteration to the end of the name. Am I going the right way about it? Or should I be doing a different method?
You cannot change variable/object name at runtime. If you want to write code against the object than you need to keep a reference to it. In your case you have changed the Name property of Panel but still you have to use it as pnl, not as pnl0 or pnl1.
The other way for doing this would be to use a Dictionary with key as the name as you assign and value as the object itself. This will help you in accessing your controls using its name that you have assigned to it.
Dictionary<string, Panel> panels = new Dictionary<string, Panel>();
for (i = 0; i <= 10; i++) {
Panel pnl = new Panel();
panels.Add("pnl" + i.ToString(), pnl);
}
//write code against panels
panels("pnl0").Width = 100;
For accessing within loop:
foreach (string pnlName in panels.Keys) {
panels(pnlName).Visible = true;
}
for (i = 0; i <= 10; i++) {
panels("pnl" + i).Visible = true;
}
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).
I´m adding controls dynamically onto my form, but I can't see those new controls.
My code is:
frmFormulario myform = new frmFormulario();
for (int i = 0; i < elements.Count; i++)
{
String nm = elements[i].name;
String chk = "chk"+nm;
CheckBox checkboxWS = new CheckBox();
checkboxWS.Name = chk;
checkboxWS.Checked = true;
checkboxWS.Visible = true;
checkboxWS.Width.Equals(40);
myform.Controls.Add(checkboxWS);
myform.Controls.SetChildIndex(checkboxWS, 0);
}
Can anybody spot the problem?
Thanks
My environment is C# Visual Studio 2010
Try adding
myform.Show(); //or myform.ShowDialog;
after the for loop. If you want to completely work on this new window (myform) and discard the former or parent then try creating it in a new thread.
Hope it helps.
You are creating a new form inside of the loop as well as the controls. Once the loop is done it falls out of scope. I'm sure you didn't want to do that, rather add them to an existing form.
Where do you show myform, the instance of frmFormulario?
Currently you are creating a new form and you don't display it.
I want to create buttons or list of items on the basis of number of items in my database or from list of items in my array and for each item create a onclick function for either buttons or any list of items
How about:
int y = 10;
foreach (string name in names)
{
Button button = new Button();
button.Text = name;
button.Position = new Point(10, y);
y += 20;
button.Click += HandleButtonClick;
Controls.Add(button);
}
You might also store the buttons in an array or a list... there's nothing particularly special about GUI controls that stops you from creating them at execution time just like any other object.
If that doesn't help, please give more information about what you need to do that the above doesn't help you with.
I have done it also by looking at Visual Studio code.