• 0

[VB.NET] Date & Time on Label


Question

Hi, I am new to VB.NET, and I have a question.

I wonder how to put current date and time on a label? I put a label (label1) on window form, and inserted following code in it:

Private Sub Label11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label11.Click
	Me.Text = Date.Now
End Sub

However, the date will be printed on window title when I click on the label.

I tried to put Label11.Text = Date.Now just under Public Class Form1, but the IDE told me to declare Label11 first.

Could someone give me some hints? Thanks very much!

Link to comment
https://www.neowin.net/forum/topic/622005-vbnet-date-time-on-label/
Share on other sites

4 answers to this question

Recommended Posts

  • 0
  Gundamdriver said:
Hi, I am new to VB.NET, and I have a question.

I wonder how to put current date and time on a label? I put a label (label1) on window form, and inserted following code in it:

Private Sub Label11_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label11.Click
	Me.Text = Date.Now
End Sub

However, the date will be printed on window title when I click on the label.

I tried to put Label11.Text = Date.Now just under Public Class Form1, but the IDE told me to declare Label11 first.

Could someone give me some hints? Thanks very much!

In .NET, Me is always referring to the Form itself. Label11.Text = Date.Now() is correct, but

you have to declare a statement first (in your case a Form1.Load statement) So that when the form loads, the label has the time in it. Remember, though, the time will not change in the label afterwards, since the load statement was run and doesn't run again. If you want to keep the time updated, you can insert a Timer object which will keep the time in the label updated.

Here's your opening code:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Label1.Text = Date.Now

End Sub

Then, if you want a timer event to update the time with each passing second, you drop a Timer component onto the Form, and Enable it in the Properties sheet. Then, in the Form1_Load Event, Type the following:

Timer1.Start()

Then this as a new Timer_Tick Event (as another Subroutine, of course)

Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
		Label1.Text = Date.Now
	End Sub

Enjoy!

Edited by devHead
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Posts

    • Will be available in EventViewer. Suppose they could have also displayed upon next log-on (same as 'didn't shutdown properly' on servers) but seems not.
    • PasteBar 0.7.0 by Razvan Serea PasteBar is a powerful yet free clipboard manager that revolutionizes how you copy, organize, and reuse information. Packed with smart features, it enhances productivity and simplifies your workflow by minimizing repetitive copy-paste tasks—so you can work faster and smarter. PasteBar offers powerful clipboard management with unlimited history, searchable copy records, and note support, making it easy to retrieve past clips. You can save and organize clips into custom collections, tabs, or boards for quick access through intuitive paste menus. Privacy is a priority, with local storage, passcode protection, and PIN-locked collections ensuring your data stays secure. The app also supports custom storage locations, giving you full control over where your clipboard history is saved. Beyond basic clipboard functions, PasteBar includes smart features like support for text, images, files, and code snippets—complete with syntax highlighting and Markdown formatting. A global search function, instant pasting, and dark theme enhance usability, while customization options let you tailor hotkeys, tray behavior, and text filters. Cross-platform compatibility (macOS & Windows) ensures seamless workflow integration, with added tools for web scraping, API data extraction, and multi-language support. Backup and restore capabilities provide extra peace of mind, making PasteBar a versatile productivity tool. PasteBar 0.7.0 changelog: Added backup and restore database and images Added custom data location to store application data in a folder of your choice instead of the default location Added keyboard shortcuts to improve navigation efficiency within the application Added long press on clip to activate clips organize Added "Trim Spaces" option for clips and "Auto Trim Spaces" option for History Capture Added Quick history window user options Added option to disable capturing and storing images from clipboard Implemented clipboard history filtering based on text length limits Added support for setting hotkeys with up to 3-key combinations Fixed update history items in quick paste window after delete operation in main window Added user setting for single-click keyboard focus in history Refactored terminology for history window split to open Updated to React 19 and React Compiler Auto-activate search in quick paste window when typing any letter or number Added single-click copy/paste option in user preferences Added persistent note icons for clips with descriptions Added detect and select language modal on first app run Fixed refresh native menu on history items delete Added history item preview max lines user setting Added protected collection with PIN access Added special copy/paste for history items context menu and settings Added global templates in user preferences for saved clips Press Delete key to delete single or multiple items with confirmation on second press Added customizable tray icon behavior on Windows as user settings Added user preference to copy only menu items to clipboard without auto-pasting Fixed enhance value preview logic to fix text overflow Updated French language translation from Jean-Michel Robert Added deleting menu folders with children by moving submenus up one level Fixed user preferences UI color bug for languages Added settings to preserve pinned and starred items when clearing clipboard history Added option "Show Always on Top" for main window Added support for notes matching in global search Added Turkish language translation by AlijonMurodov Updated Chinese translation by katelya77 Download: PasteBar | Portable | ~30.0 MB (Open Source) Download: PasteBar ARM64 | Portable ARM64 Links: PasteBar Website | Screenshot | MS Store Get alerted to all of our Software updates on Twitter at @NeowinSoftware
    • Well, it seems version 4.9 doesn't work well at least when trying to make a WinToGo USB. It ends very fast with an error. Had to use 4.8 instead.
    • Hundreds of printer models with weak password algorithms exposed, no firmware patch possible by Usama Jawad Many people don't think or care about it, but printer security is a pretty important avenue when it comes to evaluating your cybersecurity posture. Last month, it was found that the companion software for Procolored printers was distributing malware. Now, it has been revealed that hundreds of printer models all over the globe are susceptible to attacks targeting their admin credentials. Bleeping Computer has reported that CVE-2024-51978 is a one of the eight printer vulnerabilities recently discovered by security researchers. Combined, these allow authenticated and unauthenticated attackers to discover the default admin password, perform remote code execution (RCE), crash the printer, and leak other sensitive information. Severity ratings go from a score of 5.3 (medium) to 9.8 (critical), indicating that these are pretty severe vulnerabilities. The most dangerous vulnerability in there exposes the default admin password, and primarily affects Brother printers. This is because Brother utilizes a rather weak password generation algorithm that is highly dependent upon the device's serial number and a static salt table. Analysis of the code revealed that the first 16 characters of the serial number are appended with eight bytes from a static salt table, with the results being hashed with SHA256 and Base64-encoded. Finally, the first eight characters are then taken and some of them are replaced with special characters to form the password. The static nature of the password generation algorithm means that an attacker can chain various existing vulnerabilities to get access to your serial number and eventually your default admin password. It is important to note that not all printer models are affected by all of these flaws, but the default admin password exposure does affect 695 models. The breakdown for the number of printer model affected by the eight vulnerabilities is as follows: Brother: 689 Fujifilm: 46 Konica Minolta: 6 Ricoh: 5 Toshiba: 2 Brother has informed the security researchers that it cannot fully remediate the password generation vulnerability through a firmware patch. It can only fix the issue in its next printer models by patching the problem during the manufacturing process. This makes it crucial for customers of affected models to change their default admin password as soon as possible, which is good practice anyway.
  • Recent Achievements

    • Week One Done
      suprememobiles earned a badge
      Week One Done
    • Week One Done
      Marites earned a badge
      Week One Done
    • One Year In
      runge100 earned a badge
      One Year In
    • One Month Later
      runge100 earned a badge
      One Month Later
    • One Month Later
      jfam earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      567
    2. 2
      +FloatingFatMan
      177
    3. 3
      ATLien_0
      169
    4. 4
      Michael Scrip
      127
    5. 5
      Xenon
      119
  • Tell a friend

    Love Neowin? Tell a friend!