Results 1 to 10 of 10

Thread: Getting a Label control's text to update

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    Question 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.

  2. #2

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    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.

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    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.

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,598

    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

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,413

    Re: Getting a Label control's text to update

    Quote Originally Posted by passel View Post
    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…’

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2002
    Posts
    158

    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...

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width