2

私はこの質問に対する答えをグーグルで検索しましたが、これの良い実例を見つけることができないようです. StarControl というカスタムの星評価ユーザー コントロールを作成しました。コントロールは基本的に、互いに水平方向に隣接する 5 つの画像ボックスであり、次のコードがあります。

public partial class StarControl : UserControl
{
    private enum StarTypes
    {
        Hollow,
        Filled
    }

    private readonly StarTypes[] _stars;
    private int _rating;

    public StarControl()
    {
        InitializeComponent();
        Locked = false;
        _stars = new StarTypes[5];
        _stars[0] = StarTypes.Hollow;
        _stars[1] = StarTypes.Hollow;
        _stars[2] = StarTypes.Hollow;
        _stars[3] = StarTypes.Hollow;
        _stars[4] = StarTypes.Hollow;
        Rating = 0;
        SetStars();
    }

    public bool Locked
    {
        get;
        set;
    }

    public int Rating
    {
        get { return _rating; }
        set { _rating = value; SetRating(); }
    }

    private void SetRating()
    {
        if (_rating == 0)
        {
            _stars[0] = StarTypes.Hollow;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;
        }
        if (_rating == 1)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;
        }
        if (_rating == 2)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;
        }
        if (_rating == 3)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Filled;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;
        }
        if (_rating == 4)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Filled;
            _stars[3] = StarTypes.Filled;
            _stars[4] = StarTypes.Hollow;
        }
        if (_rating == 5)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Filled;
            _stars[3] = StarTypes.Filled;
            _stars[4] = StarTypes.Filled;
        }
        SetStars();
    }

    private void SetStars()
    {
        pbStar1.Image = _stars[0] == StarTypes.Hollow
            ? Properties.Resources.star_hollow
            : Properties.Resources.star_filled;

        pbStar2.Image = _stars[1] == StarTypes.Hollow
            ? Properties.Resources.star_hollow
            : Properties.Resources.star_filled;

        pbStar3.Image = _stars[2] == StarTypes.Hollow
            ? Properties.Resources.star_hollow
            : Properties.Resources.star_filled;

        pbStar4.Image = _stars[3] == StarTypes.Hollow
            ? Properties.Resources.star_hollow
            : Properties.Resources.star_filled;

        pbStar5.Image = _stars[4] == StarTypes.Hollow
            ? Properties.Resources.star_hollow
            : Properties.Resources.star_filled;
    }

    private void PbStar1MouseEnter(object sender, EventArgs e)
    {
        if (!Locked)
        {
            pbStar1.Image = Properties.Resources.star_filled;
            pbStar2.Image = Properties.Resources.star_hollow;
            pbStar3.Image = Properties.Resources.star_hollow;
            pbStar4.Image = Properties.Resources.star_hollow;
            pbStar5.Image = Properties.Resources.star_hollow;
        }
    }

    private void PbStar1MouseLeave(object sender, EventArgs e)
    {
        if (!Locked)
        {
            SetStars();
        }
    }

    private void PbStar2MouseEnter(object sender, EventArgs e)
    {
        if (!Locked)
        {
            pbStar1.Image = Properties.Resources.star_filled;
            pbStar2.Image = Properties.Resources.star_filled;
            pbStar3.Image = Properties.Resources.star_hollow;
            pbStar4.Image = Properties.Resources.star_hollow;
            pbStar5.Image = Properties.Resources.star_hollow;
        }
    }

    private void PbStar2MouseLeave(object sender, EventArgs e)
    {
        if (!Locked)
        {
            SetStars();
        }
    }

    private void PbStar3MouseEnter(object sender, EventArgs e)
    {
        if (!Locked)
        {
            pbStar1.Image = Properties.Resources.star_filled;
            pbStar2.Image = Properties.Resources.star_filled;
            pbStar3.Image = Properties.Resources.star_filled;
            pbStar4.Image = Properties.Resources.star_hollow;
            pbStar5.Image = Properties.Resources.star_hollow;
        }
    }

    private void PbStar3MouseLeave(object sender, EventArgs e)
    {
        if (!Locked)
        {
            SetStars();
        }
    }

    private void PbStar4MouseEnter(object sender, EventArgs e)
    {
        if (!Locked)
        {
            pbStar1.Image = Properties.Resources.star_filled;
            pbStar2.Image = Properties.Resources.star_filled;
            pbStar3.Image = Properties.Resources.star_filled;
            pbStar4.Image = Properties.Resources.star_filled;
            pbStar5.Image = Properties.Resources.star_hollow;
        }
    }

    private void PbStar4MouseLeave(object sender, EventArgs e)
    {
        if (!Locked)
        {
            SetStars();
        }
    }

    private void PbStar5MouseEnter(object sender, EventArgs e)
    {
        if (!Locked)
        {
            pbStar1.Image = Properties.Resources.star_filled;
            pbStar2.Image = Properties.Resources.star_filled;
            pbStar3.Image = Properties.Resources.star_filled;
            pbStar4.Image = Properties.Resources.star_filled;
            pbStar5.Image = Properties.Resources.star_filled;
        }
    }

    private void PbStar5MouseLeave(object sender, EventArgs e)
    {
        if (!Locked)
        {
            SetStars();
        }
    }

    private void PbStar1MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && !Locked)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 1;
        }
        if (e.Button == MouseButtons.Right && !Locked)
        {
            _stars[0] = StarTypes.Hollow;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 0;
        }
        SetStars();
    }

    private void PbStar2MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && !Locked)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 2;
        }
        if (e.Button == MouseButtons.Right && !Locked)
        {
            _stars[0] = StarTypes.Hollow;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 0;
        }
        SetStars();
    }

    private void PbStar3MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && !Locked)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Filled;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 3;
        }
        if (e.Button == MouseButtons.Right && !Locked)
        {
            _stars[0] = StarTypes.Hollow;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 0;
        }
        SetStars();
    }

    private void PbStar4MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && !Locked)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Filled;
            _stars[3] = StarTypes.Filled;
            _stars[4] = StarTypes.Hollow;

            Rating = 4;
        }
        if (e.Button == MouseButtons.Right && !Locked)
        {
            _stars[0] = StarTypes.Hollow;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 0;
        }
        SetStars();
    }

    private void PbStar5MouseClick(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left && !Locked)
        {
            _stars[0] = StarTypes.Filled;
            _stars[1] = StarTypes.Filled;
            _stars[2] = StarTypes.Filled;
            _stars[3] = StarTypes.Filled;
            _stars[4] = StarTypes.Filled;

            Rating = 5;
        }
        if (e.Button == MouseButtons.Right && !Locked)
        {
            _stars[0] = StarTypes.Hollow;
            _stars[1] = StarTypes.Hollow;
            _stars[2] = StarTypes.Hollow;
            _stars[3] = StarTypes.Hollow;
            _stars[4] = StarTypes.Hollow;

            Rating = 0;
        }
        SetStars();
    }
}

