-
September 30th, 2013, 04:57 AM
#1
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..
-
September 30th, 2013, 06:29 AM
#2
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.
-
September 30th, 2013, 04:45 PM
#3
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) {
}
};
}
-
September 30th, 2013, 06:47 PM
#4
Re: how to assign ' const char* ' return value of a function to 'label' in windows f
Originally Posted by sreenad
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
-
September 30th, 2013, 09:10 PM
#5
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
Originally Posted by sreenad
[...] 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.
-
October 1st, 2013, 12:20 AM
#6
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).
-
October 1st, 2013, 03:09 AM
#7
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???
-
October 1st, 2013, 03:28 AM
#8
Re: how to assign ' const char* ' return value of a function to 'label' in windows f
Originally Posted by sreenad
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
-
October 1st, 2013, 03:46 AM
#9
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.
-
October 1st, 2013, 05:40 AM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|