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. Component Text Property

Component Text Property

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

    I have a custom component that is inheriting from System.Windows.Forms.UserControl. I would like to have the nice and consistent property Text rather than the property Caption. Defining **public string Caption** works fine and I can view the property in design mode as expected. But if I replace the property definition with **public override string Text**, it doesn’t allow me to see the property. What is the nuance that I’m missing? Thanks db

    J H 2 Replies Last reply
    0
    • D dbetting

      I have a custom component that is inheriting from System.Windows.Forms.UserControl. I would like to have the nice and consistent property Text rather than the property Caption. Defining **public string Caption** works fine and I can view the property in design mode as expected. But if I replace the property definition with **public override string Text**, it doesn’t allow me to see the property. What is the nuance that I’m missing? Thanks db

      J Offline
      J Offline
      je_gonzalez
      wrote on last edited by
      #2

      try public new string Text...

      H 1 Reply Last reply
      0
      • J je_gonzalez

        try public new string Text...

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #3

        That's a bad idea. There is no reason to hide the Text property of the parent class and this will screw-up a lot of the implementation if the variables, fields, or properties to not specifically reference the derivative UserControl Type.

        Microsoft MVP, Visual C# My Articles

        J 1 Reply Last reply
        0
        • D dbetting

          I have a custom component that is inheriting from System.Windows.Forms.UserControl. I would like to have the nice and consistent property Text rather than the property Caption. Defining **public string Caption** works fine and I can view the property in design mode as expected. But if I replace the property definition with **public override string Text**, it doesn’t allow me to see the property. What is the nuance that I’m missing? Thanks db

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          It looks like you're declaring a field, not a property. There is a difference. Also, UserControl in .NET 1.1 attributes the override with the BrowsableAttribute and the EditorBrowsableAttribute to merely hide it from designers and source code editors. It's still there and you can still call it. They removed it only from view because UserControls are typically container in which Text make little sense. To show it again, declare your property like so:

          [Browsable(true)]
          [EditorBrowsable(EditorBrowsableState.Always)]
          public override string Text
          {
          get { return base.Text; }
          set { base.Text = value; }
          }

          You'll then see it again in both the designer and source code editor.

          Microsoft MVP, Visual C# My Articles

          D 1 Reply Last reply
          0
          • H Heath Stewart

            That's a bad idea. There is no reason to hide the Text property of the parent class and this will screw-up a lot of the implementation if the variables, fields, or properties to not specifically reference the derivative UserControl Type.

            Microsoft MVP, Visual C# My Articles

            J Offline
            J Offline
            je_gonzalez
            wrote on last edited by
            #5

            And the world will end... Nice to hear from you again!

            H 1 Reply Last reply
            0
            • J je_gonzalez

              And the world will end... Nice to hear from you again!

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              It may. The massive power outage on US's eastern seaboard was caused by a simple bug. The solution is very simple and examining the UserControl class using something like ildasm.exe would resolve the problem. The member is being overridden simply to hide it from designers and code editors. It was still available all along even without having to override it in order to show it again.

              Microsoft MVP, Visual C# My Articles

              J 1 Reply Last reply
              0
              • H Heath Stewart

                It may. The massive power outage on US's eastern seaboard was caused by a simple bug. The solution is very simple and examining the UserControl class using something like ildasm.exe would resolve the problem. The member is being overridden simply to hide it from designers and code editors. It was still available all along even without having to override it in order to show it again.

                Microsoft MVP, Visual C# My Articles

                J Offline
                J Offline
                je_gonzalez
                wrote on last edited by
                #7

                Probably written by a Californian. Heath, I sincerely think that you need to RELAX. It may be the way I type, but you seem to treat things as way too important. There is a radio show here in L.A. (yes, Lala land, home of Arnold the guvernor) where there is a guy who complains about everything and the question that everyone keeps asking him, and I ask you now, is "Who hurt you?". PS: I forgot to admit that the bug in the electrical grid was mine, but being a Californian it was meant to run in the electical grid of Uzbekistan, and make the lights that were left on read out, "HS rules...", damn if I got it wrong again.

                H 1 Reply Last reply
                0
                • J je_gonzalez

                  Probably written by a Californian. Heath, I sincerely think that you need to RELAX. It may be the way I type, but you seem to treat things as way too important. There is a radio show here in L.A. (yes, Lala land, home of Arnold the guvernor) where there is a guy who complains about everything and the question that everyone keeps asking him, and I ask you now, is "Who hurt you?". PS: I forgot to admit that the bug in the electrical grid was mine, but being a Californian it was meant to run in the electical grid of Uzbekistan, and make the lights that were left on read out, "HS rules...", damn if I got it wrong again.

                  H Offline
                  H Offline
                  Heath Stewart
                  wrote on last edited by
                  #8

                  je_gonzalez wrote: Heath, I sincerely think that you need to RELAX I'm not the only one. I wasn't offended by your remark. A lot of things I post in this forum are for posterity in case some n00b ever considers searching this forum first. :laugh: That made me laugh. Oh well, here's to hoping... :beer:

                  Microsoft MVP, Visual C# My Articles

                  J 1 Reply Last reply
                  0
                  • H Heath Stewart

                    je_gonzalez wrote: Heath, I sincerely think that you need to RELAX I'm not the only one. I wasn't offended by your remark. A lot of things I post in this forum are for posterity in case some n00b ever considers searching this forum first. :laugh: That made me laugh. Oh well, here's to hoping... :beer:

                    Microsoft MVP, Visual C# My Articles

                    J Offline
                    J Offline
                    je_gonzalez
                    wrote on last edited by
                    #9

                    Let's see I was taught not to say RTFM to clients anymore two years before you were born. Yeap, things have changed, now is RTFF, as manuals have given way to forums.

                    1 Reply Last reply
                    0
                    • H Heath Stewart

                      It looks like you're declaring a field, not a property. There is a difference. Also, UserControl in .NET 1.1 attributes the override with the BrowsableAttribute and the EditorBrowsableAttribute to merely hide it from designers and source code editors. It's still there and you can still call it. They removed it only from view because UserControls are typically container in which Text make little sense. To show it again, declare your property like so:

                      [Browsable(true)]
                      [EditorBrowsable(EditorBrowsableState.Always)]
                      public override string Text
                      {
                      get { return base.Text; }
                      set { base.Text = value; }
                      }

                      You'll then see it again in both the designer and source code editor.

                      Microsoft MVP, Visual C# My Articles

                      D Offline
                      D Offline
                      dbetting
                      wrote on last edited by
                      #10

                      Thanks. I was only using the Category, Description and Default attributes. Adding the Browsable and EditorBrowsable attributes did the trick. Now that I've got it showing, testing has shown a couple other nuances. 1. Using Default doesn't do anything, it always contains the default object name as the default Text, regardless of the Default attribute. Even setting a line after InitializeComponent(); as Text="";, didn't do anything. 2. I would've been able to live with the Text property not being blank upon dropping the component on the form but not remembering its value just ain't cool. Initially I tried to use base.Text as the storage for the Text value (seemed the appropriate thing to do). Close the form and open it, the Text value is gone. Tried using a private variable instead, got the same result. Tried using _new_ rather than _override_ in both scenarios of base.Text and a private variable. The component still will not remember the value of Text. None of this affected Default either. While answering this, can you tell me if there is a trick to finding info in the Help and/or SDKs? I suspect the answer you provided would've been there but, while there is probably everything I need to know at my finger tips, asking the right question seems to be the issue (thus RTFF seems to be a better solution than RTFM). Unfortunately, due to corporate bankrupcies I no longer have the team with which to collaborate (previously coded in Delphi) and some of the nuances of .Net have been somewhat frustrating to decipher. Thanks again.

                      H 1 Reply Last reply
                      0
                      • D dbetting

                        Thanks. I was only using the Category, Description and Default attributes. Adding the Browsable and EditorBrowsable attributes did the trick. Now that I've got it showing, testing has shown a couple other nuances. 1. Using Default doesn't do anything, it always contains the default object name as the default Text, regardless of the Default attribute. Even setting a line after InitializeComponent(); as Text="";, didn't do anything. 2. I would've been able to live with the Text property not being blank upon dropping the component on the form but not remembering its value just ain't cool. Initially I tried to use base.Text as the storage for the Text value (seemed the appropriate thing to do). Close the form and open it, the Text value is gone. Tried using a private variable instead, got the same result. Tried using _new_ rather than _override_ in both scenarios of base.Text and a private variable. The component still will not remember the value of Text. None of this affected Default either. While answering this, can you tell me if there is a trick to finding info in the Help and/or SDKs? I suspect the answer you provided would've been there but, while there is probably everything I need to know at my finger tips, asking the right question seems to be the issue (thus RTFF seems to be a better solution than RTFM). Unfortunately, due to corporate bankrupcies I no longer have the team with which to collaborate (previously coded in Delphi) and some of the nuances of .Net have been somewhat frustrating to decipher. Thanks again.

                        H Offline
                        H Offline
                        Heath Stewart
                        wrote on last edited by
                        #11

                        These are all design-time attributes. The DefaultAttribute is there to facilitate resetting the property value. Using a method that returns a bool called ShouldSerialize_PropertyName_ also facilitates this goal. Again, though, don't use new. Only use this when you want to hide a member. For instance, if you use new, any code that refers to your class as a Control (where Text is defined as a virtual method) or any other derivative of Control (before your class), then the virtual Text is used. Only when a variable is declared as your Type will your Text be used. Since much of the Windows Forms implementation refers to all child controls as Control types, your Text accessors wouldn't be called. To note (since you obviously read the little flame war that shouldn't have been), I don't completely advocate RTFM. RTFM goes well with RTFF, but I'm a strong believer in teaching a man to fish, if you know what I mean. Reading the docs and understanding them (like the mostly consistent naming conventions, how C# relations to .NET and IL relates to the CLR, etc.) is very important, but there's also times when people get stuck - even the best have brain farts now and then. The best thing to do is just skim the class library docs some time. Get to know where everything is. Also, whenever you have a question about a specific class, the help Index is your friend. Just type it in. Don't specify certain properties, though, like ScrollableControl.Text because it doesn't exist as a defined method of ScrollableControl - it's inherited. It will be in the member documentation for ScrollableControl but not in the index. It's just the way their help built system works (and even those like NDoc[^] which I occassionally contribute to and is very popular). Once you get to know where all the classes are roughly and how everything is named, it's not too hard finding what you need if you can't remember or even if you don't know exactly what you're looking for.

                        Microsoft MVP, Visual C#

                        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