Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Can a component obtain a reference to its container object?

Can a component obtain a reference to its container object?

Scheduled Pinned Locked Moved C#
questiondocker
11 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    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

    J 1 Reply Last reply
    0
    • L Lost User

      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

      J Offline
      J Offline
      James T Johnson
      wrote on last edited by
      #2

      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

      L A 2 Replies Last reply
      0
      • J James T Johnson

        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

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        That 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.

        1 Reply Last reply
        0
        • J James T Johnson

          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

          A Offline
          A Offline
          Andy Smith
          wrote on last edited by
          #4

          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".

          L J 2 Replies Last reply
          0
          • A Andy Smith

            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".

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            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.

            L 1 Reply Last reply
            0
            • L Lost User

              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.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              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.

              A J 2 Replies Last reply
              0
              • L Lost User

                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.

                A Offline
                A Offline
                Andy Smith
                wrote on last edited by
                #7

                why don't you do a: Type containerType = Container.GetType(); and see what the container really is?

                1 Reply Last reply
                0
                • L Lost User

                  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.

                  J Offline
                  J Offline
                  James T Johnson
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  • A Andy Smith

                    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".

                    J Offline
                    J Offline
                    James T Johnson
                    wrote on last edited by
                    #9

                    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

                    A 1 Reply Last reply
                    0
                    • J James T Johnson

                      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

                      A Offline
                      A Offline
                      Andy Smith
                      wrote on last edited by
                      #10

                      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.

                      J 1 Reply Last reply
                      0
                      • A Andy Smith

                        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.

                        J Offline
                        J Offline
                        James T Johnson
                        wrote on last edited by
                        #11

                        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

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups