# VBForums CodeBank > CodeBank - Visual Basic .NET >  Star Rating Control

## stepdragon

*UPDATED* 4/15/2012 - Overrides OnPaint instead of handling paint, so that graphics show up at design time. Thank you minitech for the suggestion!

I found the need for a simple, yet good looking Star Rating control. I haven't made a user control since I was programming in VB5, so this was quite a challenge for me.

 If you use my control, please reply or send me a pm to let me know! I love knowing when I help someone! 
Its here for you to use, no need to contact me if you need it. I just like knowing (it makes me proud)

The control uses GDI+ to draw the stars from images. 
Moving the mouse highlights what stars can be chosen.


The Control Contains 7 Custom Properties:
StarCount as Integer - (Number of stars / Highest possible rating)Value as Integer - (Number of Stars Lit)StarHeight as IntegerStarWidth as IntegerStarGold as Image - (optional Image to override the default Star Image)StarGray as Image - (optional Image to override the default Star Image)DisplayOnly as Boolean - (when True it disables the control from updating on MouseOver or Click)
(StarHeight and StarWidth adjust the visible size of the Stars. That way the control can be used when multiple sizes are needed)
(Both StarGold and StarGray must be valid or neither will be replaced. You can set them to (none) to return to the default stars)

It also Contains one Custom Event:
ValueChanged(Value, UserChanged as Boolean) - Raised whenever the Value is changed
UserChanged was added so you will know if the value was changed by code or by clicking on it. 
That will allow you to change the appearance of the control without messing up numbers in your program.

To use the control, open the project (made in VS2010 express) and build it. Then open your project, right click on the toolbox, and choose "Choose Items...". Under ".Net Framework Components" Navigate to the Bin Folder of the Control Project and add it. You can then use the control like any other.



If you have any problems loading, building, or testing this control, please let me know. Its my first in .net and I want to get it working.

UPDATE: I added a DisplayOnly Property so that you could disable user input
It works well if you just want to show a rating, rather than set one.

I also commented the entire project.

----------


## geniyin

hi. i am using ur code, but y d design is empty?how to use this rating control?

----------


## stepdragon

The code uses GDI+ to draw the stars. That means that if you are in design-mode of the control it will look blank. Are you you using it correctly? What you should first do is open my project, and compile it. That will generate a dll which you can then import into your project by right clicking on the control toolbox and choosing select controls. From there you search for the dll in the dialogue.

Sorry for the short reply I'm using my phone. Let me know if you have any other problems, I will get back to you.

----------


## geniyin

o ya i will try it out thanks. btw, u know how to update its average rating everytime user rate it?

----------


## stepdragon

> o ya i will try it out thanks. btw, u know how to update its average rating everytime user rate it?


How to have it show an average rating every time someone rates it? sure

use the event 'ValueChanged' to detect whenever the user chooses a value, and then add that value to a list you have of all users, do some simple math and then display the result by changing the value manually...

Its been a while since I've looked at the code of my control. If it throws 'ValueChanged' each time you change it by code, then you can simply use a boolean to ignore each time you set it progmatically. I'll add that feature to the control itself soon, because that's a good idea.


Keep in mind though, that using the same control for choosing a rating, and for showing an average, could confuse your users. They might get confused when they click on 5 stars, and then it shows 3 stars (the average of all the ratings). It may be a better idea to use more than one control. One for your rating, and one to display the average (using the DisplayOnly property)

----------


## geniyin

ya that's a good idea. but i m kinda not sure i can work it out anot. haha. thanks anyway.for your time.

----------


## stepdragon

what's hard to work out? Let me know where you're at, and I'll help out.

----------


## geniyin

so v nid to create a database to store each rating for each object right? so to save and get its average from database each time d project is compile?

----------


## stepdragon

or you could simply use a list or array. What are you trying to do in your program?

Make a thread in the VB.Net section to continue with this. The codebank isn't the place to do problem solving. Then come here and post a link to your question so I can follow you, (and so others on this thread will find your answer once its solved)

I also just changed my control so you will know if the value was changed by code or by the user. I'll upload it in a minute.

First post has been updated. I suggest you download the new version because the UserChanged event will help you with what you're doing. Here's an example of how to use the new code:


vb.net Code:
Private Sub StarRate1_ValueChanged(ByVal Value As Integer, ByVal UserChanged As Boolean) _
                                   Handles StarRate1.ValueChanged
        If UserChanged = True Then
            MsgBox("By the User", , "Changed!")
        Else
            MsgBox("By Code", , "Changed!")
        End If
    End Sub