コントロールは問題なく動作します。私のフォームには DataGridView コントロールがあり、DataGridView にコレクションの行を動的に入力しようとしています。コレクションは、単にこのクラスのコレクションです。

[Serializable]
public class Rating
{
    public string VendorName { get; set; }
    public int VendorRating { get; set; }
}

public List<Rating> _myRatings;

VendorNameは単なる文字列でありVendorRating、0 ~ 5 の数値を表す int です。Ratingmyプロパティを myに設定StarControlすると、その数の星が表示されます。私がやろうとしているのは、自分のStarControlユーザー コントロールを に表示させる方法を見つけることDataGridViewです。誰かがこれがどのように行われるかの例を教えてもらえますか?

この件に関するこのMSDNの記事を見たことがありますが、これは私には当てはまらないと思いDataGridViewTextBoxCellます.

4

2 に答える 2

2

データグリッドビューで星の評価を表示する最良の方法は、ユニコード文字を使用します

private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
    {
        if (e.ColumnIndex == dataGridView1.Columns["Rating"].Index
          && e.Value != null)
        {
            switch (e.Value.ToString())
            {
                case "1":
                    e.CellStyle.SelectionForeColor = Color.Red;
                    e.CellStyle.ForeColor = Color.Red;
                    //e.CellStyle.Font
                    e.Value = (char)9733;
                    break;
                case "2":
                    e.CellStyle.SelectionForeColor = Color.Brown;
                    e.CellStyle.ForeColor = Color.Yellow;
                    e.Value = (char)9733;
                    break;
                case "3":
                    e.CellStyle.SelectionForeColor = Color.Green;
                    e.CellStyle.ForeColor = Color.Green;
                    e.Value = (char)9733;
                    break;
                case "4":
                    e.CellStyle.SelectionForeColor = Color.Blue;
                    e.CellStyle.ForeColor = Color.Blue;
                    e.Value = (char)9733;
                    break;
                case "5":
                    e.CellStyle.SelectionForeColor = Color.Gold;
                    e.CellStyle.ForeColor = Color.Gold;
                    e.Value = (char)9733;
                    break;
            }
        }
    }
于 2016-03-02T07:06:49.760 に答える