Results 1 to 11 of 11

Thread: [RESOLVED] [2005] Inheriting the button control and adding?

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Resolved [RESOLVED] [2005] Inheriting the button control and adding?

    Well I was just looking at some examples at other websites, and I would like to create my own button control, one that will have different pictures during mouseover and mousedown etc. Well I was wondering if this is called a usercontrol, or if not what it is really called.

    I think I might be able to find some nice examples, but I wanna know what it is called before hand

  2. #2
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2005] Inheriting the button control and adding?

    You could probably use either, but the button control would probably be easiest, since it will expose more methods than the usercontrol will. As for mouseover/mousedown graphics, this is also fairly easy to implement. You can do something like setting two private booleans to you inherited class something like this:

    VB Code:
    1. Public Class Mybutton
    2.     Inherits Button
    3.     Private mouseIsDown As Boolean
    4.     Private mouseIsHover As Boolean
    5.     Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
    6.         MyBase.OnPaint(pevent)
    7.         If mouseIsDown Then
    8.             MyBase.BackColor = Color.Blue
    9.         ElseIf mouseIsHover Then
    10.             MyBase.BackColor = Color.Red
    11.         Else : MyBase.BackColor = Color.Gray
    12.         End If
    13.     End Sub
    14.     Protected Overrides Sub OnMouseEnter(ByVal eventargs As System.EventArgs)
    15.         MyBase.OnMouseEnter(eventargs)
    16.         mouseIsHover = True
    17.     End Sub
    18.     Protected Overrides Sub OnMouseLeave(ByVal eventargs As System.EventArgs)
    19.         MyBase.OnMouseLeave(eventargs)
    20.         mouseIsHover = False
    21.     End Sub
    22.     Protected Overrides Sub OnMouseDown(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    23.         MyBase.OnMouseDown(mevent)
    24.         mouseIsDown = True
    25.     End Sub
    26.     Protected Overrides Sub OnMouseUp(ByVal mevent As System.Windows.Forms.MouseEventArgs)
    27.         MyBase.OnMouseUp(mevent)
    28.         mouseIsDown = False
    29.     End Sub
    30. End Class

    Almost all of those subs will nearly autogenerate if you type sub On.. and then hit enter.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  3. #3
    Frenzied Member conipto's Avatar
    Join Date
    Jun 2005
    Location
    Chicago
    Posts
    1,175

    Re: [2005] Inheriting the button control and adding?

    Also, usercontrol will require alot more manual sizing and painting than button, if all you want to do is override paint events.

    Bill
    Hate Adobe Acrobat? My Codebank Sumbissions - Easy CodeDom Expression evaluator: (VB / C# ) -- C# Scrolling Text Display

    I Like to code when drunk. Don't say you weren't warned.

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Inheriting the button control and adding?

    Thanks guys, I think I am on my way.

    VB Code:
    1. Option Strict On
    2. Option Explicit On
    3.  
    4. Public Class Form1
    5.  
    6.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    7.         Dim tButton As New MyButton
    8.  
    9.         With tButton
    10.             .On_Nothing = New Bitmap("C:\Documents and Settings\Jason\Desktop\New Folder\NoMouse.jpg")
    11.             .OnMouse_Down = New Bitmap("C:\Documents and Settings\Jason\Desktop\New Folder\MouseDown.jpg")
    12.             .OnMouse_Over = New Bitmap("C:\Documents and Settings\Jason\Desktop\New Folder\MouseOver.jpg")
    13.             .Location = New Point(200, 200)
    14.             .Size = New Size(200, 200)
    15.             .Visible = True
    16.         End With
    17.  
    18.     End Sub
    19. End Class
    20.  
    21. Public Class MyButton
    22.     Inherits Button
    23.     Dim Mouse_None As Bitmap
    24.     Dim Mouse_Over As Bitmap
    25.     Dim Mouse_Down As Bitmap
    26.  
    27.     Public Property On_Nothing() As Bitmap
    28.         Get
    29.             Return Mouse_None
    30.         End Get
    31.         Set(ByVal value As Bitmap)
    32.             Mouse_None = value
    33.         End Set
    34.     End Property
    35.  
    36.     Public Property OnMouse_Over() As Bitmap
    37.         Get
    38.             Return Mouse_Over
    39.         End Get
    40.         Set(ByVal value As Bitmap)
    41.             Mouse_Over = value
    42.         End Set
    43.     End Property
    44.  
    45.     Public Property OnMouse_Down() As Bitmap
    46.         Get
    47.             Return Mouse_Down
    48.         End Get
    49.         Set(ByVal value As Bitmap)
    50.             Mouse_Down = value
    51.         End Set
    52.     End Property
    53. End Class

    However, The button does not show even after I load it. Does anyone know why? Also am I going about this the correct way so far?

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: [2005] Inheriting the button control and adding?

    At no point have you actually added the button to the form:
    VB Code:
    1. Me.Controls.Add(tButton)
    FYI, a user control is a something that inherits the UserControl class. A user control is basically a Panel that you design ahead of time with multiple other controls and then reuse as a group. It also allows you to hide the individual controls from the host and just expose the properties, methods and events you want to as members of the user control itself. When you inherit something like the Button and change its behaviour, or else inherit the Control class and do everything from scratch, that is called a custom control. A user control may contain custom controls, or standard .NET controls or a combination of both.

  6. #6

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Inheriting the button control and adding?

    Once again thankyou! Thats perfect!

    One last question before I close it, what does MyBase do? I read Conipto's post have never seen anything like it before, it is totally different than the solution I came up with.

  7. #7
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: [2005] Inheriting the button control and adding?

    MyBase is like Me but it refers to the base class rather than the current class. If your base class has a method named MyMethod and you've overridden it, Me.MyMethod will call the implementation in the derived class while MyBase.MyMethod will call the implementation in the base class.

    Also, I just noticed that the Express video series (see my sig for a link) contains an episode entitled "Customizing the Button Control". Sound useful?

  8. #8
    I'm about to be a PowerPoster!
    Join Date
    Jan 2005
    Location
    Everywhere
    Posts
    13,651

    Re: [2005] Inheriting the button control and adding?

    MyBase refers to the class the current class derives from. If you need to access a property/method or such that is located in the base class and not overriden by your class you need to qualify it by prefixing with MyBase.

    Edit: Bah, said less in more time

  9. #9

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [2005] Inheriting the button control and adding?

    Quote Originally Posted by jmcilhinney
    MyBase is like Me but it refers to the base class rather than the current class. If your base class has a method named MyMethod and you've overridden it, Me.MyMethod will call the implementation in the derived class while MyBase.MyMethod will call the implementation in the base class.

    Also, I just noticed that the Express video series (see my sig for a link) contains an episode entitled "Customizing the Button Control". Sound useful?
    Not sure I understand it so much..It's probably because I'm pretty new and dont know alot. I guess I'll just pick it up as I go..

    Naw..That doesnt seem useful at all

  10. #10
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,461

    Re: [RESOLVED] [2005] Inheriting the button control and adding?

    Here's a demonstration of how MyBase works:
    VB Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim obj As New ClassB
    5.  
    6.         obj.GiveGreetings()
    7.     End Sub
    8. End Class
    9.  
    10. Public Class ClassA
    11.  
    12.     Protected Overridable Sub SayHello()
    13.         MessageBox.Show("Hello from ClassA")
    14.     End Sub
    15.  
    16. End Class
    17.  
    18. Public Class ClassB
    19.     Inherits ClassA
    20.  
    21.     Protected Overrides Sub SayHello()
    22.         MessageBox.Show("Hello from ClassB")
    23.     End Sub
    24.  
    25.     Public Sub GiveGreetings()
    26.         Me.SayHello()       'Calls ClassB.SayHello()
    27.         MyBase.SayHello()   'Calls ClassA.SayHello()
    28.     End Sub
    29.  
    30. End Class

  11. #11

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: [RESOLVED] [2005] Inheriting the button control and adding?

    Okay that makes sense to me now..Thanks for the time and effor put into helping me out

    Reps for both of you

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