----------


## geniyin

ok thanks anyway. actually i am doing a self order system for a restaurant. so maybe doing food rating for user.

----------


## minitech

If you override OnPaint instead of handling Paint, you can have the GDI+ drawings show up in the designer, too. Anyway, well done! Five stars  :Smilie:

----------


## stepdragon

> If you override OnPaint instead of handling Paint, you can have the GDI+ drawings show up in the designer, too. Anyway, well done! Five stars


I did not know that.......  :EEK!: 

OMFGTY    :Big Grin:  :Thumb: 

Updloading an updated version right now!

----------


## wbollen

Registered just to say thanks. Job well done!  :Big Grin: 
I was just about to add a rating feature in my app and this post popped up in Google. Saved me some work.

Will try to merge dll with my main app using ilmerge unless anybody has an easier solution? I would like to avoid yet another dll to bundle with my app that currently is portable.

----------


## VincentVega

Hi Stepdragon,

first of all I want to thank you for this fantastic Control.
I changed the Control a little bit and added properties to display halfstars and with doubleklick to display a red star with a value of -1 .. all works fine. But now I have a binding problem. 

In design time I bind den Value to a Database. The Stars then display the binded value. When I change the rating with the StarControl nothing is saved (in Dataset)... when i change the value in a binded textbox the new value is saved. I added this to the Control but nothing happens:
<System.ComponentModel.DefaultBindingProperty("Value")> _

What's my failure?

----------


## stepdragon

Can you post your modified control? I'll take a look at it. I know its been a few months, but I never noticed an email notification for this post. sorry.

----------


## VincentVega

Hi Stepdragon,
sorry for the long time to answer. 

Here's a little Code to test the DataGridViewStarRateColumn. Unforunitly it doesn't work the way i like.
StarRateTester.zip

In the Sample the lower DGV is the one with der Starrate Control. Click on a Rating and see the Control.

I like to have a Control to display decimal values als Stars in a Datagridview and want to have the ability to change the Value by clicking on the stars. The only thing that work is when you click on a Rating the Control is displayed...but when you change the value and leave you get an error.

I have no more Idea what i could do. It would be very cool if you'll find a way. I don't want any workaround and want to understand how it coud be made.

Thank you for having a look at this!

----------


## sam4help

Thanks Dear it helped me too  :Smilie:  

God Bless..!!! Keep sharing  :Wink: 

Best Regards,

Sam

----------


## sujeethkv

Hi Stepdragon,

I have been looking for this since some time now. Looks awesome and just what i want. Thank you!!

I am trying to use your code in excel vba [my project restricts me to excel vba only  :Frown:  ], but unfortunately it isnt working on vba. Is there something that i need to take care of before i can use it on vba?

*I could use some help.*

Thanks in advance.

----------


## stepdragon

> Hi Stepdragon,
> 
> I have been looking for this since some time now. Looks awesome and just what i want. Thank you!!
> 
> I am trying to use your code in excel vba [my project restricts me to excel vba only  ], but unfortunately it isnt working on vba. Is there something that i need to take care of before i can use it on vba?
> 
> *I could use some help.*
> 
> Thanks in advance.


I foresee two problems with attempting to use my code with VBA. The first of which is that as per VBForums Guidelines we cannot distribute complied code, and therefore you will need to use an appropriate version of VB.Net to build my code before you can use it in your own app. When using VB.Net this can all be done together while compiling, however VBA doesn't compile to executable code, and therefore cannot create the control to begin with.

The second problem I can think of is that this is a VB.Net control. I haven't used VBA in a while but isn't it based off of classic VB? rather than .Net? This could just be my own misunderstanding because I haven't used VBA in years. But if it isn't based upon VB.Net it may not even be able to load the control in the first place.


tl;dr: I recommend obtaining a version of VB.Net (express version is a free downloaded from Microsoft). Even if you cannot use or even install it on your target machine, you can use another machine to compile the code and create a .dll file. From there you can take that file to your target machine and import it into VBA.

If you have any issues or compatibility problems from here, it would be best to post a question into the appropriate forum where more people will be able to help.

----------


## sujeethkv

5_star_ratings.zip

Thanks stepdragon

was not able to do with the draw api on vba. had to go for the image choice only. Thanks for the help and the code .

----------


## smallwondermale

> so v nid to create a database to store each rating for each object right? so to save and get its average from database each time d project is compile?


