Changing the Text In a Text Box

  • Thread starter Thread starter Steve Behman
  • Start date Start date
S

Steve Behman

In Visual C++ 8.0 Express I am trying to programmatically change the contents
of a text box.

In form1.h I find the initializing code _PAYEE->Text=L"a";.

I assumed that the type of "a" in this expression is const char[] (which
may be a drastic error). Be that as it may I have tried every conceivable
datatype on the right side of _PAYEE->Text=L"a"; each one of which generates
a compiler error saying something to the effect that it cannot find a
suitable conversion to:

_PAYEE->Text.

What is the datatype of the left side and how can I construct an object of a
type to assign to it?

Alternatively, is there another way (beside the = operator) to change the
text that appears in the box to what I want it to be?

BTW: What in He** does the 'L' mean on the right side?
 
Steve said:
In Visual C++ 8.0 Express I am trying to programmatically change the contents
of a text box.

In form1.h I find the initializing code _PAYEE->Text=L"a";.

I assumed that the type of "a" in this expression is const char[] (which
may be a drastic error). Be that as it may I have tried every conceivable
datatype on the right side of _PAYEE->Text=L"a"; each one of which generates
a compiler error saying something to the effect that it cannot find a
suitable conversion to:

_PAYEE->Text.

What is the datatype of the left side and how can I construct an object of a
type to assign to it?

Alternatively, is there another way (beside the = operator) to change the
text that appears in the box to what I want it to be?

BTW: What in He** does the 'L' mean on the right side?

Steve:

All strings in .NET are (wide) 16-bit (wchar_t in C++) strings, not 8-bit (char)
strings. The L"" denotes a wide string.

I am no expert on .NET, but I think that the type you are looking for is
System::String.
 
David,

Thank you for the response.

I tried several variants involving System::String (some of them involving
"^") but none of them worked.

I have a massive amount of code to manipulate an SLT string object that I
would like to salvage. Would you (or anyone else) be so kind as to provide
me with the code it takes to make the assignment of an SLT string object to a
System::String object and that which is required to do the assignment in the
opposite direction?



David Wilkinson said:
Steve said:
In Visual C++ 8.0 Express I am trying to programmatically change the contents
of a text box.

In form1.h I find the initializing code _PAYEE->Text=L"a";.

I assumed that the type of "a" in this expression is const char[] (which
may be a drastic error). Be that as it may I have tried every conceivable
datatype on the right side of _PAYEE->Text=L"a"; each one of which generates
a compiler error saying something to the effect that it cannot find a
suitable conversion to:

_PAYEE->Text.

What is the datatype of the left side and how can I construct an object of a
type to assign to it?

Alternatively, is there another way (beside the = operator) to change the
text that appears in the box to what I want it to be?

BTW: What in He** does the 'L' mean on the right side?

Steve:

All strings in .NET are (wide) 16-bit (wchar_t in C++) strings, not 8-bit (char)
strings. The L"" denotes a wide string.

I am no expert on .NET, but I think that the type you are looking for is
System::String.
 
Steve said:
David,

Thank you for the response.

I tried several variants involving System::String (some of them involving
"^") but none of them worked.

I have a massive amount of code to manipulate an SLT string object that I
would like to salvage. Would you (or anyone else) be so kind as to provide
me with the code it takes to make the assignment of an SLT string object to a
System::String object and that which is required to do the assignment in the
opposite direction?

Steve:

SLT?

<http://en.wikipedia.org/wiki/SLT>

I just meant that

System::String testString^ = L"a";
_PAYEE->Text = testString;

surely works.
 
David said:
SLT?

<http://en.wikipedia.org/wiki/SLT>

I just meant that

System::String testString^ = L"a";
_PAYEE->Text = testString;

surely works.

Steve:

Oh, maybe you meant STL? Actually, std::string was never part of the STL; though
the old STL and std::string are both part of the C++ Standard Library.

I think you can do

std::string stdString = "a";
String^ testString = gcnew String(stdString.c_str());
 
David,

The example you give does work however, that is not quite my problem: The
example assigns the value of a *constant* object to a System::String object
while what I must ultimately accomplish is to assign a *variable* object of
type STL (sorry, I got my metters lixed :-) "string" to _PAYEE->Text. I also
need to retrieve System::String objects to transform them into "string"
objects which can be manipulated with my existing code.

I hope that this clarifies the issue.
 
David, thanks again.

This is what I tried:

std::string teststr = "a";
String^ PAYEE->Text = gcnew String(teststr.c_str());

And the result was:
"error C2143: syntax error : missing ';' before '->' "

Only some of which I truly understand!
 
Steve said:
David, thanks again.

This is what I tried:

std::string teststr = "a";
String^ PAYEE->Text = gcnew String(teststr.c_str());

Steve:

std::string teststr = "a";
_PAYEE->Text = gcnew String(teststr.c_str());
 
David,

Thank you so much it really did the trick!

In order to make it more useful (to me) I created a preprocessor macro:

#define str_to_Str (s) (gcnew String((s).c_str()) )

There remain two questions:

1) Is there some sort of destructor needed in association with gcnew?

2) What code is needed to transform a String to an STL string?

Thanks again,
 
Steve said:
David,

Thank you so much it really did the trick!

In order to make it more useful (to me) I created a preprocessor macro:

#define str_to_Str (s) (gcnew String((s).c_str()) )

There remain two questions:

1) Is there some sort of destructor needed in association with gcnew?

2) What code is needed to transform a String to an STL string?

Steve:

1. Managed objects created with gcnew do not need deleting. The memory is
reclaimed by the garbage collector (gc) when it is no longer being used.

