📄 ratingmeter.vb
字号:
'
' Created: May 2000 - Richard Anderson (rja@vivid-creations.com)
'
' Changes: Created for build 1626. Updated for 1715+
'
Imports System
Imports System.Web.UI
Imports System.Collections
Namespace WroxControls
Public Class RatingMeter
Inherits Control
Private m_bShowRatingText As Boolean
Private m_lCellWidth As Long
Private m_lCellHeight As Long
Private m_fScore As Double
Private m_lVotes As Long
Private m_lMaxRating As Long
' Initialise defaults to enable simpler page construction (eg. fewer attributes )
Public Sub New()
m_lVotes = 0
m_lMaxRating = 5 ' default max rating is 5
m_lCellWidth = 20
m_lCellHeight = 5
m_bShowRatingText = True
End Sub
' Public accessor to set/get the score so far
Public Property Score As Double
Get
Return m_fScore
End Get
Set
m_fScore = value
End Set
End Property
' Public accessor to set/get the votes so far
Public Property Votes As Long
Get
Return m_lVotes
End Get
Set
m_lVotes = value
End Set
End Property
' Public accessor to set/get the max rating
Public Property MaxRating As Long
Get
Return m_lMaxRating
End Get
Set
m_lMaxRating = value
End Set
End Property
'/ Public accessor to set/get the cell width
Public Property CellWidth As Long
Get
Return m_lCellWidth
End Get
Set
m_lCellWidth = value
End Set
End Property
' Public accessor to set/get the cell height
Public Property CellHeight As Long
Get
Return m_lCellHeight
End Get
Set
m_lCellHeight = value
End Set
End Property
' Public accessor to set/get the show text
Public Property ShowText As Boolean
Get
Return m_bShowRatingText
End Get
Set
m_bShowRatingText = value
End Set
End Property
' Draw the rating meter
Protected Overrides Sub Render(objWriter As HtmlTextWriter)
' If there are no votes, so note vote output
If m_lVotes = 0 Then
NoRating(objWriter)
Return
End If
Dim fAverageRating As Double
fAverageRating = m_fScore / m_lVotes
' Perform sanity checks
If fAverageRating > m_lMaxRating Then
RaiseError(objWriter, "Average rating exceeds maximum defined")
Return
End If
' objWriter.Write("<p>Average vote is " & fAverageRating.ToString() & "</p>")
objWriter.Write("<table border='2' cellpadding='0' cellspacing='0'>")
objWriter.Write("<tr>")
Dim lLoop As Long
For lLoop = 0 To m_lMaxRating
objWriter.Write("<td width=" & m_lCellWidth & " height=" & m_lCellHeight & ">")
If fAverageRating > 0 Then
' Draw a full bar
If fAverageRating > 1 Then
objWriter.Write("<img src='images/reddot.gif' width=" & m_lCellWidth _
& " height=" & m_lCellHeight & " border='0' />")
Else
Dim lWidth As Long
Dim lEmptyWidth As Long
lWidth = m_lCellWidth * fAverageRating
lEmptyWidth = m_lCellWidth - lWidth
If lWidth <> 0 Then
objWriter.Write("<img src='images/reddot.gif' width=" & lWidth _
& " height=" & m_lCellHeight & " border='0' />")
End If
If lEmptyWidth <> 0 Then
objWriter.Write("<img src='images/whitedot.gif' width=" & lEmptyWidth _
& " height=" & m_lCellHeight & " border='0' />")
End If
End If
Else
objWriter.Write("<img src='images/whitedot.gif' width=" & m_lCellWidth _
& " height=" & m_lCellHeight & " border='0' />")
End If
fAverageRating -= 1
objWriter.Write("</td>")
Next
objWriter.Write("</tr>")
objWriter.Write("</table>")
' Show rating text if requested
If m_bShowRatingText = true Then
objWriter.Write("<p>Adventure Works Rating " & m_fScore.ToString() _
& " out of a possible " & m_lMaxRating * m_lVotes & "</p>")
End If
End Sub
' Draw output for no votes
Protected Sub NoRating(objWriter As HtmlTextWriter)
objWriter.Write("<p>No votes for this item yet</p>")
End Sub
' Raise Error
Protected Sub RaiseError(objWriter As HtmlTextWriter, sError As String)
objWriter.Write("<p>Error:" & sError & "</p>")
End Sub
End Class
End Namespace
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -