0

I'm making a fairly simple inventory program, and I'm stuck as to how I can add data to the original main form.

On the main form, I have a button to add an item and it's details (additem). Once the user clicks the "Add" button, it should put the entered info into one of four DGV's on the main form. This works fine, however, every time a new item is added, it is added in a new instance of the main form, and the previous instance (with the previous item entry), gets thrown out.

Here is the code for the "Add Item" button on the main form:

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

And here is the code for the additem form when the user clicks "Add":

invmain invmainobject = new invmain();

//Work done here...

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

How do I get the information to appear on the original instance and update the form so the new entries appear in the DGV's?

Andrew Savinykh
  • 25,351
  • 17
  • 103
  • 158
John
  • 447
  • 2
  • 8
  • 20

3 Answers3

1

You should have a public property in your additem form like:

public invmain CreatedItem { get; private set; }

...

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

then in your main form access the property like so:

form1.ShowDialog();
AddItem(form1.CreatedItem);
Erik
  • 5,355
  • 25
  • 39
  • I've never used get,set; how exactly do I implement it into my code? This is only my first week of programming. – John Mar 03 '15 at 17:48
  • Properties are very useful. You should read up on them: https://msdn.microsoft.com/en-us/library/x9fsa0sw.aspx and particularly the section on auto-implemented properties: https://msdn.microsoft.com/en-us/library/bb384054.aspx – Erik Mar 03 '15 at 17:58
  • I think I've got it implemented, but when I run it, I get an error on this line of code: additem(form1.CreatedItem); ...it says "additem is a 'type' but is used like a 'variable'". Note that my invmain form code has stayed the same except for that piece of code that I've added from you're answer after: form1.ShowDialog(); – John Mar 03 '15 at 18:15
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();
John
  • 447
  • 2
  • 8
  • 20
  • Add, additem is being used as a function, `additem(form1.CreatedItem)`. When I wrote that below I meant that as a stub for whatever you want to do with the item from `form1`. `form1.CreatedItem` has your object in it, you should replace that line with whatever you want to do with that object. – Erik Mar 03 '15 at 21:00
  • I don't understand how to implement your solution. I've posted all the code I have for the 'additem' page, if that helps any. – John Mar 03 '15 at 21:12
  • For now, I'm just trying to move three pieces of information from the 'additem' form: Category, Item and Quantity. How do I use your code to return those to the appropriate DGV and display the item and quantity? I've posted all the code that's on the 'additem' page. – John Mar 03 '15 at 23:04
  • How do I make the line of code: additem(form1.CreatedItem), actually add to the DataGridView? – John Mar 04 '15 at 16:50
  • That question is answered elsewhere: http://stackoverflow.com/questions/10063770/how-to-add-a-new-row-to-datagridview-programmatically – Erik Mar 04 '15 at 17:10
  • I know lol. But my the "additem" form is what adds a row to a DGV not my main form. What I'm confused about is how to use the { get; private set; } you suggested to pull the required info from the "additem" form (Category, which is just one of the four DGV's, and Item and Quantity, which are to supposed to be displayed in the appropriate DGV). How do I use what you suggested to do that? If 'CreatedItem' is the object that contains all this information, how do I access it so that I can put it into the right DGV? (I've already included code) – John Mar 04 '15 at 17:42
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;
    }
John
  • 447
  • 2
  • 8
  • 20