Can a component obtain a reference to its container object?
-
Can a component obtain a reference to its container object? I'm writing a component that needs two pieces of information about its container: 1. Is the container a form? 2. If so, what is its window handle? (.Handle property) The "Container" object only lets me access a very limited interface that doesn't do what I want. --Thanatos
You should be able to test the Container object for what type it is, then once you know it is a specific type you can cast it to that type and access the property you want. ie
if( Container is System.Windows.Form ) { System.Windows.Form containerForm = (Form) Container; // Use containerForm }
HTH, James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971 -
You should be able to test the Container object for what type it is, then once you know it is a specific type you can cast it to that type and access the property you want. ie
if( Container is System.Windows.Form ) { System.Windows.Form containerForm = (Form) Container; // Use containerForm }
HTH, James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971That sounded good, so I tried this test: I built a simple component with this method: public void ChangeParentColor() { if( Container is Form ) { Form containerForm = (Form)Container; containerForm.BackColor = Color.Aqua; } } This built just fine. I placed this component on a new form, along with a button. When the button was clicked, this method fired: private void buttonclick(object sender, System.EventArgs e) { containerTestComponent1.ChangeParentColor(); button1.Text="I was clicked"; } This project also built just fine, but even though the caption of the button changes properly (demonstrating that the button event fired), the color of the form refused to change to aqua.
-
You should be able to test the Container object for what type it is, then once you know it is a specific type you can cast it to that type and access the property you want. ie
if( Container is System.Windows.Form ) { System.Windows.Form containerForm = (Form) Container; // Use containerForm }
HTH, James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971While i'm not going to offer any advice in the container issue... I just thought you might like to know that that code is not the best way to do what you are doing. It's better* to do this: Form containerForm = Container as Form; if( containerForm != null ) { // Use containerForm } * "better" meaning that the produced IL is smaller and faster. If you don't believe me, make a simple executable and examine the IL yourself. The general rule-o-thumb is: If you are going to do a cast directly after an "if-is" and use that casted object, use "as" instead. If you just want to test the type and not do something with the object as that type, use "is".
-
While i'm not going to offer any advice in the container issue... I just thought you might like to know that that code is not the best way to do what you are doing. It's better* to do this: Form containerForm = Container as Form; if( containerForm != null ) { // Use containerForm } * "better" meaning that the produced IL is smaller and faster. If you don't believe me, make a simple executable and examine the IL yourself. The general rule-o-thumb is: If you are going to do a cast directly after an "if-is" and use that casted object, use "as" instead. If you just want to test the type and not do something with the object as that type, use "is".
Hmm... I haven't done any tests to see if (Container is Form) evaluates to true; I just assumed it would if the container was a form. If it doesn't, that could be the reason why the code isn't running. Your method may work where the other did not. I'll try it out when I get home.
-
Hmm... I haven't done any tests to see if (Container is Form) evaluates to true; I just assumed it would if the container was a form. If it doesn't, that could be the reason why the code isn't running. Your method may work where the other did not. I'll try it out when I get home.
Nope, it doesn't work. Upon further inspection, ( containerForm != null ) is evaluating as false, indicating that something is wrong with the typecasting. I'm thinking it may not be possible to cast a Container to a Form... ...in which case I'll have to do things the hard way: pass the form to the component as a parameter. I'd rather not, though, if I don't have to.
-
Nope, it doesn't work. Upon further inspection, ( containerForm != null ) is evaluating as false, indicating that something is wrong with the typecasting. I'm thinking it may not be possible to cast a Container to a Form... ...in which case I'll have to do things the hard way: pass the form to the component as a parameter. I'd rather not, though, if I don't have to.
why don't you do a: Type containerType = Container.GetType(); and see what the container really is?
-
Nope, it doesn't work. Upon further inspection, ( containerForm != null ) is evaluating as false, indicating that something is wrong with the typecasting. I'm thinking it may not be possible to cast a Container to a Form... ...in which case I'll have to do things the hard way: pass the form to the component as a parameter. I'd rather not, though, if I don't have to.
In addition to what Andy wrote your best bet is to code for the lowest common denominator. If you only need to use properties/methods that a control has, then compare to System.Windows.Forms.Control. The container may not be a form but it could be a Panel, or a UserControl or some other class. James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971
-
While i'm not going to offer any advice in the container issue... I just thought you might like to know that that code is not the best way to do what you are doing. It's better* to do this: Form containerForm = Container as Form; if( containerForm != null ) { // Use containerForm } * "better" meaning that the produced IL is smaller and faster. If you don't believe me, make a simple executable and examine the IL yourself. The general rule-o-thumb is: If you are going to do a cast directly after an "if-is" and use that casted object, use "as" instead. If you just want to test the type and not do something with the object as that type, use "is".
I thought about that after I had written the code, but I assumed that 'as' was implemented as
Container is Form ? (Form) Container : null
Apparently I made an ass of myself :-P James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971 -
I thought about that after I had written the code, but I assumed that 'as' was implemented as
Container is Form ? (Form) Container : null
Apparently I made an ass of myself :-P James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971well, don't think too badly of yourself. From the SDK Docs: ----------------------- The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form: expression as type is equivalent to: expression is type ? (type)expression : (type)null except that expression is evaluated only once. ----------------------- so it is logically exactly as you claim... but the actual IL implementation is different, and not describable with c#... which explains the requirement of the new "as" keyword.
-
well, don't think too badly of yourself. From the SDK Docs: ----------------------- The as operator is like a cast except that it yields null on conversion failure instead of raising an exception. More formally, an expression of the form: expression as type is equivalent to: expression is type ? (type)expression : (type)null except that expression is evaluated only once. ----------------------- so it is logically exactly as you claim... but the actual IL implementation is different, and not describable with c#... which explains the requirement of the new "as" keyword.
ah, maybe thats why I thought that :) James Sonork: Hasaki "I left there in the morning with their God tucked underneath my arm their half-assed smiles and the book of rules. So I asked this God a question and by way of firm reply, He said - I'm not the kind you have to wind up on Sundays." "Wind Up" from Aqualung, Jethro Tull 1971