TRating

Fuente: source/classes/trating.prg

Inherits from: TControl

TRating is an interactive star-rating control that allows users to select a rating value by clicking on star images. It supports hover preview, click-to-set, half-star display, and configurable maximum values. The control uses three image states (empty, half, full) to render the visual rating.

Key DATA Members

DATATypeDefaultDescription
nImagesNumeric5Number of rating images/stars to display
nCurrentValueNumeric0Current hover/preview value
nSelectedValueNumeric0Last clicked (committed) value
nPercentualNumeric100Maximum value for percentage calculation
bChangedBlockCodeblock executed when rating changes (receives new value)
bSetGetBlockSetGet codeblock for variable binding (read/write)
lBorderLogical.F.Show border around the control
nClrBorderNumericBorder color

Methods

MethodDescription
New( nRow, nCol, oWnd, nWidth, nHeight, nRating, cEmptyImg, cHalfImg, cFullImg, lBorder, nClrBorder, nBackColor, lDesign, nInitialValue, nMaxValue, bChanged, bSetGet )Create a new TRating control
SetValue( x )Set the rating value programmatically. If x is omitted, resets to 0.
Reset()Reset the rating to 0 (calls SetValue() with no argument)
Display()Begin paint, render, end paint
Paint()Render the rating stars based on current and selected values
MouseMove( nRow, nCol, nKeyFlags )Handle hover: update nCurrentValue for visual preview
MouseLeave( nRow, nCol, nKeyFlags )Handle mouse leave: restore display to committed value
LButtonUp( nRow, nCol, nKeyFlags, lTouch )Handle click: commit the hovered value and fire bChanged
Destroy()Destroy the control and free image handles

Example: 5-Star Rating Control

#include "FiveWin.ch"

function Main()

   local oWnd, oRating, nRating := 0

   DEFINE WINDOW oWnd TITLE "Rate this App" SIZE 400, 250

   @ 60, 40 RATING oRating ;
      RATING 5 ;
      EMPTY   "star_empty.bmp" ;
      HALF    "star_half.bmp"  ;
      FULL    "star_full.bmp"  ;
      OF oWnd ;
      MAX 100 ;
      INITIAL 0 ;
      CHANGE ( nRating := oRating:nSelectedValue, ;
               oWnd:SetText( "Rating: " + Str( nRating ) ) )

   @ 140, 40 SAY "Click a star to rate" ;
      SIZE 200, 20 OF oWnd

   @ 180, 40 BUTTON "Reset" SIZE 60, 25 OF oWnd ;
      ACTION oRating:Reset()

   ACTIVATE WINDOW oWnd CENTERED

return nil

Notes

Ver También