私はこの質問に対する答えをグーグルで検索しましたが、これの良い実例を見つけることができないようです. 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 です。Rating
myプロパティを myに設定StarControl
すると、その数の星が表示されます。私がやろうとしているのは、自分のStarControl
ユーザー コントロールを に表示させる方法を見つけることDataGridView
です。誰かがこれがどのように行われるかの例を教えてもらえますか?
この件に関するこのMSDNの記事を見たことがありますが、これは私には当てはまらないと思いDataGridViewTextBoxCell
ます.