Display current time in Label control
-
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!
-
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!
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.
-
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.
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.
-
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.
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.
-
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.
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?
-
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?
-
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 toInitializeComponent()
.Wohoo! That did it. Thanks!