altering controls from a child form

Anti-Rich

Well-known member
Joined
Jul 1, 2006
Messages
325
Location
Perth, Australia
Programming Experience
1-3
Hi guys,

i am developing an MDI application and things are going relatively smoothly... but...
i am trying to change the Text property of a ToolStriplabel, to indicate that the save was a success, now for the life of me i cant remember how to alter a control from another form!! i have done it in a previous project but i lost it and now im stumped. i would greatly appreciate any help anyone can give.


cheers
adam
 
Forms are objects and you access a member of any object the same way. When you create an MDI child form you set its MdiParent property to 'Me', so how do you suppose a child form could access its parent?
 
well, i tried using...

VB.NET:
Me.ParentForm.Controls("toolStripStatus").Text = "Save Complete!"

but it threw an error saying Object reference not set to an instance of an object.

i know how to fix this with most other situations, but i have no clue what its referring to... is it saying a need to re instanciate a new parent form, a new label...

im not sure, i think the code above is close, im just missing one last (important) piece of the puzzle...

cheers for your reply

regards
adam
 
And does your parent form have a control named "toolStripStatus"? Note that ToolStripStatusLabel is NOT a control, so if that's what you're trying to set then you can't do it that way.

Regardless, I would consider it preferable to declare a public SetStatus method in your parent form, which you would call something like this:
VB.NET:
DirectCast(Me.ParentForm, Form1).SetStatus("Save Complete!")
It would then be the responsibility of the parent form to put the supplied string in the right place.
 
ahh, i see now. i didnt know that the ToolStripStatusLabel wasnt actually a proper control (in the sense that another form would recognise it).

what does the DirectCast method specifically do? does it replace the Ctype method?
 
A ToolStripStatusLabel isn't a contol of any sort, proper or otherwise, recognised by another form or otherwise. A control is an instance of a class that inherits the Control class, either directly or indirectly. ToolStripStatusLabel doesn't inherit Control therefore ToolStripStatusLabels are not controls. Like the Control class, The ToolStripStatusLabel class is derived from the Component class. They are like cousins a couple of times removed, not ancestors. Even if they were, the labels almost certainly wouldn't be found in the form's Controls collection because they would likely be parented by the StatusStrip, thus being found in its Controls collection.

If you'd like to know what DirectCast does and how it differs from CType then the MSDN library is the place to look.
 
The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword, as the following example shows:
Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.
Both keywords take an expression to be converted as the first argument, and the type to convert it to as the second argument. Both conversions fail if there is no conversion defined between the data type of the expression and the data type specified as the second argument.
The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType.
In the preceding example, the run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer.
DirectCast throws an InvalidCastException error if the argument types do not match.

ok, i looked it up on msdn, but i dont understand why you would need to re-cast something to the same type?

could you clarify this part for me?

i understand the difference between the two, i just dont understand the necessecity...

cheers
 
It's not casting to the same type. What type is the ParentForm property? It's type Form. Does the Form class have a SetStatus method? No it doesn't. The underlying object is type Form1 or whatever but if you're using a reference of type Form then you can only access members of type Form. True casting doesn't affect the underlying object in any way whatsoever. It merely provides a reference of the specified type. By getting a reference of type Form1 you're able to access members of the Form1 class, specifically the SetStatus method in this case.

When you cast a reference you are telling the compiler that you will take responsibility for making sure that the object referred to is actually that type. As I said, the ParentForm property is type Form, so it can return any form at all. Let's say that you put this same child form inside a different parent form; one that doesn't have a SetStatus method. If you called that code at run time you're app would crash. The compiler can't make you make sure that the form is the correct type, but by requiring you to make a cast it is alerting you to the fact that you had better make sure or the app will crash and it will be your fault.

Have you ever heard the expression "to cast something in a different light"? That's how "cast" is used in programming. It doesn't mean changing the thing you're looking at. It means looking at the same thing but in a different way.
 
ah ok, that makes sense when its put like that... cheers for your help jmc,

its much appreciated

have a good one mate

regards
adam

ps. oh and the code you suggested worked!! cheers :)
 
CType and DirectCast:

Dim s as String = "123"
Dim o as Object = s

Dim i as Integer = CType(o, Integer) 'succeeds, the numerical string (boxed up in an object but still a string) is converted to an integer, probably using Convert.ToInt32() function hidden away
Dim j as Integer = DirectCast(o, Integer) 'fails - the o is an objecttype box around a string. a string cannot be cast into an integer, it must be converted


You can only cast something into a type represented in its ancestry. A TextBox ancestry looks like:

Object\MarshallByRefObject\Component\Control\TextBoxBase\TextBox

You can Dim a variable as anything in this list and assign a textbox to it. You can DirectCast the reference to anything else in the list, but you cannot use DirectCast to convert it into anything else. No conversion exists to convert a textbox into anything, but if it did, CType would be able to perform the conversion as well as the cast, DirectCast can only perform the cast

I personally never use CType, only DirectCast and Convert.xxx depending on whether i want to convert, or cast.. When programming I prefer to be explicit :D
 
Back
Top