How to assign TextBox value to string

R

rachit.goyal

Hi All,
I am using .net first time.In my windows form VC7 application, on a
click of button, I am assigning text of TextBox to STL string varibale.
Using this code-

private: System::Void FindValuesButton_Click(System::Object * sender,
System::EventArgs * e)
{
string fileLoc ;
fileLoc= CD_location->Text->ToString();
.......
}

I am getting error-

'initializing' : cannot convert from 'System::String __gc *' to
std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

I think this is due to manage class problem. Could anyone tell me, how
can I assign text box value to STL string. Can I use STL string in
VC++.net application.

Regards,
Rachit
 
S

SvenC

Hi rachit,

Hi All,
I am using .net first time.In my windows form VC7 application, on a
click of button, I am assigning text of TextBox to STL string varibale.
Using this code-

private: System::Void FindValuesButton_Click(System::Object * sender,
System::EventArgs * e)
{
string fileLoc ;
fileLoc= CD_location->Text->ToString();
......
}

I am getting error-

'initializing' : cannot convert from 'System::String __gc *' to
std::basic_string<_Elem,_Traits,_Ax>'
I think this is due to manage class problem. Could anyone tell me, how
can I assign text box value to STL string. Can I use STL string in
VC++.net application.

Have a look at System.Runtime.InteropServices.Marshal
You'll find StringToHGlobalAnsi and StringToCoTaskMemAnsi.
That gives you a native char* pointer you can assign to std::string
instances.
Do not forget to call FreeHGlobal or FreeCoTaskMem to release the memory.
 
M

Micus

Hi All,
I am using .net first time.In my windows form VC7 application, on a
click of button, I am assigning text of TextBox to STL string varibale.
Using this code-

private: System::Void FindValuesButton_Click(System::Object * sender,
System::EventArgs * e)
{
string fileLoc ;
fileLoc= CD_location->Text->ToString();
......
}

I am getting error-

'initializing' : cannot convert from 'System::String __gc *' to
std::basic_string<_Elem,_Traits,_Ax>'
with
[
_Elem=char,
_Traits=std::char_traits<char>,
_Ax=std::allocator<char>
]

I think this is due to manage class problem. Could anyone tell me, how
can I assign text box value to STL string. Can I use STL string in
VC++.net application.

Regards,
Rachit

Rachit,

I believe you can use the properties Char and Length of System::String
to pull out the individual characters and place them in a wchar_t array then
assign your STL string to the array. However note 2 things: First, you will
need to append an '\0' character to the end of the wchar_t array; Second,
from your listing of the error, the STL string template is using <char> but
the System::String is made up of unicode characters. Try declaring your
string as std::basic_string<wchar_t>... I'm a bit rusty but I would try
something along the lines of the following

std::basic_string<wchar_t> fileLoc;
wchar_t buffer[255]; //size it appropriately
for(int i = 0; i < CD_location->Text->Length; ++i)
{
buffer = CD_location->Text->Char;
}
buffer[CD_location->Text->Length] = wchar_t('\0');
fileLoc = buffer;

HTH - M
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top