Refreshing a label text

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MehrnooshD
    New Member
    • Jun 2010
    • 4

    Refreshing a label text

    Hello,I am new to C#.
    I have a UserControl. I could not Refresh a label text in the UserControl from another form.

    The Codes are:
    Code:
    public partial class ImagePBX : UserControl
        {
            private Label label;
            private string _LabelText = "Label";
            public ImagePBX()
            {
                InitializeComponent();
                this.label = new Label();
                this.label.Text = _LabelText;
                this.Controls.Add(label);
            }
            public string LabelText
            {
                get { return _LabelText; }
                set { _LabelText = value; ; }
            }
        }
    
    
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                imagePBX1.LabelText = "string1";
                imagePBX1.Refresh();
             }
        }
    Last edited by Curtis Rutland; Jun 15 '10, 02:19 PM. Reason: Please use [CODE] tags when posting code.
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    In your LabelText property, your get/set operates on a local string. That string only assigns itself to the label's text property in the constructor, which happens when the object is created.

    Basically, you need to update the label's text property at some point. A good place for this to happen is the LabelText property you've created. There's technically no need for the local string if you're not using it elsewhere, and also there's nothing stopping you from doing more than one thing in a set method. Another option (though personally, less desirable), could be to override the Refresh method and have that update the label's text property with the local string before calling the base refresh. I wouldn't go with that last one though.

    Comment

    • MehrnooshD
      New Member
      • Jun 2010
      • 4

      #3
      Originally posted by GaryTexmo
      In your LabelText property, your get/set operates on a local string. That string only assigns itself to the label's text property in the constructor, which happens when the object is created.

      Basically, you need to update the label's text property at some point. A good place for this to happen is the LabelText property you've created. There's technically no need for the local string if you're not using it elsewhere, and also there's nothing stopping you from doing more than one thing in a set method. Another option (though personally, less desirable), could be to override the Refresh method and have that update the label's text property with the local string before calling the base refresh. I wouldn't go with that last one though.
      I update the label in the set method.
      but it dosn't work.

      Comment

      • ThatThatGuy
        Recognized Expert Contributor
        • Jul 2009
        • 453

        #4
        Originally posted by MehrnooshD
        Hello,I am new to C#.
        I have a UserControl. I could not Refresh a label text in the UserControl from another form.

        The Codes are:
        Code:
        public partial class ImagePBX : UserControl
            {
                private Label label;
                private string _LabelText = "Label";
                public ImagePBX()
                {
                    InitializeComponent();
                    this.label = new Label();
                    this.label.Text = _LabelText;
                    this.Controls.Add(label);
                }
                public string LabelText
                {
                    get { return _LabelText; }
                    set { _LabelText = value; ; }
                }
            }
        
        
        public partial class Form1 : Form
            {
                public Form1()
                {
                    InitializeComponent();
                    imagePBX1.LabelText = "string1";
                    imagePBX1.Refresh();
                 }
            }
        public string LabelText
        { get { return _LabelText; }
        set { _LabelText = value; ; }

        }
        You could have directly specified the Label.Text inside get and set accessors.. that's more logical and it works

        Comment

        • GaryTexmo
          Recognized Expert Top Contributor
          • Jul 2009
          • 1501

          #5
          Originally posted by MehrnooshD
          I update the label in the set method.
          but it dosn't work.
          You don't update the label text, you're updating the local string. As ThatThatGuy said, you need to update the Label.Text property itself.

          Comment

          • NitinSawant
            Contributor
            • Oct 2007
            • 271

            #6
            try this

            Code:
                     public string LabelText
                     {
                         get { return label.Text; }
                         set { label.Text = value; }
                     }

            Comment

            • MehrnooshD
              New Member
              • Jun 2010
              • 4

              #7
              Originally posted by NitinSawant
              try this

              Code:
                       public string LabelText
                       {
                           get { return label.Text; }
                           set { label.Text = value; }
                       }
              Thanks for your replies.It works.

              Comment

              Working...