简体   繁体   English

Winform中按钮的Click事件反复触发List中的值数

[英]Click event for button in winform is firing repeatedly for number of values in List

I have a windows form (parent) that takes a value from textbox and then opens child form which then uses that value to select an image from a directory. 我有一个Windows窗体(父窗体),该窗体从文本框中获取一个值,然后打开子窗体,然后使用该值从目录中选择一个图像。 When multiple images are found for the particular value, I have the form modified to display a couple of buttons to navigate (Next & Previous) to display the different images. 当为特定的值找到多个图像时,我将表单修改为显示几个按钮以进行导航(下一个和上一个)以显示不同的图像。 Upon first opening the parent form, entering a value and then using form.show() to display the child form – everything works as expected. 第一次打开父窗体时,输入一个值,然后使用form.show()显示子窗体–一切正常。 But if another value is entered into parent form (child form can still be open or exited (hidden)) and the 'Next' button is clicked the code in the click event is running over again for however many number of images are in the List(imagesFound). 但是,如果在父窗体中输入了另一个值(子窗体仍可以打开或退出(隐藏)),并且单击了“下一步”按钮,则单击事件中的代码将再次运行,因为列表中有许多图像(imagesFound)。 Say I have 3 images in the List(imagesFound) and I step through the code in debug mode the btnNext click event fires 3 times in a row. 假设我在List(imagesFound)中有3张图片,并且我在调试模式下逐步执行了代码,则btnNext click事件连续触发3次。 This of course runs GetMultiImages method which causes sequence of displaying the images to be all of. 当然,这将运行GetMultiImages方法,该方法将导致所有显示图像的序列。 And again, this doesn't happen the first time a value is entered into parent form. 再次,这不会在第一次将值输入到父窗体中时发生。 I've made sure the list and other variables are cleared out in GetImage method. 我确保在GetImage方法中清除了列表和其他变量。 I'm stumped…any ideas? 我感到难过……有什么想法吗?

Form1: Form1中:

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();      
        }                             
    }
}

Child Form: 子表格:

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;
    }
}
    //creating event handler for btnNext
    btnNext.Click += new EventHandler(btnNext_Click);

That's a bug. 那是个错误。 You keep adding a Click event handler for the button, each time you call MultiImageForm(). 每次调用MultiImageForm()时,都将继续为按钮添加Click事件处理程序。 So the event handler runs multiple times for a single click. 因此,事件处理程序一次单击即可运行多次。

Only add event handlers in the form constructor so you can be sure it is only done once. 仅在表单构造函数中添加事件处理程序,因此可以确保仅完成一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM