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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Inheriting textbox and setting property values

Inheriting textbox and setting property values

Scheduled Pinned Locked Moved C#
tutorialquestion
5 Posts 2 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.
  • K Offline
    K Offline
    kyledunn
    wrote on last edited by
    #1

    After creating a new class that inherits the System.Windows.Forms.TextBox, in the new class I would like to set value of the default Name (other defaults as well) for the TextBox when a new instance is created by dragging the control from the Toolbox and dropping it on a Form. Any idea how to do this? Kyle

    A 1 Reply Last reply
    0
    • K kyledunn

      After creating a new class that inherits the System.Windows.Forms.TextBox, in the new class I would like to set value of the default Name (other defaults as well) for the TextBox when a new instance is created by dragging the control from the Toolbox and dropping it on a Form. Any idea how to do this? Kyle

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

      simple override the constructor to set the values. if you want it to play nice with the property browser ( making it look there hasn't been a change ), then either override the property declaration with a new DefaultValue attribute for simple types, or for complex types, override or create a new version of each ShouldSerialize method, and return false if it equals your value.

      K 1 Reply Last reply
      0
      • A Andy Smith

        simple override the constructor to set the values. if you want it to play nice with the property browser ( making it look there hasn't been a change ), then either override the property declaration with a new DefaultValue attribute for simple types, or for complex types, override or create a new version of each ShouldSerialize method, and return false if it equals your value.

        K Offline
        K Offline
        kyledunn
        wrote on last edited by
        #3

        Andy, Thank you very much for your help. Yes I do want to play nice with the property browser. Setting the value of properties in the constructor means they are now fixed at that value regardless of changes made in the property browser. I am looking to maintain normal design time behaviour. I just want to override the default name that appears when a control is dropped on a form. I tried to override the property declaration with this code: private string name = ""; [DefaultValue("tbx")] public string Name { get { return name; } set { name = value; } } but got this warning: c:\documents and settings\kyle\my documents\visual studio projects\numericonlytextbox\numericonlytextbox.cs(49,17): warning CS0108: The keyword new is required on 'CustomControl.NumericOnlyTextBox.Name' because it hides inherited member 'System.Windows.Forms.Control.Name' and it did not set the Name value to the default "tbx". I am also very intrigued by the ShouldSerialize method. Do you have any short segments of code you would share that could help me to better understand overriding the inherited property and SouldSerialize method? Thanks again, Kyle

        A 1 Reply Last reply
        0
        • K kyledunn

          Andy, Thank you very much for your help. Yes I do want to play nice with the property browser. Setting the value of properties in the constructor means they are now fixed at that value regardless of changes made in the property browser. I am looking to maintain normal design time behaviour. I just want to override the default name that appears when a control is dropped on a form. I tried to override the property declaration with this code: private string name = ""; [DefaultValue("tbx")] public string Name { get { return name; } set { name = value; } } but got this warning: c:\documents and settings\kyle\my documents\visual studio projects\numericonlytextbox\numericonlytextbox.cs(49,17): warning CS0108: The keyword new is required on 'CustomControl.NumericOnlyTextBox.Name' because it hides inherited member 'System.Windows.Forms.Control.Name' and it did not set the Name value to the default "tbx". I am also very intrigued by the ShouldSerialize method. Do you have any short segments of code you would share that could help me to better understand overriding the inherited property and SouldSerialize method? Thanks again, Kyle

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

          kyledunn wrote: Setting the value of properties in the constructor means they are now fixed at that value regardless of changes made in the property browser.
          This is false. Setting values in the constructor will not do that. you need to set the value.
          kyledunn wrote: and it did not set the Name value to the default "tbx".
          it didn't change it, because you never set it. the DefaultValue attribute is only for the IDE to know when it's been changed. It doesn't actually set the value. You have to set it yourself in the constructor.
          kyledunn wrote: but got this warning:
          that warning is because you need to explicitly say whether it is an "override" or a "new". In this case, since the "Name" property is not virtual, you can only use "new".
          All that said, it is a REALLY BAD IDEA to try to override the Name property. Controls do magic things with the name property to make sure they are unique. Making them all "tbx" would be a BAD THING.

          K 1 Reply Last reply
          0
          • A Andy Smith

            kyledunn wrote: Setting the value of properties in the constructor means they are now fixed at that value regardless of changes made in the property browser.
            This is false. Setting values in the constructor will not do that. you need to set the value.
            kyledunn wrote: and it did not set the Name value to the default "tbx".
            it didn't change it, because you never set it. the DefaultValue attribute is only for the IDE to know when it's been changed. It doesn't actually set the value. You have to set it yourself in the constructor.
            kyledunn wrote: but got this warning:
            that warning is because you need to explicitly say whether it is an "override" or a "new". In this case, since the "Name" property is not virtual, you can only use "new".
            All that said, it is a REALLY BAD IDEA to try to override the Name property. Controls do magic things with the name property to make sure they are unique. Making them all "tbx" would be a BAD THING.

            K Offline
            K Offline
            kyledunn
            wrote on last edited by
            #5

            Hi Andy, I set the value of this.Width to equal 300 in the constructor for the custom textbox control and then changed it to 100 in the property browser. Upon running the form the textbox reverted to a width of 300 which led me to the false statement above. I just tried setting the value to any other number than it's original default and it worked as you described. The original default number was influencing the behavior I was describing. Thanks for the explanation about the DefaultValue. That makes sense. I agree it's a bad idea to change the default name. I was interested in changing the default, not necessarily getting rid of the sequential number, but it was a bad example to choose. Thanks for your help. Kyle

            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