access from one inherited to the other inherited class

  • Thread starter Thread starter newbie_csharp
  • Start date Start date
N

newbie_csharp

Hi,
I have a class structure like this:

rootform : System.Windows.Forms.Form
midiform : rootform
child1 : rootform
child11 : child1
child2 : rootform
chld21 : child2

there is an instance of child11 and child21. when user selects a record
in child21 (like a part inventory), it should pass the part# to child11
(call a public method in child11). in child21, how do I have access to
child11? I tried to find it in System.Windows.Forms.Form but it just
give me the active form (Form.ActiveForm).

user in midiform menubar can open child11 and child21 as many as the
want. so there is no garantee that when user select a record in child21,
I have child11 opened. so I should detect it in the collection (if not
found, open it). is there a forms collection that I can trace it and
find a form? how I should deal with this kind of situation?

thanks
 
Hi,

Nothing keeps track of the opened form. You need to do that by yourself. For
example you can keep one class with static Register and Unregister methods
and then when the form is created in the root form constructor (or probably
Load event would be better place) you can call Register. For unregistering
you can use Form's closed event
 
thanks Stoticho. I thought so. I will use an array in rootform to keep
the name of the forms. but how can I access to them? form example when I
am in child21 and I know that child11 is already opened, how do I have
access to it?
 
how about having a collection of Form type in rootform? all instances
should be a member of my collection. then I will have access to the
other members from any form. if C# doesn't have a collection for forms,
I guess my collection should work. is that right?
 
Hi,
That's why I suggested static register. If you put it in the rootform you
need to find a reference to it unless again this is not static.
How to identify the forms in the collection.... Well, you can use dictionary
(key/value collection) look at Hashtable for example. Each form you should
give an unique name (key). If only one instance of a form exists you can use
the Type object as a key.
If dictionaries doesn't work for you you need to enumerate the collection
and find the target form from some other critera
 
Stoitcho,

thanks for your help. it worked. now I can send a parameter form a form
to another form and change the focus to that. so I don't need more than
that. but there is one thing I am curious about it and I couldn't find
it. I add the form to the dictionary as the value. if the child has a
public method, I don't have access to it from the other form. for
example child11 has "mymethod()" public method. in child21, I can use
"focus()" of child11 but I don't have access to "mymethod()". I know
why, because the type of the value in dictionary is "Form", not child11
but it could be nice to this ability. is there any trick for doing this?

thanks
 
Hi,

The trick is to cast the object in the dictionary to the class you want. Of
course you need somehow to know what the real type of the object. If you
don't know the type of the object there is not much you can do about it.
 

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

Back
Top