how to assign ' const char* ' return value of a function to 'label' in windows form
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: how to assign ' const char* ' return value of a function to 'label' in windows form

  1. #1
    Join Date
    Sep 2013
    Posts
    5

    Smile how to assign ' const char* ' return value of a function to 'label' in windows form

    i am a newbie to C++ and VS ++. I have created a windows form application by dragging and dropping button, label..etc. i wish label text to be appeared as return value from a function. The function returns ' const char* '.how this returned string pointer can be used to display label text.?

    Thanks in advance..

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

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Code:
    label->Text = Marshal::PtrToStringAnsi(IntPtr(Function()));
    Last edited by Alex F; September 30th, 2013 at 07:28 AM.

  3. #3
    Join Date
    Sep 2013
    Posts
    5

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Even though this gave an error for the first time, that worked when i used "using namespace System::Runtime::InteropServices;" Thanks. Now,I am facing one more problem. I cant access a global variable from Form1.h. My main file is test01.cpp. 'AP_HANDLE apbase' is a variable which is part of an sdk. i can access this 'apbase' from main() but cant from form.h. I tried to use this code inside InitializeComponent(void). pls see the code. usage was - this->label1->Text=Marshal::PtrToStringAnsi(IntPtr(ap_GetPartNumber(apbase)));

    //test01.cpp
    // test01.cpp : main project file.

    #include "stdafx.h"
    #include "apbase.h"
    #include "Form1.h"

    using namespace test01;

    AP apbase;

    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {

    // Enabling Windows XP visual effects before any controls are created
    Application::EnableVisualStyles();
    Application::SetCompatibleTextRenderingDefault(false);


    // Create the main window and run it
    Application::Run(gcnew Form1());
    return 0;
    }



    //FORM1.h
    #pragma once

    namespace test01 {

    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::Windows::Forms;
    using namespace System:ata;
    using namespace System:rawing;
    using namespace System::Runtime::InteropServices;

    /// <summary>
    /// Summary for Form1
    /// </summary>
    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:

    Form1()
    {
    //s1.part_num=11;
    InitializeComponent();

    //
    //TODO: Add the constructor code here
    //
    }

    protected:
    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    ~Form1()
    {
    if (components)
    {
    delete components;
    }
    }
    private: System::Windows::Forms::Label^ label1;

    protected:

    private:
    /// <summary>
    /// Required designer variable.
    /// </summary>
    System::ComponentModel::Container ^components;

    #pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    void InitializeComponent(void)
    {
    this->label1 = (gcnew System::Windows::Forms::Label());
    this->SuspendLayout();
    //
    // label1
    //
    this->label1->AutoSize = true;
    this->label1->Location = System:rawing::Point(22, 9);
    this->label1->Name = L"label1";
    this->label1->Size = System:rawing::Size(0, 13);
    this->label1->TabIndex = 0;
    this->label1->Text=Marshal::PtrToStringAnsi(IntPtr(ap_GetPartNumber(apbase)));
    //
    // Form1
    //
    this->AutoScaleDimensions = System:rawing::SizeF(6, 13);
    this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    this->ClientSize = System:rawing::Size(292, 266);
    this->Controls->Add(this->label1);
    this->Name = L"Form1";
    this->Text = L"Form1";
    this->Load += gcnew System::EventHandler(this, &Form1::Form1_Load);
    this->ResumeLayout(false);
    this->PerformLayout();

    }
    #pragma endregion
    private: System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
    }
    };
    }

  4. #4
    Join Date
    Apr 1999
    Posts
    27,424

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Quote Originally Posted by sreenad View Post
    I cant access a global variable from Form1.h. My main file is test01.cpp. 'AP_HANDLE apbase' is a variable which is part of an sdk. i can access this 'apbase' from main() but cant from form.h. I tried to use this code inside InitializeComponent(void). pls see the code. usage was -
    What is "gcnew"? Why is the "^" operator in a strange place in your code?

    The point is that the code you posted is Managed C++, and this forum does not deal with managed C++. This forum is for non-managed C++ code using the Visual C++ compiler. There is a managed C++ forum where you should be posting your question.

    Regards,

    Paul McKenzie

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,583

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Things I'd like to add...

    The C++/CLI (AKA managed C++) forum section towards which Paul pointed you is here: http://forums.codeguru.com/forumdisp...ed-C-and-C-CLI

    Quote Originally Posted by sreenad View Post
    [...] Now,I am facing one more problem. I cant access a global variable from Form1.h. My main file is test01.cpp. 'AP_HANDLE apbase' is a variable which is part of an sdk. i can access this 'apbase' from main() but cant from form.h. [...]
    The only reason I can see why you can't access that global valiable fom Form1.h is that the #include "apbase.h" is missing there, which apparently contains the declaration of the global variable. Try inserting it right after the #pragma once. If you still can't access the variable from there (however, I couldn't tell why, then), you may add a constructor parameter of the appropriate type to Form1::Form1() and pass in the variable from within main(). You can then use the passed value at will in the form class constructor, including assigning it to a form class member of the appropriate type you create, so you can access it from any Form1 member function if desired.

    [...] I tried to use this code inside InitializeComponent(void). pls see the code. usage was - this->label1->Text=Marshal::PtrToStringAnsi(IntPtr(ap_GetPartNumber(apbase)));
    Never ever do that! This warning placed in Form1.h by the IDE is not a joke:

    Code:
    #pragma region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    Never touch any of the code between the #pragma highlighted in blue in the snippet above and the corresponding #pragma endregion. Failure to observe this is likely to severely mess up the Forms Designer.

    Finally: Please use code tags when posting code.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Jul 2002
    Posts
    2,512

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Add lines:

    #include "apbase.h"
    extern AP apbase;

    to Form1.h. Move initialization code to another place (like constructor or Form Load event).

  7. #7
    Join Date
    Sep 2013
    Posts
    5

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    error C2440: '<function-style-cast>' : cannot convert from 'const char *' to 'System::IntPtr'

    That const char * to string is not working for me.
    line which made this error is --
    this->label1->Text=Marshal::PtrToStringAnsi(IntPtr(s1.part_num));
    s1 is an object with a member called 'part_num'. part_num stores a return pointer from a function called ap_GetPartnumber() [this function return type is const char *]. I declared part_num as const char * part_num; inside class declaration.
    i call ap_Getpartnumber() to assign value to part_num. i later use this part_num in form1.h to display the label. i need label in some other format.So i used Marshal::PtrToStringAnsi(IntPtr(s1.part_num)); as mentioned by "Alex F". Now what to do???

  8. #8
    Join Date
    Apr 1999
    Posts
    27,424

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Quote Originally Posted by sreenad View Post
    error C2440: '<function-style-cast>' : cannot convert from 'const char *' to 'System::IntPtr'

    That const char * to string is not working for me.
    Please ask your question in the appropriate forum. There is no such thing as "System::IntPtr" as far as this forum is concerned.

    Having said that, I am curious as to how you're returning this pointer. Are you returning a pointer to a local string? If not, then nothing you will do will work correctly. Returning pointers to anything requires that the thing being pointed to exists when the function returns, and so far I see no code (C++, non-Managed code) where you show how this function is implemented.

    Regards,

    Paul McKenzie

  9. #9
    Join Date
    Sep 2013
    Posts
    5

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    Hi Paul McKenzie,
    My aim is, i want to display the label text of the windows form at my wish. Suppose i have a function() which returns const char * and i assigned this value to a variable "const char *p". i.e i assign by writing "p=function()". So p is a pointer to a string. I want to display this string as label using this pointer. I know, i cant simply use "this->label1->Text=p"

    How this can be done? pls help.

  10. #10
    Join Date
    Sep 2013
    Posts
    5

    Re: how to assign ' const char* ' return value of a function to 'label' in windows f

    I found the answer:

    System::String has a constructor that takes a char*:

    const char* charstr = "Hello, world!";
    String^ clistr = gcnew String(charstr);
    clistr will hold the string that can be assigned to Label->text

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center