c# - 多面板表单 - ComboBox处理程序在转到下一个面板并返回到上一个面板时停止工作

时间:2016-05-31 20:38:36

标签: c# .net winforms combobox panel

我使用Visual Studios Designer创建了一个多面板表单。该表单用于将项目保存到数据库。用户在第一个表单中添加有关项目的信息,然后选择下一个向项目添加图片。不同的项目有不同数量的图片相关联。

面板的过渡如下:

  • Panel1< - > Panel2a

  • Panel1< - > Panel2b

  • Panel1< - > Panel2c

  • Panel1< - > Panel2d

因此,对于任何给定的项目,有两个面板要填写。第二个面板有4个版本,它们决定了多少个图片框和"浏览"有按钮。

panel1上的ComboBox确定选择了哪个版本的panel2。 A" next"按钮用于移动到panel2和"之前的"按钮切换回panel1。

代码如下:

public partial class formAddItem : Form
{
    // To browse for images
    OpenFileDialog mImageBrowse = new OpenFileDialog();

    // store image paths
    string mImagePath1 = null;
    string mImagePath2 = null;


    // Constructor
    public formAddItem()
    {
        InitializeComponent();
    }

    private void frmAddItem_Load(object sender, EventArgs e)
    {

        // Retrieve list of items from database, store in dataset
        // ...


        // List to use as DataSource
        List<string> listItemType = new List<string>();

        // Add items from dataset to a list and alter string for readability
        foreach (ITEM_TYPERow itemTypeRow in mDataSet.ITEM_TYPE)
        {
            listItemType.Add("ITEM_" + itemTypeRow[0]);
        }

        comboItemType.DataSource = listItemType;

        // set up current state
        panel1.Visible = true;
        panel1.BringToFront();
        panel1.Focus();
        btnNext.Enabled = true;
        btnPrevious.Enabled = false;
    }

    private void btnNext_Click(object sender, EventArgs e)
    {

        // Next panel depends on item type selection
        string itemTypeSelection = comboItemType.Text;

        switch (elementTypeSelection)
        {
            case "ITEM_A":
                panel2A.Visible = true;
                panel2A.BringToFront();
                panel2A.Focus();
                break;
            case "ITEM_B":
                panel2B.Visible = true;
                panel2B.BringToFront();
                panel2B.Focus();
                break;
            case "ITEM_C":
                panel2C.Visible = true;
                panel2C.BringToFront();
                panel2C.Focus();
                break;
            case "ITEM_D":
                panel2D.Visible = true;
                panel2D.BringToFront();
                panel2D.Focus();
                break;
            default:
                return;
        }

        // Set current state
        btnNext.Enabled = false;
        btnPrevious.Enabled = true;
    }

    private void btnPrevious_Click(object sender, EventArgs e)
    {
        btnPrevious.Enabled = false;
        btnNext.Enabled = true;
        panel1.Visible = true;
        panel1.BringToFront();
        panel1.Focus();

    }

    private void btnItemA1Browse_Click(object sender, EventArgs e)
    {
        // Set filter
        mImageBrowse.Filter = "png files (*.png)|*.png";
        mImageBrowse.FilterIndex = 1;

        DialogResult userCickedOk = mImageBrowse.ShowDialog();

        if (userCickedOk == DialogResult.OK)
        {
            mImagePath1 = mImageBrowse.SafeFileName;
            pictureBoxItemA1.Image = new Bitmap(mImageBrowse.FileName);
        }


    }


    private void btnItemA2Browse_Click(object sender, EventArgs e)
    {
        // Set filter
        mImageBrowse.Filter = "png files (*.png)|*.png";
        mImageBrowse.FilterIndex = 1;

        DialogResult userCickedOk = mImageBrowse.ShowDialog();

        if (userCickedOk == DialogResult.OK)
        {
            mImagePath2 = mImageBrowse.SafeFileName;
            pictureBoxItemA2.Image = new Bitmap(mImageBrowse.FileName);
        }


    }

    // Resets image stuff when a new item type is selected
    private void comboItemType_SelectionChangeCommitted(object sender, EventArgs e)
        {
            // Clear images and image paths
            mImagePath1 = null;
            mImagePath2 = null;
            ...

            pictureBoxItemA1.InitialImage = null;
            pictureBoxItemA2.InitialImage = null;
            ...

        }
}

当我按下“下一步”时,comboItemType_SelectionChangeCommitted()似乎有效。然后&#39;之前&#39;如果我不浏览图像。但是一旦我浏览,事情就会停止工作。

还有一些其他事情正在发生,但我希望它们与我的问题无关

1 个答案:

答案 0 :(得分:1)

查看我对以下方法所做的更改。按钮Next应显示下一个面板(不是当前面板)。

        private void btnNext_Click(object sender, EventArgs e)
        {

            // Next panel depends on item type selection
            string itemTypeSelection = comboItemType.Text;

            switch (elementTypeSelection)
            {
                case "ITEM_A":
                    panel2B.Visible = true;
                    panel2B.BringToFront();
                    panel2B.Focus();
                    elementTypeSelection = "ITEM_B";
                    break;
                case "ITEM_B":
                    panel2C.Visible = true;
                    panel2C.BringToFront();
                    panel2C.Focus();
                    elementTypeSelection = "ITEM_C";
                    break;
                case "ITEM_C":
                    panel2D.Visible = true;
                    panel2D.BringToFront();
                    panel2D.Focus();
                    elementTypeSelection = "ITEM_D";
                    btnNext.Enabled = false;
                    break;
                case "ITEM_D":
                    break;
                default:
                    return;
            }

        }