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();
}