日期:2014-05-18  浏览次数:20808 次

C# WINFORM用什么方法可对自定义的按钮的自定义属性赋值
C# WINFORM 我有多个自定义的按钮里面自定义了几个属性我怎么才能用循环来赋值呢?
按钮名为bArea1,bArea2.......共10个
每个按钮里多添加了fid,fdes,fcount三个属性
我想用类似
for (int i=1;i<=bArea1.fcount;i++)
{

}

标准的属性我可以用
for (int i=1;i<=bArea1.fcount;i++)
{
this.Controls[string.Format("button{0}", i)].text =MyDataSet.Tables[0].Rows[i-1]["fdes"].ToString();
}

定义按钮的代码为:
public partial class bArea : System.Windows.Forms.CheckBox
  {
  public bArea()
  : base()
  {
  this.Appearance = System.Windows.Forms.Appearance.Button;
  this.Size =new System.Drawing.Size(90, 40);
  this.AutoSize = false;
  this.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
  this.Text = "";
  }

  /// <summary
  /// 数据库中的字段名
  /// </summary>
  public string fid {get;set;}
  public string fdes { get; set; }
  public string fcount { get; set; }
  }


------解决方案--------------------
强制类型转换
((bArea)this.Controls[string.Format("bArea{0}", i)]).fid
------解决方案--------------------
你可以把10个按钮放到容器控件,比如panel

foreach (Control control in panel1.Controls)
{
if (control is MyButton)
{
MyButton button = (MyButton)control;
button.fid = "";
button.fdes = "";
}
}