重新加载主窗体时,自动在列表框中显示项目

时间:2016-03-30 01:23:58

标签: c# winforms

我只是想在重新加载主表单时是否可以自动查看列表框中的项目。

FrmMain frm = new FrmMain();
frm.ShowDialog();

当主窗体显示时,列表框中的项目会自动显示,没有任何点击或keydown事件。这可能吗?

    private void BtnSearch_Click(object sender, EventArgs e)
    {
        if (ACICTest.FindbyACICNo(textBox1.Text).Count() == 0)
        {
            MessageBox.Show("No record found in the database!","ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
        else
        {
            listBox1.DataSource = ACICTest.FindbyACICNo(textBox1.Text).ToList();
            listBox1.DisplayMember = "ACICNo";
            listBox1.ValueMember = "ACICId";
            listBox1.Focus();
        }
        dataGridView1.Columns.Clear();

上面的代码用于搜索按钮。

     private void BtnUpdate_Click(object sender, EventArgs e)
    {
        int breaker = 0;
        if (MessageBox.Show("Are you sure want to update the data?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
        {
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                if (Convert.ToInt16(dataGridView1.Rows[i].Cells["Prior Year"].Value) > 1)
                {
                    MessageBox.Show("Accept 1 or 0 only. [Prior Year = 1] [Non-Prior = 0]", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    breaker += 1;
                    break;
                }
                else
                {
                    ACICComplete.UpdateData(new ACICCombinedDetails
                    {
                        GFCheckID = Convert.ToInt32(dataGridView1.Rows[i].Cells["ID"].Value),
                        PYear = Convert.ToInt16(dataGridView1.Rows[i].Cells["Prior Year"].Value)
                    });
                }
            }
            if (breaker >= 1)
            {
                //Empty Code
            }
            else
            {
                if (MessageBox.Show("Data successfully updated!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information) == System.Windows.Forms.DialogResult.OK)
                {
                    BtnAdd.Enabled = false;
                    BtnSave.Enabled = false;
                    BtnCancel.Enabled = false;
                    BtnUpdate.Enabled = false;
                    listBox1.Enabled = true;
                    BtnTrustAdd.Enabled = false;
                    BtnTrustRemove.Enabled = false;
                    BtnEdit.Enabled = false;
                    dataGridView1.Columns["Prior Year"].DefaultCellStyle.BackColor = Color.White;
                    FrmMain.ActiveForm.Dispose();
                    FrmMain frm = new FrmMain();
                    frm.ShowDialog();
                  //In this portion the Main form shows up but I want listbox also show the items
                }
            }
        }
    }

这是更新按钮

2 个答案:

答案 0 :(得分:1)

更好的解决方案:为填充列表框定义如下方法:

public void PopulateListboxes()
    {
        listBox1.DataSource = ACICTest.FindbyACICNo(textBox1.Text).ToList();
        listBox1.DisplayMember = "ACICNo";
        listBox1.ValueMember = "ACICId";
        listBox1.Focus();
    }

现在点击按钮调用方法;加载它们;

 private void BtnSearch_Click(object sender, EventArgs e)
    {
        //condition if
        else
        {
           PopulateListboxes();
        }
       //rest of code
    }

要在frm.ShowDialog();的页面加载中加载它们,请使用以下代码段:

  FrmMain.ActiveForm.Dispose();
  FrmMain frm = new FrmMain();
  frm.PopulateListboxes(); // this will populate the control
  frm.ShowDialog();

答案 1 :(得分:0)

您应该将列表框中的项目保存在xml文件中(即使关闭窗口也要保存数据),并在重新加载窗口或任何其他事件时重新加载。