1

我有一个 Windows 窗体(父窗体),它从文本框中获取一个值,然后打开子窗体,然后使用该值从目录中选择一个图像。当为特定值找到多个图像时,我修改了表单以显示几个按钮来导航(下一个和上一个)以显示不同的图像。首先打开父表单,输入一个值,然后使用 form.show() 显示子表单——一切都按预期工作。但是,如果在父表单中输入了另一个值(子表单仍然可以打开或退出(隐藏))并且单击“下一步”按钮,则单击事件中的代码将再次运行,因为列表中有许多图像(图片发现)。假设我在 List(imagesFound) 中有 3 个图像,并且我在调试模式下逐步执行代码,btnNext click 事件连续触发 3 次。这当然会运行 GetMultiImages 方法,该方法会导致显示图像的顺序全部完成。同样,第一次将值输入父表单时不会发生这种情况。我已确保在 GetImage 方法中清除了列表和其他变量。我被难住了……有什么想法吗?

表格1:

public partial class Form1 : Form
{
    private string parcelID;
    Form2 viewer = new Form2();
    public Form1()
    {
        InitializeComponent();
    }
    private void button1_Click(object sender, EventArgs e)
    {
        parcelID = txtParID.Text;
        ShowViewer();
    }
    private void ShowViewer()
    {
        viewer.GetImage(parcelID);
        if (viewer.NoImageFound == true)
        {
            viewer.Show();
            viewer.Focus();     
        }
        else if (viewer.NoImageFound == false)
        {
            viewer.Hide();      
        }                             
    }
}

儿童表格:

public partial class Form2 : Form
{
    public Button btnNext = new Button();
    public Button btnPrev = new Button();
    private List<string> imagesFound = new List<string>();
    private string Path;
    private string parcel;
    private int increment;
    private int maxNum;
    public bool NoImageFound;

    //multi image members
    private string firstMultiItem;
    private string selectMultiImage;
    Image parMultiImage;

    public Form2()
    {
        InitializeComponent();
    }

    public void GetImage(string ParcelID)
    {
        NoImageFound = true;
        parcel = ParcelID;
        increment = 0;
        maxNum = 0;
        firstMultiItem = null;
        selectMultiImage = null;
        parMultiImage = null;
        imagesFound.Clear();
        Path = "........\\Images\\";
        try
        {
            if (!string.IsNullOrEmpty(parcel))
            {
                string parcelTrim = parcel.Substring(0, 6);
                Path = Path + parcelTrim + "\\";
                foreach (string s in Directory.GetFiles(Path, parcel + "_" + "*"))
                {
                    string trimString = s.Replace(Path, "");
                    imagesFound.Add(trimString);
                }
                if ((imagesFound.Count == 0))
                {
                    MessageBox.Show("No images found for ParcelID: " + parcel);
                    picBox.Image = null;
                    this.Text = "";
                    NoImageFound = false;

                }
                else
                {
                    if (imagesFound.Count == 1)
                    {
                        string firstItem = imagesFound[0].ToString();
                        string selectImage = Path + firstItem;
                        Image parImage = Image.FromFile(selectImage);
                        //in order to access the picture box control you have to change it's 
                        //access modifier (Modifier) from private to public. Defaults to private
                        picBox.Image = parImage;
                        picBox.SizeMode = PictureBoxSizeMode.StretchImage;
                        this.Text = parcel;
                        SingleForm();
                    }
                    else if (imagesFound.Count > 1)
                    {
                        firstMultiItem = imagesFound[0].ToString();
                        maxNum = imagesFound.Count;
                        selectMultiImage = Path + firstMultiItem;
                        parMultiImage = Image.FromFile(selectMultiImage);
                        picBox.Image = parMultiImage;
                        picBox.SizeMode = PictureBoxSizeMode.StretchImage;
                        this.Text = parcel;
                        MultiImageForm();

                    }
                }
            }
            else
            {
                MessageBox.Show("No ParcelID");
            }
        }
        catch (DirectoryNotFoundException)
        {
            string text = parcel;
            MessageBox.Show("ParcelID: " + text + " could not be found.  The directory may be missing.", "There's a problem locating the image.",
                MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    private void SingleForm()
    {
        this.Height = 400;
        btnNext.Visible = false;
        btnPrev.Visible = false;
    }

    private void MultiImageForm()
    {
        //set form properties
        this.Text = parcel;
        this.Height = 432;
        //set btnNext properties
        btnNext.Location = new Point(307, 375);
        btnNext.Size = new Size(75, 25);
        btnNext.Font = new Font("Maiandra GD", 10, FontStyle.Bold);
        btnNext.Text = ">>";
        //add btnNext to form
        this.Controls.Add(btnNext);
        btnNext.Visible = true;
        btnNext.Enabled = true;
        //creating event handler for btnNext
        btnNext.Click += new EventHandler(btnNext_Click);
        //set btnPrev properties
        btnPrev.Location = new Point(12, 375);
        btnPrev.Size = new Size(75, 25);
        btnPrev.Font = new Font("Maiandra GD", 10, FontStyle.Bold);
        btnPrev.Text = "<<";
        //add btnPrev to form
        this.Controls.Add(btnPrev);
        btnPrev.Visible = true;
        btnPrev.Enabled = false;
        //creating event handler for btnPrev
        btnPrev.Click += new EventHandler(btnPrev_Click);

    }

    private void GetMultiImages()
    {
        try
        {
            firstMultiItem = imagesFound[increment].ToString(); 
            selectMultiImage = Path + firstMultiItem;
            parMultiImage = Image.FromFile(selectMultiImage);
            picBox.Image = parMultiImage;
            picBox.SizeMode = PictureBoxSizeMode.StretchImage;
        }
        catch (IndexOutOfRangeException)
        {
            MessageBox.Show("Index was out of range.");
        }
    }

    private void btnNext_Click(object sender, System.EventArgs e)
    {
        if (increment != maxNum - 1)
        {
            increment++;
            GetMultiImages();
        }
        EnableButtons();
    }

    private void btnPrev_Click(object sender, System.EventArgs e)
    {
        if (increment > 0)
        {
            increment--;
            GetMultiImages();
        }
        EnableButtons();
    }

    private void EnableButtons()
    {
        if (increment == 0)
        {
            btnPrev.Enabled = false;
            btnNext.Enabled = true;
        }
        else if (increment > 0 & increment != maxNum - 1)
        {
            btnPrev.Enabled = true;
            btnNext.Enabled = true;
        }
        else if (increment == maxNum - 1)
        {
            btnPrev.Enabled = true;
            btnNext.Enabled = false;
        }
    }

    private void Form2_FormClosing(object sender, FormClosingEventArgs e)
    {
        //overriding closing event
        this.Hide();
        e.Cancel = true;
    }
}
4

1 回答 1

2
    //creating event handler for btnNext
    btnNext.Click += new EventHandler(btnNext_Click);

那是一个错误。每次调用 MultiImageForm() 时,都会为按钮添加 Click 事件处理程序。因此,事件处理程序会为一次单击运行多次。

仅在表单构造函数中添加事件处理程序,这样您就可以确保它只完成一次。

于 2013-09-19T18:16:43.153 回答