Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / September 2007

Tip: Looking for answers? Try searching our database.

How to assign a 'char *' to a text property of a control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Steven - 28 Aug 2007 22:50 GMT
Hi, I'm new at Visual C++ and I can't figure out how to change the text
property of a label.  I'm using a char[] variable that I'd like to assign to
the label.  Perhaps I should use a System::String^ instead? But when I try
that, I get an error that I can't use a System::String^ as a global or static
variable.  What I want to be able to do is the change say the third character
of the text in the text property of the label control from, say, 'k' to 'm'.  
Maybe there's a very simple way to do this.
Ben Voigt [C++ MVP] - 29 Aug 2007 20:21 GMT
> Hi, I'm new at Visual C++ and I can't figure out how to change the text
> property of a label.  I'm using a char[] variable that I'd like to assign
[quoted text clipped - 7 lines]
> 'm'.
> Maybe there's a very simple way to do this.

There's a String constructor that accepts a C++ char (.NET SByte), so:

char* the_text = "Some ordinary string here."
label1->Text = gcnew String(the_text);

However, that's going to do an ASCII->Unicode conversion.  You may be
happier using .NET Char (C++ wchar_t) arrays.  To do that you have to write
your string literals like L"string" and character literals like L'm'.

Getting to individual (Unicode) characters of .NET Strings is easy:

interior_ptr<wchar_t> unistr = PtrToStringChars(label->Text);

To do the simple replacement you mentioned, you might do:

array<wchar_t>^ s = label->Text->ToCharArray();
if (s[2] == L'k') s[2] == L'm';
label->Text = new String(s);
Steve Alpert - 01 Sep 2007 20:01 GMT
You don't say what environment you are using?  Is this managed or unmanaged
code?  If managed, look at Ben Voigt's reply.  If not, you are dealing with
window handles.  Now, are you doing raw C++, ATL, WTL, or MFC?  If the former,
investigate SetWindowText(hwnd, "here is a string").

/steveA

> Hi, I'm new at Visual C++ and I can't figure out how to change the text
> property of a label.  I'm using a char[] variable that I'd like to assign to
[quoted text clipped - 3 lines]
> of the text in the text property of the label control from, say, 'k' to 'm'.  
> Maybe there's a very simple way to do this.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.