471,321 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,321 software developers and data experts.

Trying to access control on one from from another form

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



Jan 28 '06 #1
4 1871

"JimC" <ji**@nospam.pls> wrote in message
news:uU*******************@newssvr13.news.prodigy. com...
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
Jan 28 '06 #2
Pass a reference to your main form in the constructor of your InfoForm.
--
Dale Preston
MCAD C#
MCSE, MCDBA
"JimC" wrote:
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



Jan 28 '06 #3
I wrote:
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

Jan 29 '06 #4

"JimC" <ji**@nospam.pls> wrote in message
news:mQ*******************@newssvr21.news.prodigy. com...
I wrote:
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.
Jan 29 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Julia Baresch | last post: by
6 posts views Thread by Peter Frost | last post: by
49 posts views Thread by Yannick Turgeon | last post: by
reply views Thread by jphelan | last post: by
12 posts views Thread by Phil Certain | last post: by
9 posts views Thread by prakashwadhwani | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.