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.
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 dynamic Labels and TextBox's on one panel.
I can delete the Panel. No Problem but then I also don't know how to delete the Textboxes etc
and i hoped that i can refresh or clear the panel so that all labels and textboxes will deleted..
Label makeLabelC = new Label();
makeLabelC.Width = 100;
makeLabelC.Font = new Font(makeLabelC.Font.Name, 8, FontStyle.Bold | FontStyle.Underline);
makeLabelC.Location = new Point(400, 100);
makeLabelC.Name = e.Node.Text;
makeLabelC.Text = e.Node.Text;
this.Controls.Add(makeLabelC);
this.Controls.Add(panel1);
TextBox textboxC = new TextBox();
textboxC.Width = 100;
textboxC.Location = new Point(500, 100 );
textboxC.Name = e.Node.Text + "lbl";
textboxC.Text = "enter here";
this.Controls.Add(textboxC);
this.Controls.Add(panel1);
for (int z = 0; z < n; z++)
{
Label makeLabel = new Label();
makeLabel.Width = 100;
makeLabel.Location = new Point(400, 150 + 2 * z * makeLabel.Height);
makeLabel.Name = e.Node.Text;
makeLabel.Text = e.Node.Nodes[z].Text;
this.Controls.Add(makeLabel);
this.Controls.Add(panel1);
TextBox textbox = new TextBox();
textbox.Width = 100;
textbox.Location = new Point(500, 150 + 2 * z * textbox.Height);
textbox.Name = e.Node.Text + "lbl";
textbox.Text = "enter here";
this.Controls.Add(textbox);
this.Controls.Add(panel1);
}
}
is there a way with panel how to do this or an other solution?
I thought that the Panel can help me there...
thanks Janik
You are adding the controls to the form instead of the panel - which you also add multiple times
this.Controls.Add(panel1); // do this once
panel1.Controls.Add(textbox); // add the controls to the panel
Once you have done this, when you remove the panel, you will also remove its child controls.
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);
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.