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. Geting the name of the form a user control is on

Geting the name of the form a user control is on

Scheduled Pinned Locked Moved C#
winformshelpquestion
7 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.
  • C Offline
    C Offline
    chrisclarke11
    wrote on last edited by
    #1

    Hi I've created a number of user controls to use on forms. What i need to do now is get a enum of variables from the form onto the user controls. I am trying to write the code in the user control that just looks for a Enum with a fixed name on the form. Any ideas? The problem i want to be able to reuse these controls on any form aslong as the Enum is there.

    Chris

    T 1 Reply Last reply
    0
    • C chrisclarke11

      Hi I've created a number of user controls to use on forms. What i need to do now is get a enum of variables from the form onto the user controls. I am trying to write the code in the user control that just looks for a Enum with a fixed name on the form. Any ideas? The problem i want to be able to reuse these controls on any form aslong as the Enum is there.

      Chris

      T Offline
      T Offline
      Tony Richards
      wrote on last edited by
      #2

      You can access a user control's parent's properties using the Parent property of the UserControl. If you need to access something not part of the Form class you'll need to cast the property to the Form type, but be warned, this can hinder reusing the control.

      C 1 Reply Last reply
      0
      • T Tony Richards

        You can access a user control's parent's properties using the Parent property of the UserControl. If you need to access something not part of the Form class you'll need to cast the property to the Form type, but be warned, this can hinder reusing the control.

        C Offline
        C Offline
        chrisclarke11
        wrote on last edited by
        #3

        Hi Tony Thanks for the help. The Parents propertiy it not something i have come accross before. Looking at a few points of the web and msdn. I've manged to get the name of the control that the user control is on but not access the properties.

        Chris

        T 1 Reply Last reply
        0
        • C chrisclarke11

          Hi Tony Thanks for the help. The Parents propertiy it not something i have come accross before. Looking at a few points of the web and msdn. I've manged to get the name of the control that the user control is on but not access the properties.

          Chris

          T Offline
          T Offline
          TheFoZ
          wrote on last edited by
          #4

          Hi Chris Have you thought about passing the information from the form to the user control via a property. As Tony mentioned, trying to access in the way you are proposing will cause difficulties in reusability. using a property will make it easier to pass information back to the form (if required). Regards

          The FoZ

          C 1 Reply Last reply
          0
          • T TheFoZ

            Hi Chris Have you thought about passing the information from the form to the user control via a property. As Tony mentioned, trying to access in the way you are proposing will cause difficulties in reusability. using a property will make it easier to pass information back to the form (if required). Regards

            The FoZ

            C Offline
            C Offline
            chrisclarke11
            wrote on last edited by
            #5

            Hi TheFoz Yes I no this would be the easiest way but would require some code to be added the form everytime you used the control. What i was trying to do was have have enum on the main form. When the user control is droped on the form for it o read the enum and add it to a property of the user control. This way i could have multiple copies of the user control on the form and one of the properties of the user control would allow me to select from the forms enum.

            Chris

            T 1 Reply Last reply
            0
            • C chrisclarke11

              Hi TheFoz Yes I no this would be the easiest way but would require some code to be added the form everytime you used the control. What i was trying to do was have have enum on the main form. When the user control is droped on the form for it o read the enum and add it to a property of the user control. This way i could have multiple copies of the user control on the form and one of the properties of the user control would allow me to select from the forms enum.

              Chris

              T Offline
              T Offline
              TheFoZ
              wrote on last edited by
              #6

              I see! Using this code in one of my custom controls I changed the colour of the form to alice blue when the user hovered their mouse on it

              ((Form)this.Parent).BackColor = Color.AliceBlue;

              Using that you should be able to access the enum on the form regards

              The FoZ

              C 1 Reply Last reply
              0
              • T TheFoZ

                I see! Using this code in one of my custom controls I changed the colour of the form to alice blue when the user hovered their mouse on it

                ((Form)this.Parent).BackColor = Color.AliceBlue;

                Using that you should be able to access the enum on the form regards

                The FoZ

                C Offline
                C Offline
                chrisclarke11
                wrote on last edited by
                #7

                Hi Thanks for that. That could be very usefull but it only gives you access to standard windows form properties.

                Chris

                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