Newbie: About passing parameter into a form

Discussion in 'C' started by kkyeung, May 7, 2006.

  1. kkyeung

    kkyeung New Member

    Joined:
    May 3, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    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:
    Code:
    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:

    Code:
    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.
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    You are creating an object of Type Form Form *Form1 = new Form(); but giving it just a name as Form1 but your class name is Form1 public __gc class Form1 : public System::Windows::Forms::Form and so SetParameter is a member of Form1 class and not Form class and thats why the error.
     
  3. kkyeung

    kkyeung New Member

    Joined:
    May 3, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    then, how could I change it in order to let the outside main see the SetParameter method and use it? thanks.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    change Form *Form1 = new Form(); to Form1 *frm = new Form1();
     
  5. kkyeung

    kkyeung New Member

    Joined:
    May 3, 2006
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    OK now, thanks a lot!
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    My pleasure.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice