Delete all dynamically created PictureBoxes with the same parent in c# - c#

I am writing a windows forms application in C# in which i am dynamically creating TextBoxes and PictureBoxes with a Panel as the parent:
PictureBox pb = new PictureBox();
pb.Parent = MainPanel;
pb.Name = "pb" + "r" + NumberInRow + "c" + NumberInColumn+ "bi" + buildIndex;
pb.Location = new Point(30 * NumberInRow + 192 * (NumberInRow - 1), 50 * NumberInColumn + 273 * (NumberInColumn - 1));
pb.ImageLocation = ThumbLinks[i];
TextBox tb = new TextBox();
tb.Parent = MainPanel;
tb.Name = "tb" + "r" + NumberInRow + "c" + NumberInColumn + "bi" + buildIndex;
tb.Location = new Point(pb.Location.X - 4, pb.Location.Y + pb.Size.Height + 5);
tb.Text = GalleryNames[i];
I am trying to delete them with this:
foreach (PictureBox pb in MainPanel.Controls)
{
MainPanel.Controls.Remove(pb);
}
foreach (TextBox tb in MainPanel.Controls)
{
MainPanel.Controls.Remove(tb);
}
This only seems to work once though.
Visual Studio tells me that it can't convert System.Windows.Forms.TextBox into System.Windows.Forms.PictureBox.
Is there any way to delete the PictureBoxes and TextBoxes differently?
I've read about something like MainPanel.Children.Remove(); but it doesn't seem to exist or i am doing something wrong.

foreach (var control in MainPanel.Controls
.Where(c => c is PictureBox) || c is TextBox)
{
MainPanel.Controls.Remove(control);
}
This will remove each item of types, both PictureBox and TextBox. The issue with this code of course is that you are modifying an collection whilst enumerating it.
One approach to resolving this issue could be to build a collection of controls to remove first
var controls = MainPanel.Controls.Where(c => c is PictureBox || c is TextBox).ToList();
And then to enumerate that collection removing each item from the Panel.
foreach (var toRemove in controls)
MainPanel.Controls.Remove(toRemove);
It would further benefit one to make sure that the item is removed from the UI on the correct thread
delegate void RemoveControlDelegate(Control controlToRemove);
private void RemoveControl(Control control)
{
if (InvokeRequired)
{
BeginInvoke(new RemoveControlDelegate(RemoveControl), control);
}
else
{
MainPanel.Controls.Remove(control);
}
}
foreach (var toRemove in controls)
RemoveControl(toRemove);

Look at MainPanel.Controls.OfType<PictureBox>() and MainPanel.Controls.OfType<TextBox>(). You can use this in conjunction with a .ToList() call to avoid modifying an interator while it's still active:
var PBs = MainPanel.Controls.OfType<PictureBox>().ToList();
var TBs = MainPanel.Controls.OfType<TextBox>().ToList();
foreach (var pb in PBs)
{
MainPanel.Controls.Remove(pb);
}
foreach (TextBox tb in TBs)
{
MainPanel.Controls.Remove(tb);
}

Related

Use i in variable name

