Trying to access control on one from from another form

  • Thread starter Thread starter JimC
  • Start date Start date
J

JimC

On my main form in a C# program, I create an instance of another form that
contains a ListView control, in the usual way, that is:

public class frmMain : System.Windows.Forms.Form
{

// [...]

InfoForm myInfoForm = new InfoForm( );
myInfoForm.Show ( );

// [...]
}


When I click on the ListView on myInfoForm, I want to
be able to access two or three controls on the main form which
have public access (or can be changed easily to have public
access).

Seems like a simple enough requirement, similar to everything else
I do in C#, but I can't quite make this happen. Anyone know how?

Jim
 
JimC said:
On my main form in a C# program, I create an instance of another form that
contains a ListView control, in the usual way, that is:

public class frmMain : System.Windows.Forms.Form
{

// [...]

InfoForm myInfoForm = new InfoForm( );
myInfoForm.Show ( );

// [...]
}


When I click on the ListView on myInfoForm, I want to
be able to access two or three controls on the main form which
have public access (or can be changed easily to have public
access).

Seems like a simple enough requirement, similar to everything else
I do in C#, but I can't quite make this happen. Anyone know how?

Jim

The title of the thread should be

"Trying to access control on one *form* from another form."
Sorry for the typo which almost seems de rigueur in an unintended way.

Jim
 
I said:
On my main form in a C# program, I create an instance of another form
that
contains a ListView control, in the usual way, that is:

public class frmMain : System.Windows.Forms.Form
{

// [...]

InfoForm myInfoForm = new InfoForm( );
myInfoForm.Show ( );

// [...]
}


When I click on the ListView on myInfoForm, I want to
be able to access two or three controls on the main form which
have public access (or can be changed easily to have public
access).

Seems like a simple enough requirement, similar to everything else
I do in C#, but I can't quite make this happen. Anyone know how?

Dale responds:
Pass a reference to your main form in the constructor of your InfoForm.


Yes, of course. Thanks, Dale! I was making the common mistake of
creating a field of type Form in my InfoForm class, rather than
a field of type frmMain. Works fine now.

Just for the heck of it -- if it ain't broke, then break it -- would like
to implement an alternate approach by creating an event and a
delegate. I'll report back to this thread next time I'm on.

Jim
 
JimC said:
I said:
On my main form in a C# program, I create an instance of another form
that
contains a ListView control, in the usual way, that is:

public class frmMain : System.Windows.Forms.Form
{

// [...]

InfoForm myInfoForm = new InfoForm( );
myInfoForm.Show ( );

// [...]
}


When I click on the ListView on myInfoForm, I want to
be able to access two or three controls on the main form which
have public access (or can be changed easily to have public
access).

Seems like a simple enough requirement, similar to everything else
I do in C#, but I can't quite make this happen. Anyone know how?

Dale responds:
Pass a reference to your main form in the constructor of your InfoForm.


Yes, of course. Thanks, Dale! I was making the common mistake of
creating a field of type Form in my InfoForm class, rather than
a field of type frmMain. Works fine now.

Just for the heck of it -- if it ain't broke, then break it -- would like
to implement an alternate approach by creating an event and a
delegate. I'll report back to this thread next time I'm on.

Jim

The "alternative" is what you should start with.

Your current solution makes your subform untestable and unusable without
your main form and makes your main form harder to maintain because a
maintainer has to know that parts of the form are being changed from
outside.
 
Back
Top