Click here to Skip to main content
15,893,904 members
Home / Discussions / C#
   

C#

 
AnswerRe: Retrieving Data From Form2 Pin
Pete O'Hanlon12-Dec-11 22:24
mvePete O'Hanlon12-Dec-11 22:24 
GeneralRe: Retrieving Data From Form2 Pin
AmbiguousName12-Dec-11 23:13
AmbiguousName12-Dec-11 23:13 
GeneralRe: Retrieving Data From Form2 Pin
Pete O'Hanlon12-Dec-11 23:35
mvePete O'Hanlon12-Dec-11 23:35 
AnswerRe: Retrieving Data From Form2 Pin
Subin Mavunkal12-Dec-11 23:52
Subin Mavunkal12-Dec-11 23:52 
GeneralRe: Retrieving Data From Form2 Pin
Pete O'Hanlon13-Dec-11 0:14
mvePete O'Hanlon13-Dec-11 0:14 
AnswerRe: Retrieving Data From Form2 Pin
DaveyM6913-Dec-11 1:04
professionalDaveyM6913-Dec-11 1:04 
GeneralRe: Retrieving Data From Form2 Pin
Subin Mavunkal13-Dec-11 2:03
Subin Mavunkal13-Dec-11 2:03 
AnswerRe: Retrieving Data From Form2 Pin
BillWoodruff13-Dec-11 12:14
professionalBillWoodruff13-Dec-11 12:14 
This type of question: communication of data or events between two forms, is among the most commonly asked and answered type of question on QA in CP.

The critical piece of information to have, imho, in considering a solution is to know under what circumstances Form1 and Form2 (to use your form names) are created:

1. Keeping a run-time created reference: if Form1 creates Form2, or Form2 creates Form1: at the moment of creating it: the Form that creates has a reference to the other Form it has created.