So I have this kind of code (I'm assigning values to XAML elements
), which begs for a "for" loop.
Day1d.Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(1));
Day2d.Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(2));
Day3d.Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(3));
Day4d.Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(4));
Day1t.Text = "°" + (myWeatherForecast.forecastlist[1].temp).ToString();
Day2t.Text = "°" + (myWeatherForecast.forecastlist[2].temp).ToString();
Day3t.Text = "°" + (myWeatherForecast.forecastlist[3].temp).ToString();
Day4t.Text = "°" + (myWeatherForecast.forecastlist[4].temp).ToString();
But all my attempts to include "i" in variable name failed miserably. Is there a way to achieve this?
You can create a loop where you iterate over the instances:
int counter = 1; // are you sure it shouldn't start at 0?
foreach (TextBox tb in new TextBox[] { Day1d, Day2d, Day3d, Day4d })
{
tb.Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(counter));
counter++;
}
counter = 1;
foreach (TextBox tb in new TextBox[] { Day1t, Day2t, Day3t, Day4t })
{
tb.Text = "°" + (myWeatherForecast.forecastlist[counter].temp).ToString();
counter++;
}
You can't compose the name of the variable using another variable. The way to do this would be to create a List and then iterate over that List
var textBoxes1 = new List<TextBox> { Day1d, Day2d, Day3d, Day4d }
var textBoxes2 = new List<TextBox> { Day1t, Day2t, Day3t, Day4t }
foreach (var textbox in textBoxes1)
{
var index = textBoxes1.IndexOf(textBox) + 1;
textbox.Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(index));
}
foreach (var textbox in textBoxes2)
{
var index = textBoxes2.IndexOf(textBox) + 1;
textbox.Text = "°" + (myWeatherForecast.forecastlist[index].temp).ToString();
}
NOTE: You can solve this in different ways:
using arrays instead of lists
keeping your own counter, instead of doing IndexOf
using a for loop, instead of a foreach
Which one is better is mostly based on opinion (although my method is not the fastest, but it doesn't matter if you only have 4 items)
You can use FindName assuming you are using WPF.
Try:
for (int i = 1; i < 5; i++)
{
((TextBox)this.FindName("Day" + i + "d")).Text = string.Format("{0:dd/MM}", DateTime.Today.AddDays(i));
((TextBox)this.FindName("Day" + i + "t")).Text = "°" + (myWeatherForecast.forecastlist[i].temp).ToString();
}

add listboxes to dynamically created tabpages

I am trying to programmatically add listboxes to dynamically created tabpages within tabcontrols. I don't understand where is the problem as I it doesn't give me any error when compiling. I used breakpoints on the if statementif (c is TabPage)and it shows correctly the tab pages which are created.
public void createControls()
{
TabControl tabcontrol = new TabControl();
panel1.Controls.Add(tabcontrol);
tabcontrol.Dock = DockStyle.Fill;
int n = 1;
do
{
tabcontrol.Controls.Add(new TabPage() { Name = "Property #" + n + "", Text = "Property #" + n + "" });
n++;
} while (n == pnum);
foreach (Control c in panel1.Controls)
{
if (c is TabPage)
{
ListBox list = new ListBox();
list.Items.AddRange(new object[] {
"Id",
"Name",
"Entity"});
list.Location = new System.Drawing.Point(20, 38);
list.Name = "listBox1";
list.ScrollAlwaysVisible = true;
list.SelectionMode = System.Windows.Forms.SelectionMode.MultiSimple;
list.Size = new System.Drawing.Size(134, 147);
c.Controls.Add(list);
}
}
}
You need to make sure that you access the controls of the immediate container.
foreach (Control c in panel1.Controls)
Should be
foreach (Control c in tabcontrol.Controls)

Delete dynamically created controls

I keep getting stuck on this part of my program.
whenver i call an listbox.selectitemchange event, i want the proper amount of trackbar and labels to be displayed.
now, it does not work properly.
Some of them get removed when the event is called, some of them don't.
foreach (Label label in Controls.OfType<Label>())
{
if (label.Tag != null && label.Tag.ToString() == "dispose")
{
label.Dispose();
}
}
foreach (TrackBar trackBar in Controls.OfType<TrackBar>())
{
if (trackBar.Tag != null && trackBar.Tag.ToString() == "dispose")
{
trackBar.Dispose();
}
}
for (int i = 0; i < calc; i++)
{
//string[] LineWidthSplitted = lines[lineWidth].Split(' ');
//Int32.TryParse(LineWidthSplitted[2], out WidthValue);
Label Label = new Label();
Label.Name = "TrackbarWidth" + LabelName++;
Label.Tag = "dispose";
Label.Text = "Board -" + LabelName + "- Height:";
Label.Location = new Point(10, 450 + (50 * LabelName));
Label.Size = new System.Drawing.Size(100, 25);
this.Controls.Add(Label);
TrackBar trackBar = new TrackBar();
trackBar.Name = "TrackbarWidth" + trackbarName++;
trackBar.Tag = "dispose";
trackBar.Maximum = 85;
trackBar.Minimum = 65;
trackBar.SmallChange = 5;
trackBar.TickFrequency = 5;
trackBar.Value = 65;
trackBar.Location = new Point(150, 450 + (50 * trackbarName));
trackBar.Size = new System.Drawing.Size(100, 25);
this.Controls.Add(trackBar);
lineWidth += 4;
}
while, when i remove the foreach for the trackbar, all labels get properly displayed.
they all get deleted, en recreated for the pricese amount needed to be created, no exceptions.
Any reason why?
thank you.
Don't use "Dispose" on the labels right away. First remove them. Note that you can't modify the Controls collection inside the foreach so you have to do something like this:
List<Label> itemsToRemove = new List<Label>();
foreach (Label label in Controls.OfType<Label>())
{
if (label.Tag != null && label.Tag.ToString() == "dispose")
{
itemsToRemove.Add(label);
}
}
foreach (Label label in itemsToRemove)
{
Controls.Remove(label);
label.Dispose();
}
If you want to remove all different kinds of controls in one swoop:
List<Control> itemsToRemove = new List<Control>();
foreach (Control ctrl in Controls)
{
if (ctrl.Tag != null && ctrl.Tag.ToString() == "dispose")
{
itemsToRemove.Add(ctrl);
}
}
foreach (Control ctrl in itemsToRemove)
{
Controls.Remove(ctrl);
ctrl.Dispose();
}
I can't test this now, but I think you should also remove the controls from the Form Controls collection where you have added them. By the way, in your case I think you could avoid the OfType extension and use the old fashioned for..loop that will allow to execute just one loop....
for(int x = this.Controls.Count - 1; x >= 0; x--))
{
Control ctr = this.Controls[x];
if (ctr Is Label && ctr.Tag != null && ctr.Tag.ToString() == "dispose")
{
this.Controls.Remove(ctr);
ctr.Dispose();
}
if(ctr Is TrackBar && ctr.Tag != null && ctr.Tag.ToString() == "dispose")
{
this.Controls.Remove(ctr);
ctr.Dispose();
}
}
Notice how removing elements from a collection with a for..loop should be done in reverse order, from the end to start of the collection

