Newbie: About passing parameter into a form
-
I have the following code, and I want to pass the variable test1 into the form, so that the form can read my test1 value. The var will be passed to SetParameter, a method under Form1. But I've encounted the following problem: error C2664: 'void System::Windows::Forms::TextBox::set_Text(System::String __gc *)' : cannot convert parameter 1 from 'int' to 'System::String __gc *' error C2039: 'SetParameter' : is not a member of 'System::Windows::Forms::Form' Form1.cpp main:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { int test1; string line; System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA; ifstream iFile("test.txt"); if (!iFile) { //error msg return -1; } iFile >> test1; iFile.close(); Form *Form1 = new Form(); Form1->SetParameter(test1); //put parameter into form Application::Run(Form1); //run app return 0; }
Form1.h:public __gc class Form1 : public System::Windows::Forms::Form { public: Form1() { InitializeComponent(); } void SetParameter(int t){ textBox1->Text = t; } protected: void Dispose(Boolean disposing)...... ..... ..... .....
Actually I dunno where should I put the SetParameter, and how to convert int "t" to __gc textbox text as shown in the method.(since compiling the method results the first error) Million thanks again. -
I have the following code, and I want to pass the variable test1 into the form, so that the form can read my test1 value. The var will be passed to SetParameter, a method under Form1. But I've encounted the following problem: error C2664: 'void System::Windows::Forms::TextBox::set_Text(System::String __gc *)' : cannot convert parameter 1 from 'int' to 'System::String __gc *' error C2039: 'SetParameter' : is not a member of 'System::Windows::Forms::Form' Form1.cpp main:
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { int test1; string line; System::Threading::Thread::CurrentThread->ApartmentState = System::Threading::ApartmentState::STA; ifstream iFile("test.txt"); if (!iFile) { //error msg return -1; } iFile >> test1; iFile.close(); Form *Form1 = new Form(); Form1->SetParameter(test1); //put parameter into form Application::Run(Form1); //run app return 0; }
Form1.h:public __gc class Form1 : public System::Windows::Forms::Form { public: Form1() { InitializeComponent(); } void SetParameter(int t){ textBox1->Text = t; } protected: void Dispose(Boolean disposing)...... ..... ..... .....
Actually I dunno where should I put the SetParameter, and how to convert int "t" to __gc textbox text as shown in the method.(since compiling the method results the first error) Million thanks again.You need to convert the 'int i' to a C string. You could use itoa() or printf(). Make sure the buffer is large enough to hold the resulting string. Once you have a C string, convert it to a 'System::String'. You don't need to convert it to __gc textbox because you're assigning it to the 'Text' property of the text box... The property expects a string. gmileka