fgd1030
一个没理想的程序员
采纳率100%
2017-07-13 08:38

winfrom自定义组合控件后台无法更改其属性值

20
  • 控件
已采纳

我创建了一个组合控件,后台无法更改其属性值,请问是什么原因?

//控件代码
public partial class desk : UserControl
{
private string number = "0";
private string name = "桌子";
private string summoney = "金额";
private string bz = "备注";
private string zt = "";
System.Drawing.Color pb1 = System.Drawing.Color.LimeGreen;
System.Drawing.Color lb5bj = System.Drawing.Color.LightGray;
System.Drawing.Color nameforecolor = System.Drawing.Color.Black;
System.Drawing.Color smoncolor = System.Drawing.Color.Red;
public desk()
{
InitializeComponent();
this.NotifyPropertyChanged(Summoney);

}

    public event PropertyChangedEventHandler PropertyChanged;
    protected void NotifyPropertyChanged(string propertyName)
    {
        var propertyChanged = this.PropertyChanged;
        if (propertyChanged != null)
            propertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }

     [Description("属性描述"), Category("属性卡名称"), DefaultValue("属性默认值")]

    public string Roomid
    {
        set { number = value; }
        get { return number; }

    }

    public string Roomname
    {
        get { return name; }
        set { name = value; }
    }

    public string Summoney
    {
        get { return summoney; }
        set { summoney = value; }
    }

    public Color getpb1
    {
        get { return pb1; }
        set { pb1 = value; }
    }

    public Color Lab5bj
    {
        get { return lb5bj; }
        set { lb5bj = value; }
    }

    public string Bz
    {
        get { return bz; }
        set { bz = value; }
    }

    public string Zt
    {
        get { return zt; }
        set { zt = value; }
    }

    public Color SmonColor
    {
        get { return smoncolor; }
        set { smoncolor = value; }
    }

    public Color NameForeColor
    {
        get { return nameforecolor; }
        set { nameforecolor = value; }
    }

    public event EventHandler AX;
    public delegate void select_Handle(object sender, EventArgs e, string orther);
    public event select_Handle Desk_click;      
    private void desk_Load(object sender, EventArgs e)
    {
        SetStyle(ControlStyles.DoubleBuffer | ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true);
        UpdateStyles();
        label4.Text = Roomid;//默认桌号
        label1.Text = Roomname;//默认名称
        label1.ForeColor = NameForeColor;//默认名称背景色
        label2.Text = Summoney;//金额
        label2.ForeColor = SmonColor;//金额背景色
        label3.Text = Bz;//备注
        label5.Text = Zt;//状态
        this.panel1.BackColor = getpb1;//默认顶部颜色
        this.label5.BackColor = Lab5bj;//默认背景颜色
    }

    private void desk_MouseClick(object sender, MouseEventArgs e)
    {
        //if (AX != null)
        //{
        //    AX(this, new EventArgs(),"");
        //}
    }

    private void desk_Click(object sender, EventArgs e)
    {

        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);  
    }

    private void label4_Click(object sender, EventArgs e)
    {

        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);
    }

    private void panel1_Click(object sender, EventArgs e)
    {

        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);
    }

    private void label1_Click(object sender, EventArgs e)
    {

        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);
    }

    private void label5_Click(object sender, EventArgs e)
    {
        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);
    }

    private void label2_Click(object sender, EventArgs e)
    {

        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);
    }

    private void label3_Click(object sender, EventArgs e)
    {
        if (Desk_click != null)
            Desk_click(this, new EventArgs(), this.Name);
    }
}

//前台引用代码
private void Test_Load(object sender, EventArgs e)
{

        this.desk1.Summoney = DateTime.Now.ToString("yyyy-MM-dd");
    }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • csdn_zxw csdn_zxw 4年前

    自定义控件如果和布局中属性关联:
    * 1,在Values下创建attrs文件 在此文件中 添加属性
    * 2,在布局文件中使用添加的属性 (先添加命名空间)
    * 3,在逻辑代码 俩个参数的构造方法中 获取布局中的属性 并且进行刷新数据 最后回收属性

    点赞 评论 复制链接分享
  • fgd1030 一个没理想的程序员 4年前

    刚接触winfrom,估计是控件处的Load()事件的原因。因为控件加载后,属性值固定了。我暂时的结局办法是把private改成了public,然后在改变的属性
    之后,再次用了load(),暂时解决了问题。不知大神有没有更好的办法。有好办法了就结帖

    点赞 评论 复制链接分享