Reputation: 307
I have text boxes on my second form and a send button which is in the code shown below.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.PassName = richTextBox1.Text;
f1.PassLastName = richTextBox2.Text;
f1.PassAge = comboBox1.Text;
f1.PassGender = richTextBox3.Text;
f1.ShowDialog();
}
and a DataGridView
on form 1 with this code
public partial class Form1 : Form
{
private string name;
private string lastName;
private string age;
private string gender;
public string PassName
{
get { return name; }
set { name = value; }
}
public string PassLastName
{
get { return lastName; }
set { lastName = value; }
}
public string PassAge
{
get { return age; }
set { age = value; }
}
public string PassGender
{
get { return gender; }
set { gender = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = name;
dataGridView1.Rows[n].Cells[1].Value = lastName;
dataGridView1.Rows[n].Cells[2].Value = age;
dataGridView1.Rows[n].Cells[3].Value = gender;
}
private void mnuExit_Click(object sender, EventArgs e) //adding the quit on the top file with caution message
{
if (MessageBox.Show("Do you really want to Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
private void addTask_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //show form2 so user can input data
f2.ShowDialog();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}`
This is fine if I want to send one set of data to the DataGridView
, but if I add new information again then this opens a new DataGridView
and stores it to another seperate DataGridView
then I have two DataGridView
forms. I want to put all the data onto one DataGridView
and keep adding rows. So when the user clicks the add button on the first form with the DataGridView
, it opens up the TextBox
form which is form 2, then the user fills out the information and clicks the send button which sends the information back to the DataGridView
, however this then opens up a new window with a new DataGridView
. I dont want this to happen I want it to keep adding rows on the first form.
Could someone show me how to do this?
Upvotes: 0
Views: 72
Reputation: 1587
You can use ShowDialog(this) and Owner to get the parent form's property.
Form1
private void Form1_Load(object sender, EventArgs e)
{
//Move to Form1_Activated
this.Activated += new System.EventHandler(this.Form1_Activated); //connect
}
private void Form1_Activated(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = name;
dataGridView1.Rows[n].Cells[1].Value = lastName;
dataGridView1.Rows[n].Cells[2].Value = age;
dataGridView1.Rows[n].Cells[3].Value = gender;
}
private void addTask_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //show form2 so user can input data
f2.ShowDialog(this);//set this form as Owner
}
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;//Get the Owner form
f1.PassName = richTextBox1.Text;
f1.PassLastName = richTextBox2.Text;
f1.PassAge = comboBox1.Text;
f1.PassGender = richTextBox3.Text;
//f1.ShowDialog();
f1.Show();
this.Close();
}
Upvotes: 1