|
-
Jun 29th, 2022, 09:45 AM
#1
Thread Starter
Addicted Member
Getting a Label control's text to update
I am working on a small application that includes several Label controls and I would like to update the text in these controls. Thus far, the only things that works is to delete the control, then recreate it. I am certain that there is a better way, but I have not yet found it.
Following is a simplified version of my code that shows what I have tried that doesn't work.
Code:
Public Class frmMyTime
Dim gTime As String
Dim lblTime As Label
Private Sub frmMyTime_Load(sender As Object, e As EventArgs) Handles Me.Load
lblTime = New label
Me.Controls.Add(lblTime)
End Sub
Private Sub frmMyTime_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
lblTime.Text = gTime
End Sub
Private ub GetTime()
gTime = DateTime.Now.ToString("HH:mm:ss")
Me.Refresh()
End Sub
Public Sub frmMyTime_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
GetTime()
End Sub
End Class
Can you please tell me what to change to make this work. Thanks!
Last edited by groston; Jun 29th, 2022 at 09:52 AM.
-
Jun 29th, 2022, 09:52 AM
#2
Thread Starter
Addicted Member
Re: Getting a Label control's text to update
Damn - I just tried this simplified code and it works - kinda. The time is being shown, but not in the Label control.
-
Jun 29th, 2022, 10:49 AM
#3
Re: Getting a Label control's text to update
If you want something to show up in a textbox, then assign the text to the .Text of the textbox ... just like you are with the label. Also, why add the label at run-time? Just add it to the form at designtime,.... give it a name, then use LableName.Text = ...
-tg
-
Jun 29th, 2022, 11:14 AM
#4
Thread Starter
Addicted Member
Re: Getting a Label control's text to update
Thanks for the reply. I create the Label at run time because that way, I know that I can get the exact placement that I want. Not sure why you mentioned Textbox - it is a Label control with which I am being challenged. More importantly, assigning the text to the .Text property does not seem to be working.
-
Jun 29th, 2022, 11:26 AM
#5
Re: Getting a Label control's text to update
Why would you think that using the Paint event that way is a good idea? That is not a rhetorical question; I really want to know. It is not, I assure you. If you explain what you're actually trying to achieve then we can explain how to achieve it. Setting the Text property will work fine, if you do it in a sensible way.
-
Jun 29th, 2022, 11:54 AM
#6
Thread Starter
Addicted Member
Re: Getting a Label control's text to update
I am not an expert when it comes to 'graphics' programming. In my app, I need to update the text field of some Label controls and some images in Image controls. Based on my 'deep' research, it seemed like the Paint event was the way to do that. Obviously, I erred. Please suggest a different approach (and note that the configuration of the above code make sense since the GetTime sub is called from a Timer.
-
Jun 29th, 2022, 01:45 PM
#7
Re: Getting a Label control's text to update
Just updating the text when you change it should work.
Code:
Public Class frmMyTime
Dim gTime As String
Dim lblTime As Label
Private Sub frmMyTime_Load(sender As Object, e As EventArgs) Handles Me.Load
lblTime = New label
Me.Controls.Add(lblTime)
End Sub
Private ub GetTime()
gTime = DateTime.Now.ToString("HH:mm:ss")
lblTime.Text = gTime
End Sub
Public Sub frmMyTime_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
GetTime()
End Sub
End Class
But since this is simplified test code and not what you want to do, i.e. updating graphics and using a timer, then it may not be really applicable to how you need to do it in your real case. jmcilhinney is correct in saying we need to know more details of what you are trying to do, before we can suggest an approach.
p.s. It should be updating the label, but since you don't position it and it has no border by default, perhaps you think it is updating text on the form. You can set the borderstyle and reposition the label in your example code to verify it is the label that is being updated.
Code:
Private Sub frmMyTime_Load(sender As Object, e As EventArgs) Handles Me.Load
lblTime = New Label
lblTime.BorderStyle = BorderStyle.Fixed3D
lblTime.Location = New Point(100, 50)
Me.Controls.Add(lblTime)
End Sub
Last edited by passel; Jun 29th, 2022 at 01:53 PM.
"Anyone can do any amount of work, provided it isn't the work he is supposed to be doing at that moment" Robert Benchley, 1930
-
Jun 29th, 2022, 01:54 PM
#8
Re: Getting a Label control's text to update
 Originally Posted by passel
jmcilhinney is correct in saying we need to know more details of what you are trying to do, before we can suggest an approach.
I agree…’
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jun 29th, 2022, 04:49 PM
#9
Thread Starter
Addicted Member
Re: Getting a Label control's text to update
I think I now know why I was doing this wrongly: I drew a few lines in the form and had to do that from the Paint event and then tries to do everything else from there. Working much better now - just need to figure out the PictureBox control...
-
Jun 29th, 2022, 09:08 PM
#10
Re: Getting a Label control's text to update
The reason you use the Paint event to do things like draw lines is that such drawing will disappear on each event, so you need to reinstate it. Property values like Text don't disappear because something gets painted. The Image property of a PictureBox is the same. Just assign an Image object to the property at the appropriate time, e.g. on the Click of a Button, and the control will handle the rest.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|