Trying to access control on one from from another form

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
 
J

JimC

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
 
J

JimC

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
 
N

Nick Hounsome

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top