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);
Related
I want to add 2 label at the same group box, this is my code :
int x = 0;
foreach (var item in comboboxinterface.Items)
{
drv = item as DataRowView;
Button btn = new Button();
Label lblerrortoday = new Label();
Label lblcounterror = new Label();
btn.Text = drv.Row.ItemArray[0].ToString();
btn.Location = new System.Drawing.Point(10, 20 + (x * 30));
lblcounterror.Location = new System.Drawing.Point(100, 25 + (x * 30));
lblcounterror.Text = "No";
lblerrortoday.Location = new System.Drawing.Point(120, 25 + (x * 30));
lblerrortoday.Text = "Error Today";
grouptodayerror.Controls.Add(btn);
grouptodayerror.Controls.Add(lblcounterror);
grouptodayerror.Controls.Add(lblerrortoday);
x++;
}
But when i start the program, the lblerrortoday is not showing up but the lblcountererror is fine, when i tried to comment the the lblcounterror, the lblerrortoday is showing fine, did i miss something?
For me your approach seems to ok, only point to note that width of labels that may cause for not displaying other label.
You could use Control.AutoSize property , This may resolve your problem
lblcounterror.AutoSize = true;
lblerrortoday.AutoSize = true;
The TextBox is there, but overlapped by lblcounterror. Reduce Width of lblcounterror and you will see lblerrortoday.
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
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.
I have an application that requires a set of questions that may range from 1 to 1000.
The questions are set up by the user and I need the groupbox to contain the
2 radio buttons indicated below.
The code does create multiple groupboxes containing 2 radio buttons.
This code is in a loop that is determined by how many questions are needed.
The issue is that when a radio button is clicked in any of the groupboxes,
that it removes the click from which ever groupbox was previously clicked.
How do I resolve this?
GroupBox grpAnswerType = new GroupBox(); // new groupbox
if (intZ < 9)
{
grpAnswerType.Name = "grpAnswerType00" + strQNumber;
}
if (intZ >= 10 & intZ <= 99) // intZ is the counter in the loop
{
grpAnswerType.Name = "grpAnswerType0" + strQNumber; // name is used later
}
if (intZ >= 100 & intZ <= 999)
{
grpAnswerType.Name = "grpAnswerType" + strQNumber;
}
grpAnswerType.Location = new Point(290, intR + 20);
grpAnswerType.Size = new Size(150, 45);
grpAnswerType.ForeColor = System.Drawing.Color.Red;
grpAnswerType.BackColor = SystemColors.Control;
grpAnswerType.Font = font;
grpAnswerType.Text = "Choose answer type ";
this.Controls.Add(grpAnswerType);
grpAnswerType.Show();
clsGlobals.gGroupBoxRadioButton3[intZ] = grpAnswerType; // add to array for later storage to database
pnlQ11.Controls.Add(grpAnswerType); // add to the dynamic panel on the form
RadioButton rbtnA1 = new RadioButton(); // Radio Button1
if (intZ < 9)
{
rbtnA1.Name = "rbtnA100" + strQNumber;
}
if (intZ >= 10 & intZ <= 99)
{
rbtnA1.Name = "rbtnA10" + strQNumber;
}
if (intZ >= 100 & intZ <= 999)
{
rbtnA1.Name = "rbtnA1" + strQNumber;
}
rbtnA1.Location = new Point(295, intR + 38);
rbtnA1.Size = new Size(60, 25);
rbtnA1.Text = "One";
rbtnA1.Font = font;
rbtnA1.ForeColor = System.Drawing.Color.Blue;
rbtnA1.BackColor = SystemColors.Control;
grpAnswerType.Controls.Add(rbtnA1);
pnlQ11.Controls.Add(rbtnA1); // if this is not commented, it appears on the panel, if not it does not
rbtnA1.Show();
clsGlobals.gRadioButtonOne[intZ] = rbtnA1;
rbtnA1.BringToFront();
RadioButton rbtnA2 = new RadioButton(); // Radio Button 2
if (intZ < 9)
{
rbtnA2.Name = "rbtnA200" + strQNumber;
}
if (intZ >= 10 & intZ <= 99)
{
rbtnA2.Name = "rbtnA20" + strQNumber;
}
if (intZ >= 100 & intZ <= 999)
{
rbtnA2.Name = "rbtnA2" + strQNumber;
}
rbtnA2.Location = new Point(355, intR + 38);
rbtnA2.Size = new Size(70, 25);
rbtnA2.Text = "All"; ;
rbtnA2.Font = font;
rbtnA2.ForeColor = System.Drawing.Color.Blue;
rbtnA2.BackColor = SystemColors.Control;
grpAnswerType.Controls.Add(rbtnA2);
pnlQ11.Controls.Add(rbtnA2); // if this is not commented, it appears on the panel, if not it does not
rbtnA2.Show();
clsGlobals.gRadioButtonAll[intZ] = rbtnA2;
rbtnA2.BringToFront();
I think it is because pnlQ11 is another UserControl (I am guessing you're using Windows Forms) and, acording to MSDN, a control can only be assigned to one Control.ControlCollection (http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controlcollection.add%28v=vs.100%29.aspx).
So, your RadioButton is removed from the GroupBox and added to pnlQ11, which is the same control where all other RadioButtons belong. Then, the solution is to avoid adding the RadioButton to that other control and keep it only in the GroupBox