Ahhhh....
Well this really has two parts.
First, "How do I get to data in one class from another?" (In this case, from
a child window to a parent.)
Basically, all you need to know here is the relationship. Because this is an
MDIChild window, the relationship is a little different than if this was a
control or another window. So, this is probably the disconnect.
Since this is a child window of an MDI window, the window is put in an
MDIClient container. This is the list of MDI child window for the MDI
window. So, the parents parent is the MDI window itself. From there, you can
look in the controls collection of that window for the ToolStrip, and in the
ToolStrip's items for the ToolStripStatusLabel control.
In code this might look like:
((ToolStripLabel)((ToolStrip) ((MainForm)
Parent.Parent).Controls["toolStrip1"]).Items["myLabel"]).Text = "The new
text";
Note that to get this to work there is a lot of casting that has to be done.
That is because the Controls and Items collections only know they contain a
generic item. You must cast it to get the full attributes of your
specialized item. Of course, this might fail, because for whatever reason
your named item is not of the type you said it was, or because you misstyped
the name entirely. So, a safer way might be:
Mainform mainForm = Parent.Parent as MainForm;
if (mainForm != null) {
ToolStrip toolStrip = mainForm.Controls["toolStrip1"] as ToolStrip;
if (toolStrip != null) {
ToolStripLabel label = toolStrip.Items["meToo"] as
ToolStripLabel;
if (label != null) {
label.Text = "The new text";
}
}
}
Ugly, hugh? Well that leads me into part two.
Part two is, "What is the correct way to do this?"
I am going to leave the mechanics of this as an excercise here. If you can't
get it, let me know. But, this is critical for good design. And its best
learned by doing.
Good design says that each class should manage its own internals. Also,
other classes should not know about the internals of another class.
While the controls exposed here are public, they are buired in the UI. If
you had two or three controls that you needed to get from the ToolStrip, you
could quickly see how you would be writing a lot of code. But what if you
made a method of the main MDI form which encapsulated the code above? You
could pass in the name of the tool bar item you want, and it would return
the generic control. You would still need to cast it to the
ToolBarStatusLabel, but this is still way easier than the code above. And,
it would work for anything else you put in the ToolStrip.
Hope this helps...
Frisky
For the first part, the MDI parent window is accessible to the child via the
Parent property. The Parent is actually just a
wschlichtman said:
Oops, I left out a vital piece of info. I am trying to set the textlabel,
which is in the parent container, from the child instance.
Frisky said:
You add the text to the .Text property of the ToolStripStatusLabel. You
can
also set its tooltip by setting the .ToolTip property.
To get to a label you have already placed in your ToolStrip, use
something
similar to the following code:
toolStrip.Items["labelName"].Text = "My Label Text";
The ToolStrip.Items property has an overload on both name and the index.
(You must have set the name property of your label to be able to access
it
through the string indexer overload.)
Hope this helps...
Frisky
wschlichtman said:
I have an MDI app and would like to some text and place it in a
ToolStripStatusLabel. How can I accomplish this?