Results 1 to 7 of 7

Thread: [RESOLVED] Creating a Custom User Control, don't know where to start.

  1. #1

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Resolved [RESOLVED] Creating a Custom User Control, don't know where to start.

    Hey! I figured it out. Check out the finished control here:http://www.vbforums.com/showthread.php?p=4084398

    I'm trying to make myself a simple 'star rating' control.

    It would have the properties:
    StarCount (number of stars)
    StarValue (number of stars highlighted)

    I want to draw the stars using GDI+, have them highlight on mouse over, and change the StarValue when clicked.

    I have an idea on how to do this from experience with working with forms, but I'm having some difficulties with the Control development part. I couldn't find any tutorials which didn't inherit a control (such as a checkbox or button), and I have no idea how to make my control editable in the designer.

    I think what I need to do is create StarCount as a property like this:
    Code:
    Public Property StarCount() As Integer
            Get
                Return stars
            End Get
            Set(ByVal value As Integer)
                stars = value
            End Set
    End Property
    But then, I cannot see or change it in the designer. How would I go about doing that?

    I also want to execute my drawing code at design time, so I can see the stars while laying out a form. as a side note to that, I don't know how to change the size of the control at run or design time, nor how to disable the user from changing it at design time.

    Could anyone please point me in the right direction?

    as a side note - I know that there are a few star rate controls already floating around the internet, but I do not like the quality of them (or at least the one's I've seen), they felt very windows 98. I'm trying to make something that will fit well into windows 7 (plus I want to implement custom images, but that can wait until I get it working)
    Last edited by stepdragon; Oct 27th, 2011 at 01:19 AM.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  2. #2
    PowerPoster i00's Avatar
    Join Date
    Mar 2002
    Location
    1/2 way accross the galaxy.. and then some
    Posts
    2,388

    Re: Creating a Custom User Control, don't know where to start.

    You will need to build before controls show in the designer... controls need to be inherited from something ... what makes a control a control (in .net anyway) is an object that inherits Control

    ...for ease of use why not inherit from panel and paint on that?

    also drawing code is automatically executed @ design time ... this is how a control works.

    Kris

  3. #3

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Creating a Custom User Control, don't know where to start.

    alright, I've figured out most of what I need (so far). One thing I don't know how to do is this. I want to highlight a single star when the mouse is over it. I can do that easily with the mousemove event, and some simple calculations, the problem is getting the star to hide when the mouse is not over the control. How would I accomplish this?

    I tried the MouseEnter, and MouseLeave events, but the MouseLeave event is never raised. I also tried using a timer to check if it was over, but I don't know what to check. I tried cursor.position, but that detects the cursor in regards to the screen, not the control. Also, I don't like the idea of using a timer for that. I know there's a simple way to do this, that I'm missing. I just can't find it.

    what would be the proper way of detecting this?


    - Never mind, I wasn't refreshing my graphics. I feel stupid now.

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  4. #4

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Creating a Custom User Control, don't know where to start.

    Ok, I have my control nearly complete, except one thing...

    How do I create a custom event?

    My control inherits Panel, like you suggested.
    I'm drawing on it using GDI+ in the on paint event
    I have it change an index variable / property when it is clicked

    Now I want to create a custom on index changed event, so that I can have some slightly better use of my control in a program.

    any thoughts?

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  5. #5

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Creating a Custom User Control, don't know where to start.

    Well, I have to say I'm learning a lot today. I'm also answering my own questions. but for the sake of the forum and anyone who stumbles upon this thread in the future, here's how I did it:

    Code:
    Public Event ValueChanged(ByVal Value As Integer)
    
        Public Property Value() As Integer
            Get
                Return StarValue
            End Get
            Set(ByVal value As Integer)
                If value > StarCount Then
                    StarValue = StarCount
                Else
                    StarValue = value
                End If
                RaiseEvent ValueChanged(StarValue)
            End Set
        End Property

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  6. #6

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Creating a Custom User Control, don't know where to start.

    Alright, I'm basically done. However, I can't figure out how to change the value via code at runtime.

    I'd also like someone to look over the control while I'm at it, in case they see anything that is poorly written, or if they have errors that may help me.

    when I'm finished with it, I want to submit it to the codebank, but because its my first usercontrol (in .net) I'm hoping somebody would look over it for me. (its not very large, no worries)

    vb.net Code:
    1. Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
    2.         ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    3.      'This is what I have a problem with. I can't set the StarCount at runtime.
    4.      'Is it possible? how can I fix it.  
    5.      UserControl1.StarCount = NumericUpDown1.Value
    6.     End Sub
    Attached Files Attached Files

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

  7. #7

    Thread Starter
    Hyperactive Member stepdragon's Avatar
    Join Date
    Aug 2011
    Location
    Cincinnati
    Posts
    288

    Re: Creating a Custom User Control, don't know where to start.

    Well guys, at least I didn't figure it out myself this time. My brother pointed it out to me...

    Code:
    Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, _
            ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
         'This is what I have a problem with. I can't set the StarCount at runtime.
         'Is it possible? how can I fix it.  
         UserControl1.StarCount = NumericUpDown1.Value
        End Sub 
    
    UserControl1 is the default name (I hadn't changed it yet)
    therefore this instance of the control would be named UserControl11
    
    /Fail

    If you're wrong, you'll learn. If I'm wrong, I'll learn. Try something new and go from there. That's how we improve.

    CodeBank: VB.Net - Simple Proper Image Scaling in Correct Aspect Ratio - Star Rating Control
    Useful Links: HOW TO USE CODE TAGS

Tags for this Thread

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