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. Inheriting System.Windows.Forms.Button

Inheriting System.Windows.Forms.Button

Scheduled Pinned Locked Moved C#
graphicsquestiondesignoophelp
6 Posts 4 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.
  • S Offline
    S Offline
    Shy Agam
    wrote on last edited by
    #1

    Hey all, As mentioned in one of my earlier posts, I'm currently coding a class which inherits from System.Windows.Forms.Button. My goal is to create a ShapedButton class, with a built in functionality to shape the button according to a drawing in a Bitmap object. I do not intend to write a new UserControl, encapsulating a button. My intension is to directly inherit the Button class. However, the problem is that it would be immpossible to design my ShapedButton in design view, and I would have to manually write the code to InitializeComponent(). How can I use my ShapedButton in the designer? Do I have to create a UserControl? If that's the case, then how can I inherit Button? There's no multi-inheritance, and I would have to inherit UserControl first... Thanks in advance, Shy.

    C 1 Reply Last reply
    0
    • S Shy Agam

      Hey all, As mentioned in one of my earlier posts, I'm currently coding a class which inherits from System.Windows.Forms.Button. My goal is to create a ShapedButton class, with a built in functionality to shape the button according to a drawing in a Bitmap object. I do not intend to write a new UserControl, encapsulating a button. My intension is to directly inherit the Button class. However, the problem is that it would be immpossible to design my ShapedButton in design view, and I would have to manually write the code to InitializeComponent(). How can I use my ShapedButton in the designer? Do I have to create a UserControl? If that's the case, then how can I inherit Button? There's no multi-inheritance, and I would have to inherit UserControl first... Thanks in advance, Shy.

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      shyagam wrote:

      How can I use my ShapedButton in the designer?

      Do NOT write any code in InitialiseComponent, the IDE recreates that code.

      shyagam wrote:

      If that's the case, then how can I inherit Button?

      You can't, as you say, once you inherit from UserControl, that's it, excepting for interfaces.

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      S 1 Reply Last reply
      0
      • C Christian Graus

        shyagam wrote:

        How can I use my ShapedButton in the designer?

        Do NOT write any code in InitialiseComponent, the IDE recreates that code.

        shyagam wrote:

        If that's the case, then how can I inherit Button?

        You can't, as you say, once you inherit from UserControl, that's it, excepting for interfaces.

        Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

        S Offline
        S Offline
        Shy Agam
        wrote on last edited by
        #3

        Christian Graus wrote:

        Do NOT write any code in InitialiseComponent, the IDE recreates that code.

        What can I do than in order to gain the ability to design the button in the designer?

        3 1 Reply Last reply
        0
        • S Shy Agam

          Christian Graus wrote:

          Do NOT write any code in InitialiseComponent, the IDE recreates that code.

          What can I do than in order to gain the ability to design the button in the designer?

          3 Offline
          3 Offline
          3Dizard
          wrote on last edited by
          #4

          It's not exactly what you wan't, but it might help you out a little bit because it enables you to use at least some designer abilities. To outwit the designer do the following: Let's say you have a class MyButton derived from Button. To place an instance, first place a usual Button Control on your form by using the designer (let's name it myButton1). In your code replace the designer generated code private System.Windows.Forms.Button myButton1; with private MyButton myButton1; and in InitializeComponent() use your own class, which results in replacing this.myButton1 = new System.Windows.Forms.Button(); with this.myButton1 = new MyButton();. This should make the designer switch to your own data type permanently and your own properties and events should also be listed by the designer. Hope this helps.

          S 1 Reply Last reply
          0
          • 3 3Dizard

            It's not exactly what you wan't, but it might help you out a little bit because it enables you to use at least some designer abilities. To outwit the designer do the following: Let's say you have a class MyButton derived from Button. To place an instance, first place a usual Button Control on your form by using the designer (let's name it myButton1). In your code replace the designer generated code private System.Windows.Forms.Button myButton1; with private MyButton myButton1; and in InitializeComponent() use your own class, which results in replacing this.myButton1 = new System.Windows.Forms.Button(); with this.myButton1 = new MyButton();. This should make the designer switch to your own data type permanently and your own properties and events should also be listed by the designer. Hope this helps.

            S Offline
            S Offline
            Shy Agam
            wrote on last edited by
            #5

            Well... I didn't try your suggestion, so I can't tell you if it works... :) I have found my truly beautyful solution (in my opinion at least ;P). I have discovered that after inheriting System.Windows.Forms.Button, the icon of my file in the solution explorer, changed itself to a component's icon. So I started reading about components, and finally I've discovered my solution: A component is defined as a class which implements the IComponent interface, or inherits from a class which directly/indirectly implements the IComponent interface. So I checked the heirarchy of the Button class, and there it was! Button indirectly implements the IComponent interface. So I kept on reading, and found that with some simple attributes added to your class, your class can have what is called a "Design-Time Support"![^] Thank you all again. I hope this info would help others to achieve similar goals.

            F 1 Reply Last reply
            0
            • S Shy Agam

              Well... I didn't try your suggestion, so I can't tell you if it works... :) I have found my truly beautyful solution (in my opinion at least ;P). I have discovered that after inheriting System.Windows.Forms.Button, the icon of my file in the solution explorer, changed itself to a component's icon. So I started reading about components, and finally I've discovered my solution: A component is defined as a class which implements the IComponent interface, or inherits from a class which directly/indirectly implements the IComponent interface. So I checked the heirarchy of the Button class, and there it was! Button indirectly implements the IComponent interface. So I kept on reading, and found that with some simple attributes added to your class, your class can have what is called a "Design-Time Support"![^] Thank you all again. I hope this info would help others to achieve similar goals.

              F Offline
              F Offline
              FriendOfAsherah
              wrote on last edited by
              #6

              Hi I was just searching for that solution - great !!! My problem was that I could not open the designer after I derived a class from TreeView (I got HTML errors in the designer) X| . Now I added the following property to the derived class: [Designer(typeof(SimpleControl.Design.SimpleDesigner))] then I reopend the form in designer - in voala the garphic was displayed ! Thanks a lot for that hint!:-D:-D

              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