Here is another example of using your Star rating Control using  A SELECT CASE


     <code>Private Sub StarRate1_ValueChanged(Value As Integer, UserChanged As Boolean) Handles StarRate1.ValueChanged
        Select Case Value
            Case 0
                TextBox1.Text = ""
            Case 1
                TextBox1.Text = "you choose 1 star"
            Case 2
                TextBox1.Text = "you choose 2 star"
            Case 3
                TextBox1.Text = "you choose 3 star"
            Case 4
                TextBox1.Text = "you choose 4 star"
            Case 5
                TextBox1.Text = "you choose 5 star"
        End Select
    End Sub</code>

----------


## smallwondermale

whoops wrong quote on my last post

Does not Code boxes work on this forum?

----------


## stepdragon

Use brackets instead of Tags:

[code] dim content as string [/code]
makes:


```
 dim content as string
```

<code> dim content as string </code>
makes:
<code> dim content as string </code>

[highlight=vb.net] dim content as string [/highlight]
makes:

vb.net Code:
dim content as string

For more, check out 'how to use code tags' in my sig.


as a side note, I'd love to see some screenshots of how you're using my control. I'm so happy that two years later I can still find it on the front page of the codebank!

----------


## SYNTHESISE

Using your control in my video management system. It looks great!

----------


## muziekdb

Hi, Just letting you know that I'm using your control! Thanx!!

----------


## Pyth007

I saw this and thought it looked interesting so I downloaded it even though I don't currently have a use for it... Just a comment: for making it more consistent with other controls, I'd change your "DisplayOnly" property to "Enabled" instead.

----------


## stepdragon

> I saw this and thought it looked interesting so I downloaded it even though I don't currently have a use for it... Just a comment: for making it more consistent with other controls, I'd change your "DisplayOnly" property to "Enabled" instead.


I like the idea of including an enabled property, however in terms of keeping consistent with other controls, I would think that an enabled property would gray out the entire control. regardless of if stars were selected, not just disable user input. Think of a textbox, it grays out. I may experiment and see what works best and post an update.

Remember, you're completely welcome to edit the code and post your own version in the comments. I know this thread isn't that long, but I'll try to put a link to your version in the top post so it can be found easier. If you choose to do so.

----------


## rhyzw

Hey, how do i send you a pm? I could really use your help on coding if you would be so kind. Thanks.

----------


## stepdragon

> Hey, how do i send you a pm? I could really use your help on coding if you would be so kind. Thanks.


If you're having trouble with a program. Please post in the appropriate forum. If you have a question, odds are someone else does too. By posting in the forum on a well titled thread, you will help others who have similar questions find their answers as well.

----------


## Monster4869

Hey, ummm, I wanna use your control for my first project.... But I don't know even know how to open it..... I'm doing my project on vb6 btw.....

----------


## stepdragon

The control was coded for .Net, and while there is nothing .Net specific in the code (as far as I recall) it would need to be reworked for earlier versions of VB, which I do not intend to do anytime soon.

Perhaps someone in the VB6 forum can help you convert it. Your welcome to do so.

----------


## Legjendat

I've noticed a problem with this. If I select a value, then I cannot go back to selecting 0 stars. I can go to a minimum of 1, but not 0.

----------


## stepdragon

> I've noticed a problem with this. If I select a value, then I cannot go back to selecting 0 stars. I can go to a minimum of 1, but not 0.


It's been a while since I looked at this code. But if I remember correctly, it will reset to zero of you click the currently selected star a second time.

You are welcome to add a context menu to it with a reset option if you think that will be more user friendly for your needs.

----------


## Legjendat

> It's been a while since I looked at this code. But if I remember correctly, it will reset to zero of you click the currently selected star a second time.
> 
> You are welcome to add a context menu to it with a reset option if you think that will be more user friendly for your needs.


Oh wow, hadn't noticed that if you click the selected value again it will reset to 0, that's more than enough for my needs, thanks for the reply, this control will be very helpful for my app once I'm done with it, and again thanks for the reply, I noticed u hadn't logged in since February  :big yellow:

----------


## stepdragon

> Oh wow, hadn't noticed that if you click the selected value again it will reset to 0, that's more than enough for my needs, thanks for the reply, this control will be very helpful for my app once I'm done with it, and again thanks for the reply, I noticed u hadn't logged in since February


I subscribe to all my threads  :Big Grin: 

Even 5 years later

----------


## Legjendat

An honorable choice  :Big Grin:  
Good job on the control btw, nice and simple, and it has all the options one needs.

----------