That reference can be stored, and, then, all public properties (and methods, or anything else that's public) can be accessed by the creating Form.

In terms of the question of knowing when Form2 is closed: if Form1 creates Form2: at the moment its created and assigned to some variable in Form1: you can immediately subscribe to Form2's Closing or Closed events, and add an EventHandler defined in Form1.

Here's a code example that demonstrates both the above. First, this is implemented in Form2:
C#
public DataGridView Form2DataGridView { get; set;  }

private void Form2_Load(object sender, EventArgs e)
{
    Form2DataGridView = dataGridView1;
}
And, then, this is implemented in Form1:
// inside Form1
private Form2 NewForm2
//
private void Form1_Load(object sender, EventArgs e)
{
    NewForm2 = new Form2();
    NewForm2.Closed += new EventHandler(NewForm2_Closed);

    NewForm2.Form2DataGridView.SelectionChanged += new EventHandler(Form2DataGridView_SelectionChanged);
}

private void NewForm2_Closed(object sender, EventArgs e)
{
    // your code goes here
}

public void Form2DataGridView_SelectionChanged(object sender, EventArgs e)
{
    // your code goes here
}
1.a. The special case of using 'ShowDialog' for presentation of a Form is a special case of this: imho, nothing in the OP suggests 'ShowDialog' is being used. But, you have some good answers here, now, based on the assumption you use 'ShowDialog.' But, please note that you can create one, or both Forms, once, and keep a reference ... if one Form creates the other ... just as described above. Just re-use the reference to the instance of the Form in the ShowDialog mode.

1.b. The special case where both instances of Form1 and Form2 are created by some other entity: well, you can adapt what's suggested in points #1~2. ... edit ... This also is the case, imho, where you may explore the use of Interfaces to pass instances of Forms that implement the Interface cast to the Interface so you effectively limit what is exposed by the passed object. S. A. Kryukov often advocates this method in his QA answers: for example:[^]. ... edit ...

2. Injection: This usually means that one Form has a "vacant" public property, and another Form, when creating the second form with the property sets the value of the public property.

So, Form1 could have a public property of type DataGridView, and in Form2's Load event, Form2 could set the value of that property to the instance's DataGridView: but look at what is implied by that: Form2 must have access to the instance of Form1 ! To me this is "dog wag tail" implemenation: where this makes sense is if Form1 contains the DataGridView, and Form1 creates Form2, and Form2 needs access to the DataGridView on Form1. Then, this is easy because Form1 has access to the instance of Form2 at the moment it creates it.

2.a. I have to admit I have a strong bias against the idea of injecting a reference to a whole Class, or Form, into another Form or Class because I believe this is a violation of the principle of "separation of concerns."

Does Form1 really need access to everything public in Form2 ? If the answer is "yes," then I'd go for code as shown in the example above. But, if the answer is "no," then I'd modify Form1 code as shown above:
// inside Form1
private DataGridView F2DataGridView;
//
private void Form1_Load(object sender, EventArgs e)
{
    // edit : corrected omission of 'Form2 here ...
    Form2 NewForm2 = new Form2();
    NewForm2.Closed += new EventHandler(NewForm2_Closed);
  
    F2DataGridView = NewForm2.Form2DataGridView;

    F2DataGridView .SelectionChanged += new EventHandler(F2DataGridView _SelectionChanged);
}

private void NewForm2_Closed(object sender, EventArgs e)
{
    // your code goes here
}

public void F2DataGridView _SelectionChanged(object sender, EventArgs e)
{
    // your code goes here
}
The difference in code may seem trivial: but, this implementation means that the only "exposed" object in Form2's instance within Form1's instance is the DataGridView on Form2, because the variable NewForm2 exists only in the scope of Form1's Load EventHandler.

2. Use of some "external" class to expose objects to other classes: You can create a public static class that maintains public Properties which can be set in each Form's 'Load, or 'Shown, EventHandlers, and then are visible to any Form in the same NameSpace. You could even set EventHandlers within that static class.

3. Raising events: This is probably the strategy most appropriate for the scenario you have described, particularly since it enables multiple subscribers to consumers (creators and users) of the instance(s) of Form2.

I've omitted code examples for #2~3, believing they are easy to find here on CP, within articles, tip/tricks, and QA, but if you have further questions, just ask.
"For no man lives in the external truth among salts and acids, but in the warm, phantasmagoric chamber of his brain, with the painted windows and the storied wall." Robert Louis Stevenson


modified 14-Dec-11 6:42am.

AnswerRe: Retrieving Data From Form2 Pin
BobJanova13-Dec-11 23:11
BobJanova13-Dec-11 23:11 
QuestionSeriously? user-defined conversions to or from an interface are not allowed Pin
Alaric_12-Dec-11 9:16
professionalAlaric_12-Dec-11 9:16 
AnswerRe: Seriously? user-defined conversions to or from an interface are not allowed Pin
BobJanova14-Dec-11 5:09
BobJanova14-Dec-11 5:09 
QuestionTCPClient and TCPListener Pin
michaelgr112-Dec-11 2:38
michaelgr112-Dec-11 2:38 
AnswerRe: TCPClient and TCPListener Pin
jschell12-Dec-11 8:32
jschell12-Dec-11 8:32 
GeneralRe: TCPClient and TCPListener Pin
michaelgr112-Dec-11 8:57
michaelgr112-Dec-11 8:57 
GeneralRe: TCPClient and TCPListener Pin
Addy Tas12-Dec-11 11:44
Addy Tas12-Dec-11 11:44 
GeneralRe: TCPClient and TCPListener Pin
jschell13-Dec-11 8:32
jschell13-Dec-11 8:32 
QuestionWriting a 6502 cross assembler in c# Pin
Derek Henderson12-Dec-11 0:27
Derek Henderson12-Dec-11 0:27 
AnswerRe: Writing a 6502 cross assembler in c# Pin
Luc Pattyn12-Dec-11 1:05
sitebuilderLuc Pattyn12-Dec-11 1:05 
GeneralRe: Writing a 6502 cross assembler in c# Pin
Derek Henderson12-Dec-11 1:19
Derek Henderson12-Dec-11 1:19 
GeneralRe: Writing a 6502 cross assembler in c# Pin
harold aptroot12-Dec-11 4:52
harold aptroot12-Dec-11 4:52 
AnswerRe: Writing a 6502 cross assembler in c# Pin
Richard MacCutchan12-Dec-11 5:01
mveRichard MacCutchan12-Dec-11 5:01 
GeneralRe: Writing a 6502 cross assembler in c# Pin
Derek Henderson12-Dec-11 5:06
Derek Henderson12-Dec-11 5:06 
AnswerRe: Writing a 6502 cross assembler in c# Pin
jschell12-Dec-11 8:34
jschell12-Dec-11 8:34 
Questionestablishing a connection error for mssql 2005 Pin
Member 808991411-Dec-11 23:53
Member 808991411-Dec-11 23:53 
AnswerRe: establishing a connection error for mssql 2005 Pin
thatraja12-Dec-11 3:03
professionalthatraja12-Dec-11 3:03 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.