Create control in runtime

I can't manage to get the values from textboxes that are created at run-time.
I want an user to choose something from a checkedlistbox, and to enter any values he wants in textboxes that are created at every button click.
How can I get the name of those texboxes? They really exist? I am a beginner and I really don't understand how they are created.
This is my code where I create textboxes.
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int x = 466;
int y = 84;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
int i = 0;
TextBox tb = new TextBox();
tb.Location = new Point(x, y);
tb.Name = "txtBox" + i++.ToString();
Controls.Add(tb);
y += 30;
}
just place the i outside the foreach and done.
int i = 0;
foreach (var itemChecked in checkedListBox1.CheckedItems)
{
i++;
string textBoxName = "textBox" + i.ToString();
TextBox tb = new TextBox();
tb.Location = new Point(x, y);
//tb.Name = "txtBox" + i++.ToString(); <--Your Version
tb.Name = textBoxName;
//...
//Other stuff or your codes
}
Rather than searching for exact name what you can do is have a string(fixed) which is searched for the control.
so if you find that string ( in your case which is 'textbox' ), what you can do is search for that fixed string in the name of control. if it exists then it's dynamically generated control.
foreach(Control c in parentControlIdOrName.Controls)
{
if(c.GetType()==typeof(TextBox))
{
if(((TextBox)c).Name.indexOf("textbox")!=-1)
{
// do your coding here...what ever you want....
}
}
}
Haven't tested but,Hope for the best. It might work.

GetEnumerator problem in c#

i got this GetEnumerator problem.. here's my situation
Panel eachPanel = new Panel();
eachPanel.Size = new Size(pnlProcessCon.Width - 27, 24);
eachPanel.Location = new Point(5, startPoint);
eachPanel.BackColor = (defaultColor == alterColor[0]) ? alterColor[1] : alterColor[0];
TextBox txtProcess = new TextBox();
txtProcess.Size = new Size(50, 20);
txtProcess.Location = new Point(2,2);
txtProcess.TextAlign = HorizontalAlignment.Center;
txtProcess.Text = "P" + Convert.ToString(startProcess);
TextBox txtBurstTime = new TextBox();
txtBurstTime.Size = new Size(50, 20);
txtBurstTime.Location = new Point(txtProcess.Right + 70, 2);
txtBurstTime.TextAlign = HorizontalAlignment.Center;
TextBox txtPriority = new TextBox();
txtPriority.Size = new Size(50, 20);
txtPriority.Location = new Point(txtBurstTime.Right + 70, 2);
txtPriority.TextAlign = HorizontalAlignment.Center;
eachPanel.Controls.Add(txtProcess);
eachPanel.Controls.Add(txtBurstTime);
eachPanel.Controls.Add(txtPriority);
pnlProcessCon.Controls.Add(eachPanel);
but when i call each of their text and add to dictionary, i got this error..
Error 1 foreach statement cannot operate on variables of type 'System.Windows.Forms.Panel' because 'System.Windows.Forms.Panel' does not contain a public definition for 'GetEnumerator' C:\Users\vrynxzent#yahoo.com\Documents\Visual Studio 2008\Projects\Scheduler\Scheduler\Form1.cs 68 13 Scheduler
and got my error here..
foreach (var each in pnlProcessCon)
{
String[] temp = new String[3];
foreach (var process in each)
{
temp = process.Text;
}
}
There are a few issues there.
First, you should enumerate the Controls collections. Second, you will have to cast each control to TextBox before you can retrieve the text. Third, you declared temp as an array, so you can't directly assign a string to it. Fourth (as Henk Holterman pointed out), you should use actual types and not var in the foreach loops.
I'm going to take a stab at working code here. Feel free to adjust for your own needs.
TextBox txtProcess = new TextBox();
txtProcess.Name = "Process";
//configure other textboxes, add to panels, etc.
foreach (Panel each in pnlProcessCon.Controls)
{
String[] temp = new String[3];
foreach (Control control in each.Controls)
{
if(control.Name == "Process")
{
temp[0] = ((TextBox)control).Text;
}
}
}
This would be the general idea (note: no var)
foreach (Panel p in pnlProcessCon.Controls)
{
foreach (Control process in p.Controls)
{
}
}
But make sure pnlProcessCon only contains Panels.

Categories

Resources