有一个类
Class Dog
{
    private Lable lbName = new Lable();
    public Lable LbName
    {
        Set{lbName=value;}
        Get{return lbName;}
    }    private Lable lbType=new Lable();
    public Lable LbType
    {
        Set{lbType=value;}
        Get{return lbType;}
    }
}
以下是调用List<Dog> doglist = new List<Dog>();doglist.Add(new Dog());doglist[0].LbName.Text="旺财";doglist[0].LbType.Text="金巴";this.Controls.Add(doglist[0].LbName);this.Controls.Add(doglist[0].LbType);
调用后把Dog这个对像的两个Lable添加到窗体后,想更改Text值,如下
doglist[0].LbName.Text="小黄";doglist[0].LbType.Text="金毛";更改后窗体上的Text不改变,请问有什么方法让他刷新过来吗

解决方案 »

  1.   

    看了一下,好像逻辑没有问题,你set value,界面应该能变化才对,我估计问题不在你给的代码上。
      

  2.   

    你的代码能通过编译?
    是Lable?还是Label?
    Set应该是set吧?
    Get应该是get吧?
      

  3.   


            public Form1()
            {
                InitializeComponent();
                List<Dog> doglist = new List<Dog>();            doglist.Add(new Dog());            doglist[0].LbName.Text = "旺财";            doglist[0].LbType.Text = "金巴";            this.Controls.Add(doglist[0].LbName);
                this.Controls.Add(doglist[0].LbType);            doglist[0].LbName.Text = "小黄";            doglist[0].LbType.Text = "金毛";
            }
    我测试时,在窗体上显示的是“小黄”,也就是说值改变了...
      

  4.   

    你这代码有个漏洞,如果想实现你那个效果,内部的两个控件需要设置为只读,否则就可能出现你那种修改无效的现象,虽然不确定你什么时候将其指向修改了。Class Dog
    {
        private Lable lbName = new Lable();
        public Lable LbName
        {
            Get{return lbName;}
        }    private Lable lbType=new Lable();
        public Lable LbType
        {
            Get{return lbType;}
        }
    }
      

  5.   

    你最好自己调试一下。
    看看你的Label的location和size分别是多少。
      

  6.   

    我觉得dog类的两个属性应该是只读属性比较好,既然你内部new Lable,还允许外部赋值,会混乱的
      

  7.   


    我接着你的说,我觉得最大的问题是Dog属于model层面的内容,是data,是model而Label是显示层面的东西,是view,你不应该让model依赖Label啊
    当然了,我说这些不是本题相关的,只是小的设计的原则问题。
      

  8.   

    new Lable();
    new出来的位置都是同一个地方,被覆盖了,所以你看不到变化?
      

  9.   


    Public Class Dog    Public Property Name As String
        Public Property Type As StringEnd ClassPublic Class Form1    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Dim LbName As New Label With {.Left = 0}
            Dim LbType As New Label With {.Left = 100}        Me.Controls.Add(LbName)
            Me.Controls.Add(LbType)        Dim dogList As New List(Of Tuple(Of Label, Label, Dog))        Dim dog As New Dog
            dog.Name = "旺财"
            dog.Type = "金巴"        dogList.Add(Tuple.Create(LbName, LbType, dog))        dog.Name = "小黄"
            dog.Type = "金毛"        For Each d In dogList
                d.Item1.Text = d.Item3.Name
                d.Item2.Text = d.Item3.Type
            Next    End SubEnd Class
      

  10.   

    因为你始终没有赋值也就是没有set
      

  11.   


    Public Class Form1    Private LbName As New Label With {.Left = 0}
        Private LbType As New Label With {.Left = 100}    Private dogList As New List(Of Tuple(Of Label, Label, Dog))
        Private dog As New Dog    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load        Me.Controls.Add(LbName)
            Me.Controls.Add(LbType)        dogList.Add(Tuple.Create(LbName, LbType, dog))    End Sub    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click        dog.Name = "旺财"
            dog.Type = "金巴"        PaintDog()    End Sub    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click        dog.Name = "小黄"
            dog.Type = "金毛"        PaintDog()    End Sub    Private Sub PaintDog()        For Each d In dogList
                d.Item1.Text = d.Item3.Name
                d.Item2.Text = d.Item3.Type
            Next    End SubEnd ClassPublic Class Dog    Public Property Name As String
        Public Property Type As StringEnd Class
      

  12.   

    List<Dog> doglist既然只有两个标签,而且是你重新给狗赋值,而不是增加,为什么用集合呢?这个意义不明白
      

  13.   

    不好意思了各位看来我连发两贴意思还是没表达清楚.我再来说明下 上面只是我的举例,实际软件的功能是这个样子软件实现温湿度监控和空调联动,有两个类sensor传感器类和controller控制器类两个类下面都有N个属性就如上面的Label一样我在实际的使用中 如下,只是其中一小部分,把传感器根据数据库中的数量动态的加到窗体上,这步显示是没有任何问题
            /// <summary>
            /// 创建控制器图标
            /// </summary>
            /// <param name="code">编号</param>
            /// <param name="x">坐标X</param>
            /// <param name="y">坐标Y</param>
            /// <param name="warehouse">所在仓库</param>
            /// <param name="InstallDate">安装日期</param>
            private void CreateControllerForMap(string code, int x, int y, string warehouse, string InstallDate)
            {
                this._device.ControllerList.Add(new Controller());            int index = this._device.ControllerList.Count - 1;            this._device.ControllerList[index].P = new Point(x, y);            this._device.ControllerList[index].Code = code;            this._device.ControllerList[index].DevicePic.Tag = index;            this._device.ControllerList[index].DevicePic.Name = "c_pic" + code;            this._device.ControllerList[index].DevicePic.Anchor = AnchorStyles.Bottom | AnchorStyles.Right;            Image img = Image.FromFile(PathList.Img_Controller_Stop);            this._device.ControllerList[index].DevicePic.Image = img;            this._device.ControllerList[index].DevicePic.Width = img.Width;            this._device.ControllerList[index].DevicePic.Height = img.Height;            this._device.ControllerList[index].DevicePic.Location = this._device.ControllerList[index].P;            this._device.ControllerList[index].DevicePic.BorderStyle = BorderStyles.NoBorder;            this._device.ControllerList[index].DevicePic.Properties.ShowMenu = false;            this._device.ControllerList[index].DevicePic.Properties.ReadOnly = false;            this._device.ControllerList[index].DevicePic.Properties.AllowFocused = false;            this._device.ControllerList[index].DevicePic.Properties.SizeMode = PictureSizeMode.Zoom;            this._device.ControllerList[index].DevicePic.ShowToolTips = true;            this._device.ControllerList[index].DevicePic.AllowHtmlTextInToolTip = DevExpress.Utils.DefaultBoolean.True;            this._device.ControllerList[index].DevicePic.Paint += new PaintEventHandler(picForController_Paint);            this._device.ControllerList[index].DevicePic.MouseUp += new MouseEventHandler(picForController_MouseUp);            this._device.ControllerList[index].DevicePic.MouseMove += new MouseEventHandler(picForController_MouseMove);            this._device.ControllerList[index].DevicePic.MouseDown += new MouseEventHandler(picForController_MouseDown);            this.picMain.Controls.Add(this._device.ControllerList[index].DevicePic);
            }
            /// <summary>
            /// 创建控制器信息(Label)
            /// </summary>
            /// <param name="code">编号</param>
            /// <param name="x">坐标X</param>
            /// <param name="y">坐标Y</param>
            /// <param name="warehouse">库房名称</param>
            private void CreateControllerForPnl(string code, ref int x, ref int y, string warehouse)
            {
                int width = this.pageForControllerList.ClientRectangle.Width;         //TabControl容器宽度            PanelControl pnl = new PanelControl();            Label lbcode = new Label();            Label lbwarehouse = new Label();            pnl.Location = new Point(x, y);            pnl.Size = new Size(148, 85);            pnl.Name = "c_pnl" + code;            pnl.Tag = code;            if (x + 148 + 13 + 148 < width)
                {
                    x = x + 161;
                }
                else
                {
                    x = 13;                y = y + 13 + 85;
                }            lbcode.Location = new Point(9, 11);            lbcode.BackColor = Color.BlueViolet;            lbcode.Text = code + "#";            lbcode.BackColor = System.Drawing.SystemColors.GradientActiveCaption;            lbcode.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            lbcode.ForeColor = System.Drawing.Color.Snow;            lbcode.Size = new Size(26, 12);            lbwarehouse.Location = new Point(39, 11);            lbwarehouse.Text = warehouse;            int index = this._device.ControllerList.Count - 1;            Image img = Image.FromFile(PathList.Img_Controller_Status_Stop);            this._device.ControllerList[index].StatusPic.Image = img;            this._device.ControllerList[index].StatusPic.Location = new Point(9, 40);            this._device.ControllerList[index].StatusPic.Size = img.Size;            this._device.ControllerList[index].StatusPic.BorderStyle = BorderStyles.NoBorder;            this._device.ControllerList[index].StatusPic.BackColor = Color.Transparent;            this._device.ControllerList[index].StatusPic.Properties.ShowMenu = false;            this._device.ControllerList[index].StatusPic.Properties.AllowFocused = false;            this._device.ControllerList[index].LbStatus.Location = new Point(39, 46);            this._device.ControllerList[index].LbStatus.Name = "c_lbstatus" + code;            this._device.ControllerList[index].LbStatus.Text = "已停止";            this._device.ControllerList[index].LbStatus.Size = new Size(100, 18);            lbwarehouse.Font = this._device.ControllerList[index].LbStatus.Font = new System.Drawing.Font("宋体", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));            pnl.Controls.Add(lbcode);            pnl.Controls.Add(lbwarehouse);            pnl.Controls.Add(this._device.ControllerList[index].StatusPic);            pnl.Controls.Add(this._device.ControllerList[index].LbStatus);            pnl.DoubleClick += new EventHandler(pnlForController_DoubleClick);            this.pageForControllerList.Controls.Add(pnl);            this.pageForControllerList.Invalidate(true);
            }然后有个定时器,定时去采集数据,根据数据确定是不是要开启空调联动
    如果要开启
    private void OperController(string mintemp, string maxtemp, string mindist, string maxdist, string temper, string dist, string warehouse, string code)
            {
                IDataReader idr = this._dal.GetSensorRelativeControllerInfo(code);            try
                {
                    while (idr.Read())
                    {
                        object controllercode = idr["ControllerCode"];                    DeviceCmd.ControllerType c_type = (DeviceCmd.ControllerType)Enum.Parse(typeof(DeviceCmd.ControllerType), idr["DeviceTypeId"].ToString());                    int index = GetControllerPicIndex(controllercode);                    if (Convert.ToDouble(temper) > Convert.ToDouble(maxtemp) || Convert.ToDouble(temper) < Convert.ToDouble(mintemp) || Convert.ToDouble(dist) > Convert.ToDouble(maxdist) || Convert.ToDouble(dist) < Convert.ToDouble(mindist))
                        {
                            if (this._device.ControllerList[index].Status == 2)
                            {
                                WriteComForController(Convert.ToByte(controllercode), c_type, DeviceCmd.ControllerCmdType.运行);                            this._dal.AddControllingRecord(code, "1", DateTime.Now.ToString());                            this.Invoke(new EventHandler(delegate
                                {
                                    this._device.ControllerList[index].DevicePic.Image = Image.FromFile(PathList.Img_Controller_Start);                                this._device.ControllerList[index].StatusPic.Image = Image.FromFile(PathList.Img_Controller_Status_Start);                                this._device.ControllerList[index].LbStatus.Text = "运行中";
                                    this._device.ControllerList[index].Status = 1;
                                }));
                                Thread.Sleep(1000 * 5);
                                continue;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            if (this._device.ControllerList[index].Status == 1)
                            {
                                WriteComForController(Convert.ToByte(controllercode), c_type, DeviceCmd.ControllerCmdType.停止);                            this._dal.AddControllingRecord(code, "2", DateTime.Now.ToString());                            this.Invoke(new EventHandler(delegate
                                {
                                    this._device.ControllerList[index].DevicePic.Image = Image.FromFile(PathList.Img_Controller_Stop);                                this._device.ControllerList[index].StatusPic.Image = Image.FromFile(PathList.Img_Controller_Status_Stop);                                this._device.ControllerList[index].LbStatus.Text = "已停止";
                                    this._device.ControllerList[index].Status = 2;
                                }));                            Thread.Sleep(200);                            continue;
                            }
                            else
                            {
                                continue;
                            }
                        }
                    }
                }
                catch (Exception ex) { MessageBox.Show(ex.ToString()); }            idr.Dispose();            idr.Close();
            }
    以上标红色的就是赋值的地方,但界面不刷新过来
      

  14.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using DevExpress.XtraEditors;
    using System.Windows.Forms;namespace Onsoft.Main.Common
    {
        public class Controller : DeviceInfo
        {
            /// <summary>
            /// 1)运行 2)停止 3)查询
            /// </summary>
            private int _status = 2;        private int _kind = 0;        private Label _lbStatus = new Label();        private PictureEdit _statusPic = new PictureEdit();        public int Status
            {
                get { return _status; }
                set { _status = value; }
            }        public int Kind
            {
                get { return _kind; }
                set { _kind = value; }
            }        public PictureEdit StatusPic
            {
                get { return _statusPic; }
                set { _statusPic = value; }
            }        public Label LbStatus
            {
                get { return _lbStatus; }
                set { _lbStatus = value; }
            }
        }
    }controller类using System;
    using System.Collections.Generic;
    using System.Text;namespace Onsoft.Main.Common
    {
       public class DeviceList
        {
            private List<Sensor> _sensorList = new List<Sensor>();        private List<Controller> _controllerList = new List<Controller>();        public List<Sensor> SensorList
            {
                get
                {
                    return _sensorList;
                }
                set
                {
                    _sensorList = value;
                }
            }        public List<Controller> ControllerList
            {
                get
                {
                    return _controllerList;
                }
                set
                {
                    _controllerList = value;
                }
            }
        }
    }
    设备列表,