[英]Remove controls of flowlayoutpanel and recreating it in C#
I had been experimenting on writing a code to generate images inside a FlowLayoutPanel. 我一直在尝试编写代码以在FlowLayoutPanel中生成图像。
This is what i had done so far, when i click on a button for the first time (by using a checkboxes - read in number of images to open), it will generate the images, when i click on the button on second try, it will not update the flowlayoutpanel. 这是我到目前为止所做的,当我第一次单击按钮时(通过使用复选框-读取要打开的图像数),当我再次尝试单击按钮时,它将生成图像。它不会更新flowlayoutpanel。
Even though i tried to remove the controls inside the FlowLayoutPanel, it still doesn't show the second try of the images. 即使我试图删除FlowLayoutPanel中的控件,它仍然没有显示图像的第二次尝试。
This is the code snippet of the method: 这是该方法的代码片段:
FlowLayoutPanel fwPanel = null;
private void btnOpenFile_Click(object sender, EventArgs e)
{
//if there is content inside the flowpanel, dump it
if (fwPanel != null)
{
listOfFile.Clear();
}
//create a new FLP
fwPanel = new FlowLayoutPanel();
int panelWidth = width * 4 + 50;
int panelHeight = height * 4 + 50;
fwPanel.Size = new Size(panelWidth, panelHeight);
fwPanel.Location = new Point(0, 0);
this.Controls.Add(fwPanel);
//each checked item would be stored into an arraylist
foreach(object itemChecked in clbFile.CheckedItems)
{
listOfFile.Add((clbFile.Items.IndexOf(itemChecked)+1).ToString());
}
int noOfCheckedFile = listOfFile.Count;
PictureBox[] listOfPicture = new PictureBox[noOfCheckedFile];
int positionX = 0, positionY = 0;
int maxPaddingX = (width * MATRIX_SIZE) - 1;
int maxPaddingY = (height * MATRIX_SIZE) - 1;
//dynamically create images.
for (int i = 0; i < noOfCheckedFile; i++)
{
listOfPicture[i] = new PictureBox();
listOfPicture[i].Image = resizeImage((Image)show_picture(Convert.ToInt32(listOfFile[i])), new Size(width, height));
listOfPicture[i].Size = new Size(width, height);
if (positionX > maxPaddingX)
{
positionX = 0;
positionY += height;
}
if (positionY > maxPaddingY)
{
positionY = 0;
}
listOfPicture[i].Location = new Point(positionX,positionY);
listOfPicture[i].Visible = true;
fwPanel.Controls.Add(listOfPicture[i]);
positionX += width;
}
}
show_picture
is a method that takes in and integer and returns a bitmap image. show_picture
是一种接受整数并返回位图图像的方法。
listOfFile
is to trace which file to return. listOfFile
用于跟踪要返回的文件。
listOfPicture
is to store each images. listOfPicture
用于存储每个图像。
i tried replacing this lines 我试图替换这行
//if there is content inside the flowpanel, dump it
if (fwPanel != null)
{
listOfFile.Clear();
}
i have added this line into it, when i do a second click, everything just gone missing, but this is not what i want, because it does not re-populating the FlowLayoutPanel. 我已经在这行中添加了内容,当我第二次单击时,所有内容都丢失了,但这不是我想要的,因为它不会重新填充FlowLayoutPanel。
if (fwPanel != null)
{
fwPanel.SuspendLayout();
if (fwPanel.Controls.Count > 0)
{
for (int i = (fwPanel.Controls.Count - 1); i >= 0; i--)
{
Control c = fwPanel.Controls[i];
c.Dispose();
}
GC.Collect();
}
fwPanel.ResumeLayout();
listOfFile.Clear();
}
I also tried this, but on second click, nothing will happen. 我也尝试过此操作,但是第二次单击将不会发生任何事情。
if (fwPanel != null)
{
List<Control> listControls = fwPanel.Controls.Cast<Control>().ToList();
foreach (Control control in listControls)
{
fwPanel.Controls.Remove(control);
control.Dispose();
}
listOfFile.Clear();
}
I wonder if i miss out anything, can someone enlighten me on what did i miss out ? 我想知道我是否错过任何事情,有人能启发我错过什么吗? Or guide me for the best practice of doing this.
或指导我获得最佳实践。
as Suggested, i shifted the creation outside (credit to Sinatr for spotting it) 正如建议的那样,我将创作移到了外面(发现它的原因是Sinatr)
FlowLayoutPanel fwPanel = new FlowLayoutPanel();
private void createFLP()
{
int panelWidth = width * 4 + 50;
int panelHeight = height * 4 + 50;
fwPanel.Size = new Size(panelWidth, panelHeight);
fwPanel.Location = new Point(0, 0);
this.Controls.Add(fwPanel);
}
that solves the nothing happen part. 解决了什么都没有发生的部分。 Followed by using this to remove controls
随后使用此删除控件
if (fwPanel != null)
{
List<Control> listControls = fwPanel.Controls.Cast<Control>().ToList();
foreach (Control control in listControls)
{
fwPanel.Controls.Remove(control);
control.Dispose();
}
listOfFile.Clear();
}
and everything works like a charm, hope that this answer will be able to help others who are facing the same problem too. 一切都像魅力一样,希望这个答案也能帮助其他面临相同问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.