/// <summary> /// This raises the <see cref="MapRatingToValue"/> event /// </summary> /// <param name="e">The event arguments</param> protected internal virtual void OnMapRatingToValue(MapRatingEventArgs e) { var handler = MapRatingToValue; if (handler != null) { handler(this, e); } }
/// <summary> /// This is overridden to update the cell value with a new rating value when it is clicked /// </summary> /// <param name="e">The event arguments</param> protected override void OnContentClick(DataGridViewCellEventArgs e) { RatingColumn owner = base.OwningColumn as RatingColumn; int rating; object newValue = this.NewValue; if (keyRating != -1) { rating = keyRating; } else if (this.MouseRating != -1) { rating = this.MouseRating + 1; } else { rating = -1; } if (!base.ReadOnly && base.DataGridView != null && owner != null && rating != -1 && rating <= owner.MaximumRating) { // Let the user map the rating to a cell value MapRatingEventArgs mapArgs = new MapRatingEventArgs(e.ColumnIndex, e.RowIndex, rating, rating); owner.OnMapRatingToValue(mapArgs); // If the value changed, use that. If the value didn't change, use the new index value but // convert it to the matching type. if (mapArgs.Value == null || !mapArgs.Value.Equals(rating)) { this.NewValue = mapArgs.Value; } else if (newValue is int) { this.NewValue = rating; } else if (newValue is short) { this.NewValue = (short)rating; } else { this.NewValue = mapArgs.Value; } base.DataGridView.NotifyCurrentCellDirty(true); base.DataGridView.InvalidateCell(this); } }
/// <summary> /// This is handled to raise the <see cref="DataGridView.CellClick"/> and /// <see cref="DataGridView.CellContentClick"/> events when one of the digit keys 0 thorough 9 /// or + or - is released. /// </summary> /// <param name="e">The event arguments</param> /// <param name="rowIndex">The index of the row containing the cell</param> /// <remarks>Digits 0 through 9 can be used to set a rating in that range. The plus (+) and minus (-) /// keys can be used to adjust the current rating up or down by one.</remarks> protected override void OnKeyUp(KeyEventArgs e, int rowIndex) { object cellValue; if (base.DataGridView != null && ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || e.KeyCode == Keys.Add || e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Oemplus || e.KeyCode == Keys.OemMinus) && !e.Alt && !e.Control && (!e.Shift || (e.Shift && e.KeyCode == Keys.Oemplus))) { if (e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) { keyRating = (int)(e.KeyCode - Keys.D0); } else { if (base.IsInEditMode) { cellValue = this.NewValue; } else { cellValue = base.GetValue(rowIndex); } if (cellValue is int) { keyRating = (int)cellValue; } else if (cellValue is short) { keyRating = (short)cellValue; } // Let the user map the value to a rating MapRatingEventArgs mapArgs = new MapRatingEventArgs(base.ColumnIndex, rowIndex, cellValue, keyRating); ((RatingColumn)base.OwningColumn).OnMapValueToRating(mapArgs); if (e.KeyCode == Keys.Add || e.KeyCode == Keys.Oemplus) { keyRating = mapArgs.Rating + 1; } else { keyRating = mapArgs.Rating - 1; } } DataGridViewCellEventArgs args = new DataGridViewCellEventArgs(base.ColumnIndex, rowIndex); base.RaiseCellClick(args); if (base.ColumnIndex < base.DataGridView.Columns.Count && rowIndex < base.DataGridView.Rows.Count) { base.RaiseCellContentClick(args); } e.Handled = true; keyRating = -1; } }
/// <summary> /// This raises the <see cref="MapValueToRating"/> event /// </summary> /// <param name="e">The event arguments</param> protected internal virtual void OnMapValueToRating(MapRatingEventArgs e) { var handler = MapValueToRating; if(handler != null) handler(this, e); }
/// <summary> /// This is used to draw the cell image on behalf of the cell /// </summary> /// <param name="value">The current cell value</param> /// <param name="rowIndex">The cell's row index</param> /// <param name="mouseIndex">The index under the mouse or -1 if the mouse isn't over an index</param> /// <returns>The image to display or null if there is no image</returns> protected internal Image DrawImage(object value, int rowIndex, int mouseIndex) { ImageList imageList = this.ImageListInternal; int idx, imageIdx, x = 0, cellValue = 0, width = imageList.ImageSize.Width; // Create the cell bitmap on first use if(cellImage == null) cellImage = new Bitmap(width * maxRating, imageList.ImageSize.Height); if(value is int) cellValue = (int)value; else if(value is short) cellValue = (short)value; // Let the user map the value to a rating MapRatingEventArgs mapArgs = new MapRatingEventArgs(base.Index, rowIndex, value, cellValue); this.OnMapValueToRating(mapArgs); cellValue = mapArgs.Rating; if(cellValue < 0) cellValue = 0; else if(cellValue > maxRating) cellValue = maxRating; using(Graphics g = Graphics.FromImage(cellImage)) { // Draw each image using the appropriate state for(idx = 0; idx < maxRating; idx++, x += width) { if(mouseIndex != -1) { if(idx <= mouseIndex) imageIdx = 2; // Hot else imageIdx = 0; // Empty } else if(idx < cellValue) imageIdx = 1; // Filled else imageIdx = 0; // Empty g.DrawImage(imageList.Images[imageIdx], x, 0); } } return cellImage; }
/// <summary> /// This is handled to raise the <see cref="DataGridView.CellClick"/> and /// <see cref="DataGridView.CellContentClick"/> events when one of the digit keys 0 thorough 9 /// or + or - is released. /// </summary> /// <param name="e">The event arguments</param> /// <param name="rowIndex">The index of the row containing the cell</param> /// <remarks>Digits 0 through 9 can be used to set a rating in that range. The plus (+) and minus (-) /// keys can be used to adjust the current rating up or down by one.</remarks> protected override void OnKeyUp(KeyEventArgs e, int rowIndex) { object cellValue; if(base.DataGridView != null && ((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || e.KeyCode == Keys.Add || e.KeyCode == Keys.Subtract || e.KeyCode == Keys.Oemplus || e.KeyCode == Keys.OemMinus) && !e.Alt && !e.Control && (!e.Shift || (e.Shift && e.KeyCode == Keys.Oemplus))) { if(e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) keyRating = (int)(e.KeyCode - Keys.D0); else { if(base.IsInEditMode) cellValue = this.NewValue; else cellValue = base.GetValue(rowIndex); if(cellValue is int) keyRating = (int)cellValue; else if(cellValue is short) keyRating = (short)cellValue; // Let the user map the value to a rating MapRatingEventArgs mapArgs = new MapRatingEventArgs(base.ColumnIndex, rowIndex, cellValue, keyRating); ((RatingColumn)base.OwningColumn).OnMapValueToRating(mapArgs); if(e.KeyCode == Keys.Add || e.KeyCode == Keys.Oemplus) keyRating = mapArgs.Rating + 1; else keyRating = mapArgs.Rating - 1; } DataGridViewCellEventArgs args = new DataGridViewCellEventArgs(base.ColumnIndex, rowIndex); base.RaiseCellClick(args); if(base.ColumnIndex < base.DataGridView.Columns.Count && rowIndex < base.DataGridView.Rows.Count) base.RaiseCellContentClick(args); e.Handled = true; keyRating = -1; } }
/// <summary> /// This is overridden to update the cell value with a new rating value when it is clicked /// </summary> /// <param name="e">The event arguments</param> protected override void OnContentClick(DataGridViewCellEventArgs e) { RatingColumn owner = base.OwningColumn as RatingColumn; int rating; object newValue = this.NewValue; if(keyRating != -1) rating = keyRating; else if(this.MouseRating != -1) rating = this.MouseRating + 1; else rating = -1; if(!base.ReadOnly && base.DataGridView != null && owner != null && rating != -1 && rating <= owner.MaximumRating) { // Let the user map the rating to a cell value MapRatingEventArgs mapArgs = new MapRatingEventArgs(e.ColumnIndex, e.RowIndex, rating, rating); owner.OnMapRatingToValue(mapArgs); // If the value changed, use that. If the value didn't change, use the new index value but // convert it to the matching type. if(mapArgs.Value == null || !mapArgs.Value.Equals(rating)) this.NewValue = mapArgs.Value; else if(newValue is int) this.NewValue = rating; else if(newValue is short) this.NewValue = (short)rating; else this.NewValue = mapArgs.Value; base.DataGridView.NotifyCurrentCellDirty(true); base.DataGridView.InvalidateCell(this); } }
/// <summary> /// This is used to draw the cell image on behalf of the cell /// </summary> /// <param name="value">The current cell value</param> /// <param name="rowIndex">The cell's row index</param> /// <param name="mouseIndex">The index under the mouse or -1 if the mouse isn't over an index</param> /// <returns>The image to display or null if there is no image</returns> protected internal Image DrawImage(object value, int rowIndex, int mouseIndex) { ImageList imageList = this.ImageListInternal; int idx, imageIdx, x = 0, cellValue = 0, width = imageList.ImageSize.Width; // Create the cell bitmap on first use if (cellImage == null) { cellImage = new Bitmap(width * maxRating, imageList.ImageSize.Height); } if (value is int) { cellValue = (int)value; } else if (value is short) { cellValue = (short)value; } // Let the user map the value to a rating MapRatingEventArgs mapArgs = new MapRatingEventArgs(base.Index, rowIndex, value, cellValue); this.OnMapValueToRating(mapArgs); cellValue = mapArgs.Rating; if (cellValue < 0) { cellValue = 0; } else if (cellValue > maxRating) { cellValue = maxRating; } using (Graphics g = Graphics.FromImage(cellImage)) { // Draw each image using the appropriate state for (idx = 0; idx < maxRating; idx++, x += width) { if (mouseIndex != -1) { if (idx <= mouseIndex) { imageIdx = 2; // Hot } else { imageIdx = 0; // Empty } } else if (idx < cellValue) { imageIdx = 1; // Filled } else { imageIdx = 0; // Empty } g.DrawImage(imageList.Images[imageIdx], x, 0); } } return(cellImage); }