Click to See Complete Forum and Search --> : Windows form app add variable to textbox


suncica2222
March 6th, 2010, 02:06 PM
I made GUI in VC++2008 Win form app template and I dont know how to assign string or int variable to a textbox that I made.There's no variable field in text field properties and no add variable on right click.I haven't found any.

All that I found in Form.h is :

private: System::Windows::Forms::TextBox^ textBox2;
private: System::Windows::Forms::TextBox^ textBox3;
private: System::Windows::Forms::TextBox^ textBox4;
private: System::Windows::Forms::TextBox^ textBox5;
private: System::Windows::Forms::TextBox^ textBox6;
private: System::Windows::Forms::Button^ button1;
private: System::Windows::Forms::TextBox^ textBox1;

I guess those are just made for panel and are not supposed to be casted into strings or types I need.

This should be an easy question.

cilu
March 6th, 2010, 03:24 PM
A TextBox has a property of type string called Text. Convert the value you want to display and assigned it to this property. For instance:

int i = 42;
textBox1->Text = i.ToString();

cilu
March 6th, 2010, 03:24 PM
[ redirected ]

suncica2222
March 6th, 2010, 03:46 PM
Thanx cilu!

So if I have textbox and user types email@yahoo.com in, I will read that and store it in char * variable like this:

char* emailVar;
emailVar=textBox1->Text.toChar();

??? I gues I missed types cast func???

and second question:

what lib do I need to include to use UpdateData(); ???

Alex F
March 7th, 2010, 03:47 AM
If you decided from some reason to write C++/CLI GUI application, don't mix managed and unmanaged types if this is not necessary. To get a textbox text, use String variable:

String^ s = textBox1->Text;

Windows form doesn't have UpdateData function. You need to get/set Text property for every textbox on the form to emulate MFC UpdateData behavior.

suncica2222
March 7th, 2010, 04:16 AM
what confuses me is that as I see it Ill should write all code in button clicked function in Form.h file ???

like this:

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
emailTo=this->textBox1->Text;
MessageBox::Show(emailTo,"caption");
char a[]= "kljlkj";
//UpdateData(true);
}


coz when I make new class or write it in main.cpp I can not reference Form or textboxes...???

Alex F
March 7th, 2010, 07:36 AM
C++/CLI designer produces ugly inline code, because the same program works also for C# and VB .NET. There is also Form1.cpp file, which contains only:
#include "Form1.h"

You can move implementation to .cpp file according to general C++ rules.