Hi,
You can communicate between form using events.
Add to your project file: SearchResultEventArgs.cs:
public class SearchResultEventArgs : EventArgs
{
public string ResultText1
{
get;
private set;
}
public string ResultText2
{
get;
private set;
}
public SearchResultEventArgs(string result1, string result2)
{
ResultText1 = result1;
ResultText2 = result2;
}
}
Add this code to your Form2:
public event EventHandler<SearchResultEventArgs> SearchResult;
private void OnSearchResult(string result1, string result2)
{
var handler = SearchResult;
if (handler != null)
{
handler(this, new SearchResultEventArgs(result1, result2));
}
}
Modify your DataGridView CellContentClick event (on Form2):
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
DataGridViewRow row = dataGridView1.Rows[e.RowIndex];
OnSearchResult(row.Cells[0].Value.ToString(), row.Cells[1].Value.ToString);
}
Now in Form1 add this method:
private void form2_SearchResult(object sender, SearchResultEventArgs e)
{
textBox1.Text = e.ResultText1;
textBox2.Text = e.ResultText2;
}
And modify code that you're using to call Form2:
using (var form2 = new Form2())
{
...
form2.SearchResult += new EventHandler<SearchResultEventArgs>(form2_SearchResult);
if (form2.ShowDialog() == DialogResult.OK)
{
}
...
}
Now few links:
http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx[
^]
http://msdn.microsoft.com/library/system.eventargs%28v=vs.110%29.aspx[
^]
Step by Step: Event handling in C#[
^]
Generic Event Arguments[
^]
Hope it helps you :)