Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Display current time in Label control

Display current time in Label control

Scheduled Pinned Locked Moved Managed C++/CLI
helpcsharpc++visual-studio
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lucidation
    wrote on last edited by
    #1

    I'm almost brand new to C++ so I'm hoping someone can help me out. I'm using Visual Studio 2008 to create a C++ Windows Form application, and I want to display the current time in place of some static text I was previously using as one of my Label controls. I've spent some time poking around and I think I'm missing something basic. I was trying something like this: this->label1->Text = System::DateTime::Now; and something a little more complexe like this: this->label1->Text = (System::DateTime::Now.ToString("T")); But no matter how I manipulate the line, either the code editor or the design editor within Visual Basic Studio barks at me. On my first example shown above, the code editor tells me it "....cannot convert parameter 1 from 'System::DateTime' to 'System::String ^'" while the design editor is just fine. And on my second example shown above, the design editor tells me "C++ CodeDOM parser error:Line:76, Column:56 --- Internal Error" while the code editor is just fine. So can someone help me out here and let me know what I'm missing? My goal is to just have something like "5:11:45 PM" show up as the text of my Label control when a user runs the program. Thanks!

    L 1 Reply Last reply
    0
    • L Lucidation

      I'm almost brand new to C++ so I'm hoping someone can help me out. I'm using Visual Studio 2008 to create a C++ Windows Form application, and I want to display the current time in place of some static text I was previously using as one of my Label controls. I've spent some time poking around and I think I'm missing something basic. I was trying something like this: this->label1->Text = System::DateTime::Now; and something a little more complexe like this: this->label1->Text = (System::DateTime::Now.ToString("T")); But no matter how I manipulate the line, either the code editor or the design editor within Visual Basic Studio barks at me. On my first example shown above, the code editor tells me it "....cannot convert parameter 1 from 'System::DateTime' to 'System::String ^'" while the design editor is just fine. And on my second example shown above, the design editor tells me "C++ CodeDOM parser error:Line:76, Column:56 --- Internal Error" while the code editor is just fine. So can someone help me out here and let me know what I'm missing? My goal is to just have something like "5:11:45 PM" show up as the text of my Label control when a user runs the program. Thanks!

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I would start by dropping the parentheses around the righthand side. :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


      Getting an article published on CodeProject now is hard and not sufficiently rewarded.


      L 1 Reply Last reply
      0
      • L Luc Pattyn

        I would start by dropping the parentheses around the righthand side. :)

        Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


        Getting an article published on CodeProject now is hard and not sufficiently rewarded.


        L Offline
        L Offline
        Lucidation
        wrote on last edited by
        #3

        If I change it to this: this->label1->Text = System::DateTime::Now.ToString("T"); I get the same error in the design editor as when it's set to this: this->label1->Text = (System::DateTime::Now.ToString("T")); I should also mention, that as long as the code editor doesn't have a problem, when I run the compiled .EXE it works just fine. But I'm leary of ignoring an error in the design editor since this is kinda of my first C++ project.

        L 1 Reply Last reply
        0
        • L Lucidation

          If I change it to this: this->label1->Text = System::DateTime::Now.ToString("T"); I get the same error in the design editor as when it's set to this: this->label1->Text = (System::DateTime::Now.ToString("T")); I should also mention, that as long as the code editor doesn't have a problem, when I run the compiled .EXE it works just fine. But I'm leary of ignoring an error in the design editor since this is kinda of my first C++ project.

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, 1. as long as there are compiler errors, you are not running your new code. you are either running a previous version (the last EXE that got build successfully), or not at all. which it is depends on some option (see menu Tools/Options/Projects and Solutions/Build and Run/On Run, when errors occur...; I suggest "Do not launch"). 2.

          this->label1->Text=DateTime::Now.ToString("T");

          works fine for me. :)

          Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


          Getting an article published on CodeProject now is hard and not sufficiently rewarded.


          L 1 Reply Last reply
          0
          • L Luc Pattyn

            Hi, 1. as long as there are compiler errors, you are not running your new code. you are either running a previous version (the last EXE that got build successfully), or not at all. which it is depends on some option (see menu Tools/Options/Projects and Solutions/Build and Run/On Run, when errors occur...; I suggest "Do not launch"). 2.

            this->label1->Text=DateTime::Now.ToString("T");

            works fine for me. :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            Getting an article published on CodeProject now is hard and not sufficiently rewarded.


            L Offline
            L Offline
            Lucidation
            wrote on last edited by
            #5

            Okay, so this is what I tried just now... 1. Make a brand new Windows Forms Application project (in Visual Studio 2008 Professional Edition). 2. Place a Label control on the empty form. 3. Select View | Code. 4. Change the line that reads "this->label1->Text = L"label1";" to "this->label1->Text = DateTime::Now.ToString("T");" 5. Select Build | Build Solution. 6. The code editor says the build succeeded. 7. I click the "Form1.h" tab to switch to the design editor, and it says "C++ CodeDOM parser error:Line:70,Column:47 --- Internal Error". So am I doing something wrong, or missing something?

            G 1 Reply Last reply
            0
            • L Lucidation

              Okay, so this is what I tried just now... 1. Make a brand new Windows Forms Application project (in Visual Studio 2008 Professional Edition). 2. Place a Label control on the empty form. 3. Select View | Code. 4. Change the line that reads "this->label1->Text = L"label1";" to "this->label1->Text = DateTime::Now.ToString("T");" 5. Select Build | Build Solution. 6. The code editor says the build succeeded. 7. I click the "Form1.h" tab to switch to the design editor, and it says "C++ CodeDOM parser error:Line:70,Column:47 --- Internal Error". So am I doing something wrong, or missing something?

              G Offline
              G Offline
              Ghydo
              wrote on last edited by
              #6

              Your code is correct, but you should not modify "by hand" the code inside the InitializeComponent method. You should put your custom initialization code inside the constructor, after the call to InitializeComponent().

              L 1 Reply Last reply
              0
              • G Ghydo

                Your code is correct, but you should not modify "by hand" the code inside the InitializeComponent method. You should put your custom initialization code inside the constructor, after the call to InitializeComponent().

                L Offline
                L Offline
                Lucidation
                wrote on last edited by
                #7

                Wohoo! That did it. Thanks!

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups