Just to give a little more info; I'm making a simple inventory program. From the main inventory page, a user can click the "Add Item" button, where they will be taken to another form, where they can fill out the item's info. Three fields are required (Category, Item, Quantity). Based on the category they choose, it will go to one of four DGV's. And the only thing displayed will be the item and quantity (in the appropriate DGV).

Could someone give me an example of how I can pass data to the original instance of a main form using { get; private set; }?

If I could get help for my specific program, that would be even better. Here is the code for the 'additem' form:

namespace WindowsFormsApplication1
{
 public partial class additem : Form
{
public additem()
{
    InitializeComponent();
    datetimelabel.Text = DateTime.Now.ToString("MM/dd/yyyy");
    this.quantitybox.KeyPress += new KeyPressEventHandler(quantitybox_KeyPress);

}
private void additem_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
    invmain form1 = new invmain();
    form1.Show();
    this.Close();
}

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();
    //invmainobject = CreatedItem;

    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.CreatedItem = invmainobject;
    this.DialogResult = DialogResult.OK;
    this.Dispose();
}

private void button1_Click_1(object sender, EventArgs e)
{
    //OPTION TO ADD IMAGE
    OpenFileDialog open = new OpenFileDialog();
    if (open.ShowDialog() == DialogResult.OK)
        uploadpic.Image = Bitmap.FromFile(open.FileName);
}

And here is the code for the main form:

namespace WindowsFormsApplication1
{
 public partial class invmain : Form
{
public invmain()
{
    InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
    DialogResult exit = MessageBox.Show("Exit the program?\t\t\t", "Exit Program", MessageBoxButtons.YesNo, MessageBoxIcon.Asterisk);
    if (exit == DialogResult.Yes)
    {
        this.Close();
    }
}

private void button2_Click(object sender, EventArgs e)
{
    additem form1 = new additem();
    form1.ShowDialog();
    //(form1.CreatedItem);
    this.Visible = true;
    this.Refresh();
}

private void deletebutton_Click(object sender, EventArgs e)
{
    DialogResult deleteitem = MessageBox.Show("Delete selected item?\t\t\t", "Delete Item", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
    if (deleteitem == DialogResult.Yes)
    {
        foreach (DataGridViewRow item in this.datagridview1.Rows)
        {
            datagridview1.Rows.RemoveAt(item.Index);
        }
        foreach (DataGridViewRow item in this.datagridview2.Rows)
        {
            datagridview2.Rows.RemoveAt(item.Index);
        }
        foreach (DataGridViewRow item in this.datagridview3.Rows)
        {
            datagridview3.Rows.RemoveAt(item.Index);
        }
        foreach (DataGridViewRow item in this.datagridview4.Rows)
        {
            datagridview4.Rows.RemoveAt(item.Index);
        }

        MessageBox.Show("Item deleted.\t\t\t", "Delete Item", MessageBoxButtons.OK, MessageBoxIcon.Information);    
    }
    this.Refresh();
}

Spark_2 wrote: "Could someone give me an example of how I can pass data to the original instance of a main form using { get; private set; }?"

Where is the data, that you want to pass, coming from? Can you post the code you're using, so we can get an idea of what you want to acomplish?

:)

Tekkno

Hi

I think you may be approaching this in slightly the wrong way. Instead of looking to have the addItem form communicate with the invMain form, you really want to have the invMain form ask the addItem form for it's data.

For example, add the following three properties to your addItem form:

        public string Category { get; set; }
        public string Item { get; set; }
        public int Quantity { get; set; }

Then, when you click your button that validates and closes the form, update the properties:

        private void button1_Click(object sender, EventArgs e)
        {
            this.Category = "Category from somewhere";
            this.Item = "Item from somewhere";
            this.Quantity = 10;
            this.Close();
        }

Note: button1 on this form has the DialogResult property set to Ok.

Now on your invMain form, when you open the form, you can check if the addItem form returned Ok and then request the data (if it is not Ok then you won't have the dialog result and therefore you won't work with the data - maybe the user cancelled).

So on your invMain form you would have something like:

        private void button1_Click(object sender, EventArgs e)
        {
            addItem addItemForm = new addItem();
            if (addItemForm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Console.WriteLine(addItemForm.Category);
                Console.WriteLine(addItemForm.Item);
                Console.WriteLine(addItemForm.Quantity);
            }
        }

Obviously this is a much simplified example of your post, but hopefully it will give you an idea of how to implement this into your current solution.

HTH

Thank you! That's what I was looking for:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.