Results 1 to 12 of 12

Thread: [RESOLVED] Button Wrapper Class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Resolved [RESOLVED] Button Wrapper Class

    I have a bunch of buttons in the current application I am working on. The client doesn't like to use a mouse, so I was just asked to set the buttons to change colors upon Enter and Leave. I have a few options here and wanted an opinion as to the easiest way to quickly get this taken care of.

    1 - Create a generic event handler for Enter and Leave and use AddHandler statements on each form for each button. This seems to be the option that would take the longest amount of time to implement.
    2 - Create a User Control with the necessary code and then use a Find and Replace. This seems to be the quickest and easiest.
    3 - I thought I could create a Component that inherits from Button that was more or less just a wrapper around the button control. At design-time, I could use the standard button control and then at run-time I could replace the standard buttons with my Component. However, I have been toying around with this one and haven't been able to get it to work. (If someone has some code for creating the component and implementing it, I would like to see it.)

    Any assistance/opinions will be greatly appreciated.
    My.Settings.Signature = String.Empty

  2. #2
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Button Wrapper Class

    I think number 1 would be easiest
    VB Code:
    1. Dim ACTIVE As Color = Color.Red
    2.     Dim INACTIVE As Color = Me.BackColor
    3.     Private Sub SwapColor_ButtonEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Enter, Button2.Enter
    4.         DirectCast(sender, Button).BackColor = active
    5.     End Sub
    6.     Private Sub SwapColor_ButtonLeave(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Leave, Button2.Leave
    7.         DirectCast(sender, Button).BackColor = INACTIVE
    8.     End Sub

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button Wrapper Class

    You should inherit the Button class:
    VB Code:
    1. Public Class MyButton
    2.     Inherits Button
    3.  
    4.     Private _normalBackColour As Color
    5.     Private _focusedBackColour As Color
    6.  
    7.     <System.ComponentModel.Category("Appearance")> _
    8.     Public Property NormalBackColour() As Color
    9.         Get
    10.             Return Me._normalBackColour
    11.         End Get
    12.         Set(ByVal value As Color)
    13.             Me._normalBackColour = value
    14.  
    15.             If Not Me.Focused Then
    16.                 Me.BackColor = value
    17.             End If
    18.         End Set
    19.     End Property
    20.  
    21.     <System.ComponentModel.Category("Appearance")> _
    22.     Public Property FocusedBackColour() As Color
    23.         Get
    24.             Return Me._focusedBackColour
    25.         End Get
    26.         Set(ByVal value As Color)
    27.             Me._focusedBackColour = value
    28.  
    29.             If Me.Focused Then
    30.                 Me.BackColor = value
    31.             End If
    32.         End Set
    33.     End Property
    34.  
    35.     Public Sub New()
    36.         MyBase.New()
    37.  
    38.         Me._normalBackColour = Me.BackColor
    39.         Me._focusedBackColour = Me.BackColor
    40.     End Sub
    41.  
    42.     Protected Overrides Sub OnEnter(ByVal e As System.EventArgs)
    43.         Me.BackColor = Me._focusedBackColour
    44.         MyBase.OnEnter(e)
    45.     End Sub
    46.  
    47.     Protected Overrides Sub OnLeave(ByVal e As System.EventArgs)
    48.         Me.BackColor = Me._normalBackColour
    49.         MyBase.OnLeave(e)
    50.     End Sub
    51.  
    52. End Class
    You can then add instances of this class to your form from the Toolbox, where it will be automatically placed for the solution in which it is declared, or else you can do a Find and Replace on existing Button's.

    Just a note on terminology. If you inherit a class you are not creating a wrapper. Does a Dog wrap an Animal? No it doesn't. It just specialises the type to a greater degree. Creating a wrapper means defining a type that has an instance of another type inside. The two objects are distinct, with one enclosing, or wrapping, the other. Also, a "user control" is a type that inherits the UserControl class. If you inherit a class like Button, or ideed Control itself, you are creating a "custom control".
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: Button Wrapper Class

    What do these lines do?
    VB Code:
    1. <System.ComponentModel.Category("Appearance")>

  5. #5
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: Button Wrapper Class

    This specifies that the sub/property it preceeds belongs to the "Appearance" category when it is shown in the IDE's Property panel.
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: Button Wrapper Class

    You can then add instances of this class to your form from the Toolbox, where it will be automatically placed for the solution in which it is declared, or else you can do a Find and Replace on existing Button's.
    I am working with 2005 Express. I do not see it added to my Toolbox and do not see how to add it.

    PS - Just to get things straight, you are just creating a class here and not a User Control, correct?
    My.Settings.Signature = String.Empty

  7. #7
    Fanatic Member dom_stapleton's Avatar
    Join Date
    Sep 2005
    Location
    Leigh-on-Sea, UK
    Posts
    665

    Re: Button Wrapper Class

    I would call it an extension of a user control using a class.
    I use Microsoft Visual Basic 2008 Express Edition.

    If my post has been helpful, please rate it, unless you don't believe in Karma... which actually I don't!

    Resources:
    Visual Basic Tutorials (1, 2) | MSDN Library | Google | Krugle | Search Forums

    Free components:
    Windows Forms Components | XP Common Controls Library

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Button Wrapper Class

    Quote Originally Posted by dom_stapleton
    I would call it an extension of a user control using a class.
    I would call it a class.


    inheriting from the button class does nothing different than the standard button class, plus whatever functionality you add to it in the child class.

    A user control follows a different inheritence chain, specifically inheriting from System.Windows.Forms.UserControl, which in the chain goes like this:

    I will start at control, which is the place in the inheritence chain where a control and usercontrol split

    Regular Winform Control (like inheriting from a button)

    System.Windows.Forms.Control
    System.Windows.Forms.ButtonBase
    System.Windows.Forms.Button
    YourCustomButtonClass

    Winform User Control

    System.Windows.Forms.Control
    System.Windows.Forms.ScrollableControl
    System.Windows.Forms.ContainerControl
    System.Windows.Forms.UserControl

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button Wrapper Class

    Quote Originally Posted by Aspnot
    I am working with 2005 Express. I do not see it added to my Toolbox and do not see how to add it.

    PS - Just to get things straight, you are just creating a class here and not a User Control, correct?
    Like I said, it is added automatically for the solution in which it is declared. You have to compile the project that contains it first of course. If you want it available for other solutions then you'll have to add it to the Toolbox manually.
    Attached Images Attached Images  
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: Button Wrapper Class

    I thought that was the way it was supposed to work, but I am missing something.

    Let's go over what you have going on. You have a Solution with two projects (Do they have to be in separate projects?). One project contains the Class the other project contains the form. Both projects are created as "Windows Application"?

    I have tried all kinds of iterations and have not yet seen the "Project Name" Components section get added to my Toolbox.

    I either have the project types incorrect or something like that.
    My.Settings.Signature = String.Empty

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Button Wrapper Class

    If you want the types that a project contains to be usable by other projects then that project has to compile to a DLL. You aren't seeing your button class in the Toolbox because the project containing the form cannot see it. Change the output type of the project containing the button class to DLL and it will appear in the Toolbox when you're working on the project containing the form. Having said that, noone said anything about having to create a second project. You don't create new projects for other classes you define do you, so why this one? If you only intend to use this class in the one application then declare it in that application's project. If you will or may want to use a class in multiple projects then declare it in a library project, which you can then reference from as many other projects as you like.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: Button Wrapper Class

    Ah ha! I tested this out on my home PC and it worked fine. So, like any other inquisitive person, I started comparing the setting on the two machines. Under Tools -> Options -> Windows Forms Designer -> General there is a setting "AutoToolboxPopulate". I had set this to False on my laptop due to my understanding that it was supposed to make the UI faster. It should be set to True.

    Well... When you count the hours I have spent trying to figure this whole thing out, I think that option just cost me far more time than it saved me.

    Thanks to all who posted with assistance and were willing to stick with this!!
    My.Settings.Signature = String.Empty

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width