473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:
Expand|Select|Wrap|Line Numbers
  1. public partial class ImagePBX : UserControl
  2.     {
  3.         private Label label;
  4.         private string _LabelText = "Label";
  5.         public ImagePBX()
  6.         {
  7.             InitializeComponent();
  8.             this.label = new Label();
  9.             this.label.Text = _LabelText;
  10.             this.Controls.Add(label);
  11.         }
  12.         public string LabelText
  13.         {
  14.             get { return _LabelText; }
  15.             set { _LabelText = value; ; }
  16.         }
  17.     }
  18.  
  19.  
  20. public partial class Form1 : Form
  21.     {
  22.         public Form1()
  23.         {
  24.             InitializeComponent();
  25.             imagePBX1.LabelText = "string1";
  26.             imagePBX1.Refresh();
  27.          }
  28.     }
Jun 15 '10 #1
6 14862
GaryTexmo
1,501 Expert 1GB
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.
Jun 15 '10 #2
@GaryTexmo
I update the label in the set method.
but it dosn't work.
Jun 16 '10 #3
ThatThatGuy
449 Expert 256MB
@MehrnooshD
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
Jun 16 '10 #4
GaryTexmo
1,501 Expert 1GB
@MehrnooshD
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.
Jun 16 '10 #5
NitinSawant
270 100+
try this

Expand|Select|Wrap|Line Numbers
  1.          public string LabelText
  2.          {
  3.              get { return label.Text; }
  4.              set { label.Text = value; }
  5.          }
  6.  
Jun 18 '10 #6
@NitinSawant
Thanks for your replies.It works.
Jun 19 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: BillZondlo | last post by:
How come when I put the label text setting here: /// The main entry point for the application. /// </summary> static void Main() { Application.Run(new Form1()); } private void LoadInfo()
3
by: kai | last post by:
Hi, All When I place a label on a Web form, label text is always left align, I cannot find the property to set right align. Any way to set align property for the label text to right align? ...
2
by: kaczmar2 | last post by:
I have an ASP.NET page written in VB.NET that has a label: <asp:Label runat="server" ID="lblStatus" CssClass="LabelTxt"></asp:Label> In my code behind, I am running some stored procedures and...
3
by: Mike Johnson | last post by:
I'm new to VB.Net and programming. I just brought VB.Net Standard I'm working on a small program for work. I've created two forms the first is named Forms1 and the second is named SettingsForm on...
4
by: nate axtell | last post by:
In a VB .NET app I changed the text of a label then proceed to run a stored procedure which takes about 20 seconds. The label change doesn't show until after the stored procedure is finished. Is...
2
by: rigamonk | last post by:
I have a form that runs a procedure when a button is clicked. the procedure does a few things, then outputs the results to labels on a form....which it does fine. However, when I call that...
2
by: Joe Delphi | last post by:
Hi, I want to position label text so that it always appears centered when the user re-sizes the browser window. I tried adding the HTML property text-align="CENTER" to the code, but ASP.NET...
3
by: ASP Developer | last post by:
I have a variety of pages that use a master page. For some reason when I attempt to copy any of the label text on the screen it seems to highlight most of the screen randomly and I can't simply...
1
by: rmiah | last post by:
Hi I am using Visual Basic 2005. I have a stream of data which I want to set over 20 label.text properties. Instead of setting the data individually ie label1.text = ....label2.text=........
12
by: vbnewbie | last post by:
I am having problems accessing properties of dynamically generated objects in VB2005. Can someone please help? In a nutshell: My app creates an equal number of checkboxes and labels that share the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.