回到相同的表单并更新信息

时间:2015-03-03 17:18:23

标签: c# winforms add

我正在制作一个相当简单的库存程序,而且我不知道如何将数据添加到原始主表单中。

在主窗体上,我有一个按钮来添加项目及其详细信息(additem)。用户单击“添加”按钮后,应将输入的信息放入主窗体上的四个DGV之一。这样可以正常工作,但是,每次添加新项目时,它都会添加到主窗体的新实例中,而前一个实例(使用上一个项目条目)将被抛出。

以下是主表单上“添加项目”按钮的代码:

additem form1 = new additem();
this.Hide();
form1.ShowDialog();
this.Visible = true;
this.Refresh();

以下是用户点击“添加”时additem表单的代码:

invmain invmainobject = new invmain();

//Work done here...

this.DialogResult = DialogResult.OK;
invmainobject.Show(); 

如何让信息显示在原始实例上并更新表格,以便新条目出现在DGV中?

3 个答案:

答案 0 :(得分:1)

您的additem表单中应该有一个公共属性,如:

public invmain CreatedItem { get; private set; }

...

this.DialogResult = DialogResult.OK;
this.CreatedItem = invmainobject;

然后在您的主窗体中访问该属性,如下所示:

form1.ShowDialog();
AddItem(form1.CreatedItem);

答案 1 :(得分:0)

(additem FORM):

public invmain CreatedItem { get; private set; 

        //"Add" button click event

        //work done here to direct the info user entered to appropriate DGV
        this.DialogResult = DialogResult.OK;
        this.Dispose();
        this.CreatedItem = invmainobject;

(invmain FORM)

        //"Add Item" button->takes user to additem form

        additem form1 = new additem();
        form1.ShowDialog();
        additem(form1.CreatedItem);
        this.Visible = true;
        this.Refresh();

答案 2 :(得分:0)

private void quantitybox_KeyPress(object sender, KeyPressEventArgs e)
    {
        e.Handled = !(char.IsDigit(e.KeyChar) || e.KeyChar == '\b'); //OR: e.KeyChar==8
    }

    public invmain CreatedItem { get; private set; }

    public void add_Click(object sender, EventArgs e)
    {           


        //SWITCH STATEMENT FOR DIRECTING USER-ENTERED INVENTORY DATA  
        //TO THE APPROPRIATE TABCONTROL TAB AND DGV<1:4>

        //OBJECT REFERENCE TO DGV
        invmain invmainobject = new invmain();

        switch(combobox1.SelectedIndex)
        {
            case 0: //ELECTRICAL
                invmainobject.datagridview1.Rows.Add(itembox.Text, quantitybox.Text);
                break;
            case 1: //MECHANICAL
                invmainobject.datagridview2.Rows.Add(itembox.Text, quantitybox.Text);
                break;
            case 2: //CABLES
                invmainobject.datagridview3.Rows.Add(itembox.Text, quantitybox.Text);
                break;
            case 3: //MISC.
                invmainobject.datagridview4.Rows.Add(itembox.Text, quantitybox.Text);
                break;
            default:
                MessageBox.Show("Please select a category.\t\t", "Required Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                combobox1.Focus();
                return;
        }

        if (string.IsNullOrWhiteSpace(this.itembox.Text))
        {
            MessageBox.Show("The 'Item' field is required.\t\t\t", "Required Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            itembox.Focus();
            return;
        }

        if (string.IsNullOrWhiteSpace(this.quantitybox.Text))
        {
            MessageBox.Show("The 'Quantity' field is required.\t\t\t", "Required Field Missing", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            quantitybox.Focus();
            return;
        }

        this.DialogResult = DialogResult.OK;
        this.Dispose();
        this.CreatedItem = invmainobject;
    }