-
February 9th, 2010, 10:28 PM
#1
Writing to forms label
I am new to VC++ but have used Borland C++ Builder for many years.
I am trying to write to a forms label using
char s{40];
strcpy(s,"Hello World");
Label1->Text = s;
I get
cannot convert from 'char *' to 'System::String ^'
Can someone give me an example of how to do this?
-
February 10th, 2010, 01:24 AM
#2
Re: Writing to forms label
Label1->Text = "Hello, World";
or
Label1->Text = L"Hello, World";
One of these versions will be OK.
BTW, why do you work with CLR project? Is this really what you need?
-
February 10th, 2010, 11:08 AM
#3
Re: Writing to forms label
Label1->Text = "Hello, World";
or
Label1->Text = L"Hello, World";
These work but I need to change the text periodically and am trying to do this with a changeable character array.
e.g.
char s[40];
float a = 123.45;
sprintf(s,"%.2f",a);
Label1->Text = s;
This is where I get -
cannot convert from 'char *' to 'System::String ^'
I am working with a CLR project because when I created a forms project it told me it was converting my project to CLR. I don't even know what CLR is or does.
-
February 10th, 2010, 11:23 AM
#4
Re: Writing to forms label
Not sure if this will solve your problem, but instead of
Code:
char s[40];
float a = 123.45;
sprintf(s,"%.2f",a);
Label1->Text = s;
try using this
Code:
TCHAR s[40];
float a = 123.45;
_stprintf_s(s,_T("%.2f"), 40, a);
Label1->Text = s;
Hope that helps.
Be sure to rate those who help!
-------------------------------------------------------------
Karl - WK5M
PP-ASEL-IA (N43CS)
PGP Key: 0xDB02E193
PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193
-
February 10th, 2010, 01:14 PM
#5
Re: Writing to forms label
You are writing mixed managed-unmanaged code without any need.
C++/CLR requires good knowledge of both native C++ and .NET. According to your needs, you may decide to go to pure native solution, with using MFC or WTL as UI Framework, or pure .NET solution using C#.
C++/CLI is used only in rare cases, for managed-unmanaged interoperability.
For your specific problem, tale a look at .NET String class and String.Format function, which replaces native sprintf.
-
February 10th, 2010, 02:08 PM
#6
Re: Writing to forms label
I tried your suggestion and get:
1>c:\orbitalprogram\orbital\orbital\Form1.h(189) : error C2664: 'void System::Windows::Forms::Control::Text::set(System::String ^)' : cannot convert parameter 1 from 'TCHAR [40]' to 'System::String ^'
1> Reason: cannot convert from 'TCHAR *' to 'System::String ^'
1> No user-defined-conversion operator available, or
1> Cannot convert an unmanaged type to a managed type
on the statement:
Label1->Text = s;
-
February 10th, 2010, 04:05 PM
#7
Re: Writing to forms label
I tried to go to MFC and various other options to build a project using Windows Forms. I really need Windows Forms but when I select it, I get the message it is converting to CLR.
The only problem I have with the CLR approach is the problem of setting
Label1->Text = s; where s is a character array defined by char s[40];
I really need to do this.
-
February 11th, 2010, 02:49 PM
#8
Re: Writing to forms label
I suggested you to use String::Format function. Did you try this?
-
February 13th, 2010, 10:12 AM
#9
Re: Writing to forms label
I've tried many combinations of String:Format with no success. I just don't know how to use it.
Could you please give me the missing puzzle piece so I can get on track.
I need:
double V = 1.2345;
char s[40];
String^ StrVal;
sprintf(s,"%.4f",V);
(Set Strval string to s contents)
Label1->Text = StrVal;
-
February 13th, 2010, 12:25 PM
#10
Re: Writing to forms label
I wonder why you write sprintf and call this String.Format.
http://msdn.microsoft.com/en-us/libr...ng.format.aspx
-
February 13th, 2010, 01:27 PM
#11
Re: Writing to forms label
double V = 1.2345;
String^ StrVal;
StrVal = String::Format("F01", V);
Label1->Text = StrVal;
I get F01 displayed instead of 1.2
-
February 14th, 2010, 01:25 AM
#12
Re: Writing to forms label
Do you really read something before asking the question?
StrVal = String::Format("{0}", V);
StrVal = String::Format("{0,8:0.000}", V);
StrVal = String::Format("{0:0000.000}", V);
-
February 20th, 2010, 09:52 AM
#13
Re: Writing to forms label
Alex,
That worked. Many thnanks for your patience. I have spent a lot of time trying to find this information to no avail.
-
February 21st, 2010, 08:41 AM
#14
Re: Writing to forms label
I'd just like to add that a simpler way would be to declare a .NET string:
// this declares a variable of a reference type (a.k.a __gc type) string,
// also known as a pointer to an object on the managed heap,
// or a handle to a managed object. The ^ replaces __gc* sintax.
String^ s;
This can be initialized in the constructor, and its value can be manipulated anytime by simply assigning a new string
s = "some text";
or by using the ToString() method which all .NET types inherit from System::Object
double d = 1.2345;
s = d.ToString();
P.S. I think that, when you create a .NET (CLR) project, all the fundamental type keywords should be aliases of their .NET equivalents.
Last edited by TheGreatCthulhu; February 21st, 2010 at 08:54 AM.
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
|