My program is currently set up to just add one row of controls below the original. How can I make it so the user can add as many lines of controls as they would like?
private void surfaceAddButton_Click(object sender, EventArgs e)
{
//Adds new set of controls on button click
for (int i = 1; i < 2; i++)
{
ComboBox surfaceCombo2 = new ComboBox();
TextBox sideWallsText2 = new TextBox();
TextBox backWallsText2 = new TextBox();
TextBox floorText2 = new TextBox();
Label newLabel = new Label();
Label newLabel2 = new Label();
Label newLabel3 = new Label();
Absorption_Coefficients alpha = new Absorption_Coefficients(); //Adds surfaces to combobox
List<string> materialslist = alpha.listLoad();
materialslist.Sort();
surfaceCombo2.Items.AddRange(materialslist.ToArray());
surfaceCombo2.DropDownStyle = ComboBoxStyle.DropDownList;
//Sets locations and sizes of new row of options, then displays them
surfaceCombo2.Location = new Point(1, 150 * i + 30);
sideWallsText2.Location = new Point(393, 153 * i + 30);
backWallsText2.Location = new Point(561, 153 * i + 30);
floorText2.Location = new Point(711, 153 * i + 30);
newLabel.Location = new Point(321, 158 * i + 30);
newLabel2.Location = new Point(439, 158 * i + 30);
newLabel3.Location = new Point(609, 158 * i + 30);
surfaceCombo2.Width = 322;
sideWallsText2.Width = 43;
backWallsText2.Width = 43;
floorText2.Width = 43;
newLabel.Text = "Side Walls, ft²";
newLabel2.Width = 120;
newLabel2.Text = "Back/or Front Walls, ft²";
newLabel3.Text = "Floor/or Ceiling, ft²";
this.Controls.Add(surfaceCombo2);
this.Controls.Add(sideWallsText2);
this.Controls.Add(backWallsText2);
this.Controls.Add(floorText2);
this.Controls.Add(newLabel);
this.Controls.Add(newLabel2);
this.Controls.Add(newLabel3);
this.Size = new Size(769, 209 * i + 30); //Increases form to accomodate new controls
}
}
This is the form with just the one surface added. I want to make it so there is a new row underneath each time the user clicks +Surface.
for (int i = 1; i < 2; i++)
Instesd of 2 in for loop, replace with n, where n will be number gathered from some control
First step is to take your existing code and create a method that creates a row. Ideally as part of this step, you create a custom control that encapsulates much of the logic you already have now.
Second step is to take that new method and require a parameter numberOfRows, use that parameter, and default it to 1. Then make sure it still works.
Final step is to provide a method for the user to input numberOfRows and send that to your method.
Related
I have form that programmatically add a panel in it;
For each task coming in to my program, I add a label and
progress bar to panel which have 30 pixel Y added to previous Y position.
But when panel get scrolls sometimes when want to scroll down,
positions multiplied and not in their exact position.
Remember, I checked and written Y positions to console and saw that Y position is ok, but panel does not show it correctly
I think problem is for panel draw method,
but don't know how to fix it.
Problem is that task 26 should come just after 25 but not in correct position, despite of console I've seen position is correct.
code to add controls:
private static void AddTaskControls(int taskId)
{
Label lbl = new Label();
lbl = new Label();
lbl.Name = "lbl" + taskId.ToString();
lbl.Text = "Task " + taskId.ToString() + ":";
lbl.Width = 57;
lbl.Height = 13;
lbl.Location = new Point(0, 25 + (taskId) * 30);
ProgressBar pr = new ProgressBar();
pr = new ProgressBar();
pr.Name = "pr" + taskId.ToString();
pr.Width = 180;
pr.Height = 23;
pr.Location = new Point(50, 20 + (taskId) * 30);
Label lbl2 = new Label();
lbl2.Name = "lbl" + taskId.ToString() + "List";
lbl2.Text = "Starting " + taskId.ToString() + "";
lbl2.Width = 200;
lbl2.Height = 13;
lbl2.Location = new Point(230, 25 + (taskId) * 30);
//Console.WriteLine(lbl2.Location.Y + "taskid:"+taskId);
if (panel1.InvokeRequired)
{
AddTaskControllsCallback t = new AddTaskControllsCallback(AddTaskControls);
panel1.BeginInvoke(t, new object[] { taskId });
}
else
{
panel1.Controls.Add(lbl);
panel1.Controls.Add(pr);
panel1.Controls.Add(lbl2);
pr.BringToFront();
}
}
This has nothing to do with threading. When adding controls the current scroll position is used to re-calculate the effective Location. Weird? Yes..
So the reason probably is that your Panel scrolls down while you are adding new controls. I don't see where it happens but here is a tiny test to demonstrate the problem:
Looks familiar, right?
I enforce the scrolling in the code but in your case the reason should be similar, as should be the fix..
private void button20_Click(object sender, EventArgs e)
{
// lets add a few buttons..
for (int i = 0; i < 20; i++)
{
Button btn = new Button();
btn.Top = i * 25;
btn.Parent = panel2;
}
// ..now let's enforce a scoll amount of 111 pixels:
panel2.AutoScrollPosition = new Point(panel2.AutoScrollPosition.X,
-panel2.AutoScrollPosition.Y + 111);
// ..and add a few more buttons..
for (int i = 20; i < 40; i++)
{
Button btn = new Button();
btn.Top = i * 25; // not good enough!
// btn.Top = i * 25 + panel2.AutoScrollPosition.Y; // this will fix the problem!
btn.Parent = panel2;
}
}
So your code needs to set the Locations like this:
lbl.Location = new Point(0, 25 + (taskId) * 30 + panel1.AutoScrollPosition.Y);
..
pr.Location = new Point(50, 20 + (taskId) * 30 + panel1.AutoScrollPosition.Y));
..
lbl2.Location = new Point(230, 25 + (taskId) * 30 + panel1.AutoScrollPosition.Y));
(Or you find out how the scrolling happens and prevent it.)
I've found that it is a lot easier to add a second panel inside the scrollable panel, add the controls to that second panel, and increase the size of the second panel. Any problems with scrolling position do not apply then.
So create a form, add a button, add a panel panel1 with AutoScroll = true;, and add another panel panel2 inside the first panel (with location 0, 0).
private void button1_Click(object sender, EventArgs e)
{
// lets add a few buttons..
for (int i = 0; i < 25; i++)
{
Button btn = new Button();
btn.Top = i * 25;
panel2.Controls.Add(btn); // Add to panel2 instead of panel1
}
// Recalculate size of the panel in the scrollable panel
panel2.Height = panel2.Controls.Cast<Control>().Max(x => x.Top + x.Size.Height);
panel2.Width = panel2.Controls.Cast<Control>().Max(x => x.Left + x.Size.Width);
}
I have an application where i will need to add dynamic controls to a panel
based on the value of a number entered in a textbox.
E.g 5 means i generate 5 rows of the controls on the button click event.
The issue is that when a large number (for example 50) is entered,although
50 rows of dynamic controls are added,i am unable to scroll down to each
of the 50 rows.
private void button1_Click(object sender, EventArgs e)
{
int inputNumber = Int32.Parse(textBox1.Text);
int wid=0;
int hgt = 0;
for (int i = 1; i <= inputNumber; i++)
{
//Create a new label and text box
Label labelInput = new Label();
TextBox textBoxNewInput = new TextBox();
ComboBox cb = new ComboBox();
//Initialize label's property
labelInput.Text = "Input " + i;
labelInput.Location = new Point(30, textBox1.Bottom + (i * 30));
labelInput.AutoSize = true;
//Initialize textBoxes Property
textBoxNewInput.Location = new Point(labelInput.Width, labelInput.Top - 3);
cb.Location = new Point(textBoxNewInput.Width + labelInput.Width + 10, textBoxNewInput.Top);
hgt += textBoxNewInput.Top;
//Add the labels and text box to the form
panel1.Controls.Add(labelInput);
panel1.Controls.Add(textBoxNewInput);
panel1.Controls.Add(cb);
}
ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (mender, f) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);
Controls.Add(panel1);
}
How can i be able to scroll from row 1 to row 100 or row 500 as the case may be?
Thanks
Not sure why you're not using a grid? Manipulate the grid instead.
I did further research and i got the answer:
I removed these lines of code
ScrollBar vScrollBar1 = new VScrollBar();
vScrollBar1.Dock = DockStyle.Right;
vScrollBar1.Scroll += (mender, f) => { panel1.VerticalScroll.Value = vScrollBar1.Value; };
panel1.Controls.Add(vScrollBar1);
And i just set the autoscroll property
panel1.AutoScroll=true;
Works fine
The essence of my program is the user can click for an amount of textboxes and radiobuttons they want to add by clicking on a button and using a numericUpDown. After clicking the button the program adds a button under the text boxes, but if I change the number of rows the first button stays and a new one gets created. So I would like to know how can I only have the one button? Would it also be possible for me to apply this with removing the textboxes and radiobuttons.
This is my code for the button handler
int length = (int)this.numericUpDownNumComp.Value;
bool created = false;
CustomButton c = new CustomButton();
for(int i = 0; i < length; i++)
{
//instantiate and configure the text boxes
textboxComputer.Add(new TextBox());
System.Drawing.Point p = new System.Drawing.Point(176, 114 + i * 25);
//to evoke an object in an ArrayList we use the 'as' keyword
(textboxComputer[i] as TextBox).Location = p;
(textboxComputer[i] as TextBox).Size = new System.Drawing.Size(183, 20);
//use 'as' again here to add the control to the controls Collection
this.Controls.Add(textboxComputer[i] as TextBox);
//instantiate and configure the labels
this.labels.Add(new Label());
System.Drawing.Point pLabel = new System.Drawing.Point(100, 114 + i * 25);
(labels[i] as Label).Location = pLabel;
(labels[i] as Label).Size = new System.Drawing.Size(80, 13);
(labels[i] as Label).Text = #"Computer " + (i + 1).ToString() + ":";
this.Controls.Add((labels[i] as Label));
//add some mouse events
(textboxComputer[i] as TextBox).MouseEnter += new System.EventHandler(this.textBox_mouseEnter);
(textboxComputer[i] as TextBox).MouseLeave += new System.EventHandler(this.textBox_mouseLeave);
//add the radio buttons - these are already sized (See RadioButtons.cs) so just need to place at a point
radioButtons.Add(new RadioButtons());
(radioButtons[i] as RadioButtons).Location = new System.Drawing.Point(370, 110 + i * 25);
this.Controls.Add(radioButtons[i] as RadioButtons);
int last = length - 1;
}
if (created == true)
{
this.Controls.Remove(c as Button);
//(c as Button).Location = new System.Drawing.Point(370, 110 + i * 25 + 25);
created = false;
}
created = true;
this.Controls.Add(c as Button);
(c as Button).Location = new System.Drawing.Point(370, 110 + length * 25 + 25);
I want to use multiple labels in my form. I am using the following code:
Label[] lblLeftUp = new Label[12];
for (int i = 0; i < 12; i++)
{
lblLeftUp[i] = new Label();
lblLeftUp[i].Location = new Point((100 + (20 * i)), 100);
lblLeftUp[i].Text = Convert.ToString(i + 1);
this.Controls.Add(lblLeftUp[i]);
}
however, I can see only one label. any idea to fix that?
The labels are blocking eachother. So try this:
lblLeftUp[i].AutoSize = true;
This happen because of lblLeftUp[i].Location = new Point((100 + (20 * i)), 100);.
How you can see, you set the wrong location for the following labels. Infact 20 isn't enough. So my recommendation is to set the location dipending on the size of the labels. So if you want the labels oredered in horizontal. Try this:
Label[] lblLeftUp = new Label[12];
int PointX = 100; //100 is the initial distance from the left border of the control
for (int i = 0; i < 12; i++)
{
lblLeftUp[i] = new Label();
lblLeftUp[i].Location = new Point(PointX, 100);
lblLeftUp[i].Text = Convert.ToString(i + 1);
this.Controls.Add(lblLeftUp[i]);
PointX += lblLeftUp[i].Width;
}
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