MDI Application

T

Tony Johansson

Hello!

I just made a test for fun to try to change all the MDI child so each one
had an add a Button.
I put the code below in an event handler but no Button is added the child
form was unchanged.
Can somebody explain why it's not possibly do in the way I did ?
I mean the tempChild and MdiChildren[x] is refering to the same object so
the
only thing that I can think of is that the child object is read only which
would explain
why my test didin't work.


for (int x = 0; x < this.MdiChildren.Length; x++)
{
// Create a temporary Button control to add to the child form.
Button tempButton = new Button();
// Set the location and text of the Button control.
tempButton.Location = new Point(100, 100);
tempButton.Text = "OK";
// Create a temporary instance of a child form (Form 2 in this
case).
Form tempChild = (Form)this.MdiChildren[x];
// Add the Button control to the control collection of the form.
tempChild.Controls.Add(tempButton);
}

//Tony
 
J

Jeff Johnson

Tony Johansson said:
Hello!

I just made a test for fun to try to change all the MDI child so each one
had an add a Button.
I put the code below in an event handler but no Button is added the child
form was unchanged.
Can somebody explain why it's not possibly do in the way I did ?
I mean the tempChild and MdiChildren[x] is refering to the same object so
the
only thing that I can think of is that the child object is read only which
would explain
why my test didin't work.


for (int x = 0; x < this.MdiChildren.Length; x++)
{
// Create a temporary Button control to add to the child form.
Button tempButton = new Button();
// Set the location and text of the Button control.
tempButton.Location = new Point(100, 100);
tempButton.Text = "OK";
// Create a temporary instance of a child form (Form 2 in this
case).
Form tempChild = (Form)this.MdiChildren[x];
// Add the Button control to the control collection of the
form.
tempChild.Controls.Add(tempButton);
}

How about tempButton.Visible = true? Just a thought.
 
T

Tony Johansson

Jeff Johnson said:
Tony Johansson said:
Hello!

I just made a test for fun to try to change all the MDI child so each one
had an add a Button.
I put the code below in an event handler but no Button is added the child
form was unchanged.
Can somebody explain why it's not possibly do in the way I did ?
I mean the tempChild and MdiChildren[x] is refering to the same object so
the
only thing that I can think of is that the child object is read only
which would explain
why my test didin't work.


for (int x = 0; x < this.MdiChildren.Length; x++)
{
// Create a temporary Button control to add to the child form.
Button tempButton = new Button();
// Set the location and text of the Button control.
tempButton.Location = new Point(100, 100);
tempButton.Text = "OK";
// Create a temporary instance of a child form (Form 2 in this
case).
Form tempChild = (Form)this.MdiChildren[x];
// Add the Button control to the control collection of the
form.
tempChild.Controls.Add(tempButton);
}

How about tempButton.Visible = true? Just a thought.

No the result was the same. No Button is shown on the Form.
Any more suggestions ?

//Tony
 
T

Tony Johansson

Jeff Johnson said:
Are there any other controls on the child forms?

Yes just a Button where I can close the form. This Button isn't nesessary
because I can close the form by using the icon up to the right.

//Tony
 
T

Tony Johansson

Tony Johansson said:
Yes just a Button where I can close the form. This Button isn't nesessary
because I can close the form by using the icon up to the right.

//Tony

I add a label to the actual Form and then I add some text to this label in
this method and that worked.
In the foreach I get a hit for the Button I add in the method but no Button
is shown in the child form.
This is strange ?

private void TestToolStripMenuItem_Click(object sender, EventArgs e)
{
for (int x = 0; x < this.MdiChildren.Length; x++)
{
// Create a temporary Button control to add to the child form.
Button tempButton = new Button();
// Set the location and text of the Button control.
tempButton.Location = new Point(100, 100);
tempButton.Text = "OK";
tempButton.Visible = true;
// Create a temporary instance of a child form (Form 2 in this
case).
Form tempChild = (Form)this.MdiChildren[x];
tempChild.Controls.Add(tempButton);

foreach (Control ctrl in tempChild.Controls)
{
if (ctrl is Label)
{
ctrl.Text = "testing";
}

if (ctrl is Button)
{
}
}
}
}
 
T

Tony Johansson

Peter Duniho said:
You have a bug. It's somewhere in the code you _didn't_ bother to post.
So it's impossible for anyone here to point it out to you.

I've copied below a concise-but-complete code example that demonstrates
the effect you desire. It works fine. Compare my implementation with
your own code and figure out what's different. When you find that, fix
it. Then your program should work.

Pete

My bug was that the MDI child had a Richtextbox's Dock=Fill so there was not
possible for any MDI child to show any Button.

//Tony
 
J

Jeff Johnson

My bug was that the MDI child had a Richtextbox's Dock=Fill so there was
not possible for any MDI child to show any Button.

Which is EXACTLY why I asked if there were any other controls on the child
forms, to which you replied that there was only another button and didn't
mention the rich text box at all!
 
T

Tony Johansson

It was easy to miss which I did because the RichTextBox vovered the whole
form.

//Tony
 
R

Registered User

It was easy to miss which I did because the RichTextBox vovered the whole
form.
Here is a tip which can speed up problem solving. When the dynamically
created controls failed to appear on the form, it was time to write
some throwaway code in the form of a new project. This project's sole
purpose would have been to explore why dynamically created controls
weren't appearing on the form. There is nothing else to figuratively
and literally get in the way. On your first go round the controls
would act as they should identifying the problem as the specific form
with "nothing" on it.

Yes it's the same outcome but you could have solved the problem in
considerably less time. There is nothing wrong with writing throwaway
code, the real investment is in the wetware.

regards
A.G.
 
J

Jeff Johnson

It was easy to miss which I did because the RichTextBox vovered the whole
form.

And therefore your form should have looked mostly white, which SHOULD have
been a total giveaway since an empty form should have a gray background,
right?
 

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