Sign in to follow this  
RogerThat123

Windows Form Textboxes

Recommended Posts

Im running visual studio 2003. And I have created a windows form application. Designed it in the designer with some textboxes, some buttons, etc. When I click my button, I want the textbox to be set to a char* thats read from my file. My question is how can I set the textbox to that text.


private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
		 {
			 char *szVersion = GetNewVersion(); // read file , returns 1.32 (example) and yes it must be a char* in my case		
			 textBox2->Text = ? // Is this how you set the text? If so how can i convert the char* to the input type of the text box?
		 }

Ty for your help, sorry for stupid questions.

Share this post


Link to post
Share on other sites
It's been a while since I've used C++/CLI, but you might try:


textbox2->Text = gcnew System::String(szVersion);


(edit) Oh wait, you're using old-school Managed C++, otherwise your .Net type *s would be ^s.


textbox2->Text = __gc new System::String(szVersion);


Or something like that. MC++ is truly ghetto.

Share this post


Link to post
Share on other sites
What's with all the people using C++/CLI these days?

Seriously, unless you're writing a managed wrapper around a native library (e.g. like what the SlimDX guys do) there's no reason to be using C++/CLI for anything. I'd rather do GUI stuff in assembly than doing it in C++/CLI!

Share this post


Link to post
Share on other sites
Quote:
Original post by Codeka
What's with all the people using C++/CLI these days?

Seriously, unless you're writing a managed wrapper around a native library (e.g. like what the SlimDX guys do) there's no reason to be using C++/CLI for anything. I'd rather do GUI stuff in assembly than doing it in C++/CLI!


QFE. I only use C++/CLI for wrapper classes to C and only if I absolutely have to. It's way easier to use WinForms in C#. I usually rewrite C code in C# instead unless it's too much of a pain.

Share this post


Link to post
Share on other sites
Hey sorry guys, I have one more question.

So I got everything working now.

But answer this, Is it possible to have another form inside? Like when you click a button on the main form, a new one will pop up. But so I can use another designer to design it?

Currently I just have a message box that pops up when I click the button, but I want to put an image in it.

Share this post


Link to post
Share on other sites
Ok screw that question, this one is more simple I think.

I inserted a picture box into my form, then in the property window I added a picture from my picture album, but when I compile. I get this error

this->pictureBox1->Image = (__try_cast<System::Drawing::Image * >(resources->GetObject(S"pictureBox1.Image")));

the error is

c:\Users\HOMEOFFICE\Documents\Visual Studio Projects\wa\Form1.h(94): error C2039: 'GetObjectA' : is not a member of 'System::Resources::ResourceManager'

The generated code shouldnt be giving me errors should it? Or is there more to do when adding a picture.

Ps. i want to include it as a resource and dont want to rely on it being on my HD

Share this post


Link to post
Share on other sites
Quote:
Original post by RogerThat123
this->pictureBox1->Image = (__try_cast<System::Drawing::Image * >(resources->GetObject(S"pictureBox1.Image")));

the error is

c:\Users\HOMEOFFICE\Documents\Visual Studio Projects\wa\Form1.h(94): error C2039: 'GetObjectA' : is not a member of 'System::Resources::ResourceManager'
You shouldn't be #include'ing <windows.h> in a C++/CLI project - that's what's causing this error (windows.h has a line that #define GetObject GetObjectA).

Just another reason that you shouldn't be doing this sort of thing in C++/CLI...

Share this post


Link to post
Share on other sites
If you're using windows forms, then you should create a C# project.

If you really want to use C++, then use a native framework and stay away from managed code (I personally like wxWidgets, others might recommend Qt or something else).

Share this post


Link to post
Share on other sites
It is possible. As I said, you shouldn't #include <windows.h> in a C++/CLI project. If you take that out, it'll fix that problem (though it might cause other problems...)

Like Promit says in this other recent thread, you should only be using C++/CLI if have a thorough understanding of both C++ and .NET already... do yourself a favour and either switch to a C# project, or use a native GUI library such as wxWidgets.

Share this post


Link to post
Share on other sites
If you are new to programming and wish to make Windows programs, I would suggest that you consider downloading either Visual C# or Visual Basic .NET express editions. Registration is free, and you can use them for any purpose. You will have a much easier time, I think using these either of those than you would C++.

http://www.microsoft.com/express/vcsharp/

http://www.microsoft.com/express/vb/default.aspx

Edit:
I just read above that you don't want to switch to C# because you've already studied some C++. This, I believe, is a very serious mistake. C++ is a very complicated language, and if all you want to do is make some simple programs, you really don't need it. Further more, the same .NET libraries you've been accessing with C++/CLI are able to be used from C# and Visual Basic.

Professional developers use C# and Visual Basic for a wide range of purposes, and there is certainly no shame in doing the same for hobby work.

Share this post


Link to post
Share on other sites
Quote:

I have 2 other files that are included in the project, that are written in C++. Converting to C# would take way too long for me considering I have never touched it.

But you're not writing C++ already. If you're using VS 2003 and you're using Windows Forms, you're using C++ with the Managed C++ extensions enabled. This is fundamentally massively different from C++ -- almost, in fact, as different as C#. And in a much more unpleasant fashion.

You cannot use Windows Forms and C++ together directly. If you want to use C++, use a different windowing toolkit like wxWidgets. If you want to use Windows Forms, use C#.

C++/CLI and C++ w/ Managed C++ extensions are terribly complicated languages that are not suitable for usage by people who did not realize they were using them. If you persist in this direction, you need to understand that you are intentionally taking one of the most difficult paths available to you and that you will need to learn quite a lot about how native and managed interoperability code works.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this