Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Managed C++/CLI
  4. Newbie: About passing parameter into a form

Newbie: About passing parameter into a form

Scheduled Pinned Locked Moved Managed C++/CLI
helpc++tutorial
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kkyeung
    wrote on last edited by
    #1

    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.

    G 1 Reply Last reply
    0
    • K kkyeung

      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.

      G Offline
      G Offline
      georgeraafat
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups