Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to access controls on one MDI Child From another form

Status
Not open for further replies.

dalec

Programmer
Jul 8, 2000
191
0
0
US
I have a MDI child open and when I click a button I open a regular form to ask the user more information (No problems so far), but, when I try to reference some controls on the Child form I get another instance of it instead of the control I'm looking for.

So, the question is... How do I get the proper info from the MDI child without Launching another instance?

I've searched here, but, havent found anything close to this question. Thanks in advance!!
 
To call a control of one form from another form you must use the form name and then the control name. For example, if the MDIForm has the focus set to it and you want to set a variable equal to the property value of a control on another form use the following format:

Variable = ChildForm.ChildControl.Property

Also make sure that the correct form has the focus set to it.
 
That's exactly what I'm doing, except because it's an MDI child form it creates another instance, instead of referencing the controls on the form that passed.

Any Idea's on an MDI Child form doing the same thing you do with regular forms?
 
Maybe this will help... What I'm trying to do is reference a MSFlex Grid on the Child form. Since it keeps creating a new instance, I'm now trying to pass the control to the other form through the tag property (not having very much success). My first method would be better if possible.
 
Just peforming a basic test.. I don't see that behavior. I've got one MDI form(MDIForm1), one child form(Form1) and one free-standing form(Form2). I've got a button on Form2 that, when clicked, gets the contents of a text box on Form1 and puts them in a text box on Form2.
It says:
Private Sub Command1_Click()
Text1 = Form1.Text1
End Sub


This does not cause another instance of Form1 to appear.

Anyway, another way to reference other forms would be:

Private Sub Command1_Click()
Dim frmForm as Form
For Each frmForm In Forms
If frmForm.Name = "Form1" Then
Text1 = frmForm.Text1
Exit For
End If
Next
Set frmForm = Nothing
End Sub

 
Your example was close, but what I'm doing in my child form is calling a regular form (modal) and there trying to reference back to the child, and thats when it creates the other instance.
 
There must be something else going on. I changed mine so there's a button on Form1 that opens Form2 modally. I click Command1 button on Form2 and it successfully retrieves Form1's Text1 contents without opening another instance of Form1.
In the back of my head I remember seeing behavior like what you're describing. As I recall, rather than diagnose what was going on, I decided on an altogether different course, changed everything around and it went away. I know that's lots of help but...
 
Maybe you can help with this, I traced the code and as soon as I reference an object on (using your example) form1, it fires the Form_Load event of form1 (I'm looking for a MSFlexGrid so...

form1.msflexgird.textmatrix (or whatever), then it creates fires the Child Form_Load event. When I first create the child I do it from the parent and do the usual stuff.

I tested it plain using your example and I do get another instance, but after running it I never get more than two instances...

Heres the Main MDI form code
(There is just 1 menu item)
Code:
Option Explicit
Private Sub LC_Click()
Dim newForm As New Form1
Set newForm = New Form1
newForm.Show
End Sub

The Child:
{There is a label and a button)
Code:
Option Explicit
Private Sub Command1_Click()
Form2.Show
End Sub
[/child]

The Called regular form from the child:
(just a label to display the caption from the child)
[code]
Option Explicit
Private Sub Form_Load()
Label1.Caption = Form1.Label1.Caption
End Sub

Any help would greatly be appreciated... Thanks
 
Possible clue:
I noticed when I call the child (from the main MDI form) it launchs the child form_load procedure twice. ? Huh?
 
I got it do work like yours.. Yeah,this is probably what happened to me that one time.
This won't fix it but: In your MDI form's LC_Click event, when you declare newForm as such
Dim newForm as New Form1
you don't need to then set it as a New Form1.
Declaring it as a New whatever takes care of instantiation for you.

Otherwise, I'm as stumped as you are.
A couple of solutions:
If you aren't going to need multiple instances of Form1, don't load it the way you are. Change the LC_Click event to:
Private Sub LC_Click()
Form1.Show
End Sub

and leave it at that. This prevents the extra Form1 later on.
Otherwise, like I said before, in your Form2 Form_Load event,
Private Sub Form_Load()
Dim frmForm As Form
For Each frmForm In Forms
If frmForm.Name = "Form1" Then
Text1 = frmForm.Text1
Exit For
End If
Next
Set frmForm = Nothing
End Sub

This seems to work as well.
If you're going to have multiple Form1s with duplicate names, give them something unique in ther .Tag property then change If frmForm.Name = "Form1" Then to If frmForm.Tag = "the form's unique tag" Then .
 
yeah, this is stange, I noticed any reference to the child by name starts a new instance, I added a global counter and a form counter and unloaded the extra child, then when I reference it later I get "form not loaded" (even though there's one instance of it...

Thing is, this was the only form in the application that was going to be a child. [neutral], It's kind of a general information form, and I was really wanting it to be available for multiple instances. I guess I need to rethink this, from what I've read about .net, forms are going to be retooled and I'll have to change my way of thinking anyways (maybe to make this sort of thing easier).

Thank you for your help.
 
I played around with going through the forms till I get the one it came from and I think I'll use it. It does keep the ghost from happening.

Thank you again!!!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top