0

I'm implementing an application that :

  • loads images from "My Picture" folder to a panel automatically when the form is loaded. (You can find the code here)

  • There's another function in my program which is: Open folder, that enables the user to open a folder to load its images to the same panel too.

My question is: When ever I choose to open a new folder, the images of this folder appears under the images of "My Picture" folder, I know what is the problem but I don't know how to solve it.

The code for loading the images automatically from "My Picture" contains a variable called Position that defines the position of the current PictureBox control and it's initial value is 0.

//2 variables, one for the Y position of the current PictureBox control
            //and one for help count the number of images in the directory
            int position = 0;
            int count = 0;

the code for Open Folder is the same code I used in loading the images from My Pictures, and position initial value is 0 too! thats why the new loaded images appears under the old ones.

private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Getting files.....";

            int position = 0;
            int count = 0;

how can I fix this problem? I thought of saving the position of latest created PictureBox and use it then as initial value in private void openFolderToolStripMenuItem_Click

Thanks!

4

1 回答 1

0

尝试这样做

        int position = 0;
        int count = 0;
        private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Getting files.....";
            //your work
        }

所以当你再次调用这个位置时,位置不会是'0'\

编辑 我读了那个链接

PictureBox pb = new PictureBox();

您创建动态的控件,删除旧图像执行此操作

private void openFolderToolStripMenuItem_Click(object sender, EventArgs e)
        {
        foreach (Control ctrl in this.Controls)
        {
            if (ctrl is PictureBox)
                this.Controls.Remove(ctrl);
        }
        //Your code
于 2012-04-26T11:46:15.550 回答