CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Feb 2010
    Posts
    15

    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?

  2. #2
    Join Date
    Jul 2002
    Posts
    2,543

    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?

  3. #3
    Join Date
    Feb 2010
    Posts
    15

    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.

  4. #4
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    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

  5. #5
    Join Date
    Jul 2002
    Posts
    2,543

    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.

  6. #6
    Join Date
    Feb 2010
    Posts
    15

    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;

  7. #7
    Join Date
    Feb 2010
    Posts
    15

    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.

  8. #8
    Join Date
    Jul 2002
    Posts
    2,543

    Re: Writing to forms label

    I suggested you to use String::Format function. Did you try this?

  9. #9
    Join Date
    Feb 2010
    Posts
    15

    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;

  10. #10
    Join Date
    Jul 2002
    Posts
    2,543

    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

  11. #11
    Join Date
    Feb 2010
    Posts
    15

    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

  12. #12
    Join Date
    Jul 2002
    Posts
    2,543

    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);

  13. #13
    Join Date
    Feb 2010
    Posts
    15

    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.

  14. #14
    Join Date
    Jan 2010
    Posts
    1,133

    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
  •  





Click Here to Expand Forum to Full Width

Featured