2. Information on Inter-op between managed and unmanaged C++ may be found here

http://msdn.microsoft.com/en-us/library/2x8kf7zx(VS.80).aspx

However, before you go on, you should be sure that learning C++/CLI is the way
you want to go. Microsoft is no longer promoting C++/CLI for writing .NET GUI
applications. For most people (including even experienced C++ developers), C# is
the way yo go if you want to target the .NET framework.
 
David, thanks once again.

To be honest I had absolutely no idea that I was dealing with .NET and
perhaps, had I known, I would not have undertaken working with it.

I especially appreciated the pointer to the full discussion of these issues.
There are many topics contained therein that will help me develop my
programs without having to run to this forum for answers. I also appreciate
your patience in providing me with this help.

Below is an excerpt from a previous post of mine in this forum. I believe
it will explain where I'm coming from vis-à-vis learning new tools, reading
books and learning new languages.

=================================================
I appreciate your suggestion. I have been programming for 55 years in
about as many languages as years, I'm afraid that one more language would be
the straw that breaks this old camel's back.

If it was not for programs called ZoomText and Dragon NaturallySpeaking I
couldn't read or write anything at all -- but they do not work with paper.

Most of my own C++ programming was for OS/2 for which I developed the
equivalent of the MFC (with an interface to DB/2 using the IBM equivalent of
the SDK.

As a consequence of the above I will do piecewise development of my
application with the Visual Studio tool and then port them to the MFC for
integration into a whole application (that is if I live that long :-).
============================================


That having been said, below is my (perhaps naïve) resolution for getting
back and forth between std:string and System:: String. It puts me in a place
I understand and salvages a whole bunch of my old code.



using namespace Runtime::InteropServices;

#define toStr(a) (gcnew String((a).c_str()))


string tostr( String ^ s)
{
int i=0;
string out="";
wstring os;
const wchar_t* chars = (const
wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars));
while( i<s->Length ) out += s[ i++ ];
return out;
}
 
Steve said:
David, thanks once again.

To be honest I had absolutely no idea that I was dealing with .NET and
perhaps, had I known, I would not have undertaken working with it.

I especially appreciated the pointer to the full discussion of these issues.
There are many topics contained therein that will help me develop my
programs without having to run to this forum for answers. I also appreciate
your patience in providing me with this help.

Below is an excerpt from a previous post of mine in this forum. I believe
it will explain where I'm coming from vis-à-vis learning new tools, reading
books and learning new languages.

=================================================
I appreciate your suggestion. I have been programming for 55 years in
about as many languages as years, I'm afraid that one more language would be
the straw that breaks this old camel's back.

If it was not for programs called ZoomText and Dragon NaturallySpeaking I
couldn't read or write anything at all -- but they do not work with paper.

Most of my own C++ programming was for OS/2 for which I developed the
equivalent of the MFC (with an interface to DB/2 using the IBM equivalent of
the SDK.

As a consequence of the above I will do piecewise development of my
application with the Visual Studio tool and then port them to the MFC for
integration into a whole application (that is if I live that long :-).
============================================


That having been said, below is my (perhaps naïve) resolution for getting
back and forth between std:string and System:: String. It puts me in a place
I understand and salvages a whole bunch of my old code.



using namespace Runtime::InteropServices;

#define toStr(a) (gcnew String((a).c_str()))


string tostr( String ^ s)
{
int i=0;
string out="";
wstring os;
const wchar_t* chars = (const
wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars));
while( i<s->Length ) out += s[ i++ ];
return out;
}

Steve:

Just one point here. Why yo you define toStr() as a macro, and tostr() as a
function? Macros should be avoided unless necessary; why not just make them both
functions?

As to your overall strategy, I am a bit confused. If your ultimate goal is to do
with MFC, why are you messing with C++/CLI?

And if you think you might like to go with .NET, it is really much better to
learn C#. Even apart form the fact that MS does not promote it for writing
entire .NET applications, C++/CLI is essentially standard C++ and C# combined
together in one complex language. Yes, it uses C++ syntax (:: for scoping, ->
rather than . for reference class objects, ...), but you still have to learn
essentially everything you would learn in C#.

If you like C++, and you are not too concerned with jumping on the .NET
bandwagon, then I would go with MFC, or maybe some other native C++ library like
WTL, QT, wxwidgets.
 
That having been said, below is my (perhaps naïve) resolution for
getting back and forth between std:string and System:: String. It
puts me in a place I understand and salvages a whole bunch of my old
code.

How about the "Really Easy Way":
http://msdn.microsoft.com/en-us/library/bb384859.aspx
http://msdn.microsoft.com/en-us/library/bb384865.aspx
using namespace Runtime::InteropServices;

#define toStr(a) (gcnew String((a).c_str()))


string tostr( String ^ s)
{
int i=0;
string out="";
wstring os;
const wchar_t* chars = (const
wchar_t*)(Marshal::StringToHGlobalUni(s)).ToPointer();
os = chars; Marshal::FreeHGlobal(IntPtr((void*)chars));
while( i<s->Length ) out += s[ i++ ];
return out;
}



David Wilkinson said:
Steve:

1. Managed objects created with gcnew do not need deleting. The
memory is reclaimed by the garbage collector (gc) when it is no
longer being used.

2. Information on Inter-op between managed and unmanaged C++ may be
found here

http://msdn.microsoft.com/en-us/library/2x8kf7zx(VS.80).aspx

However, before you go on, you should be sure that learning C++/CLI
is the way you want to go. Microsoft is no longer promoting C++/CLI
for writing .NET GUI applications. For most people (including even
experienced C++ developers), C# is the way yo go if you want to
target the .NET framework.
 
Back
Top