Ask a questionAsk a question
 

AnswerCall fuction from another form.

  • Wednesday, September 30, 2009 3:07 PMkzona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I would like to call a fuction from another form. For example, in Form1 I read numbers and calculate, and it has a button when i click it open form2. In form2 i want to call the values from form1 and draw it. I tried to include form1 in form2 and call the fuction, but it doesn't work.
    thanks

Answers

  • Wednesday, September 30, 2009 4:42 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Give Form2 a constructor that let's the method in Form1 pass the numbers it should work on.

    Hans Passant.
  • Thursday, October 01, 2009 4:40 AMcrescens2k Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you don't want to go to a school then there are always C++ language books to help you learn.
    Either way you will have to put time and effort into it.
    Then again the description that you have is so accurate it is hard to figure out what it really is there for.

    As a little example which I hope is easier to understand.
    A constructer is there to initialise the class that is all.

    class CMyClass
    {
    private:
      int a;
      int b;

    public:
      CMyClass() //default constructor
      {
        a = 0;
        b = 0;
      }

      CMyClass(int newa, int newb)
      {
        a = newa;
        b = newb;
      }
    };

    int main()
    {
      CMyClass mc; //this creates a CMyClass object calling the default constructor
      CMyClass mc2(1, 2); //this creates a CMyClass object calling the (int, int) constructor

      CMyClass* mc3 = new CMyClass(1,2); //shows how to do it using new

      delete mc3;

      return 0;
    }

    So as you can see from that, the job of the constructor is to set any member variables inside the class to sane values before you start to use it, (in this case a and b). The reason why this happens is who knows what kinds of weird values it will store when you create it. With the second constructor (yes, you can have more than one), you can give the member variables a known set of values when you create it. The constructor doesn't need to have the parameters I gave, so you can have 3 ints, a float and 2 doubles whatever you choose. The important thing is to create the object with the parenthases after it.

    Be careful though, this example is for native C++, it works the same for C++/CLI too, just change the syntax accordingly.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
  • Thursday, October 01, 2009 1:08 AMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Constructors have nothing to do with a GUI.  They are a very basic feature of the C++ language, can't really get more basic than that.  Educate yourself, there are schools that can teach you what you need to know to write code like this.  We can't teach you in a forum.

    Hans Passant.

All Replies

  • Wednesday, September 30, 2009 4:42 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Give Form2 a constructor that let's the method in Form1 pass the numbers it should work on.

    Hans Passant.
  • Wednesday, September 30, 2009 10:07 PMkzona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I don't get it. In Form1, I have for example the fuction:

    void function()
    {
    double aux1=(2*pi)/3;
    array<Point>^ curvePoints = gcnew array<Point>(1000);

        

    for (t=0;t<10;t+=0.0005)
     
    {
     

     
    o=(t*60);

     
    matrizA[0][0]=0.66666667*cos(o);
    matrizA[0][1]=0.66666667*cos(o-aux1);
    matrizA[0][2]=0.66666667*cos(o+aux1);
    matrizA[1][0]=0.66666667*sin(o);
    matrizA[1][1]=0.66666667*sin(o-aux1);
    matrizA[1][2]=0.66666667*sin(o+aux1);
    matrizA[2][0]=0.33333333;
    matrizA[2][1]=0.33333333;
    matrizA[2][2]=0.33333333;
     
     
    matrizB[0][0]=100*cos(2*3.1415*t);
    matrizB[1][0]=100*cos(2*3.1415*t+aux1);
    matrizB[2][0]=100*cos(2*3.1415*t-aux1);

     for (int i=0; i<3; i++)
    {
    for (int k=0; k<1; k++)
    {
    matrizC[i][k] = 0;
    for (j=0; j<3; j++)
    {
    matrizC[i][k] = matrizC[i][k] + matrizA[i][j] * matrizB[j][k];
    }
    }
    }
       V1=matrizC[0][0];
    V2=matrizC[0][1];
    V3=matrizC[0][2];


          curvePoints[t] = Point(cos[V1],V2);
    }
    }

    I want to take "curvePoints" and draw it in form two.

    thanks     

  • Wednesday, September 30, 2009 10:14 PMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Have you figured out yet what a constructor does?

    Hans Passant.
  • Thursday, October 01, 2009 12:51 AMkzona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I find what a constructor does in MSDN (http://msdn.microsoft.com/en-us/library/kstd9k24.aspx), but I'm not understand how I can use it. Also I find some examples, but I could undestand it. Sorry, but I started to use VC++ just some time ago and it's my first time using GUI.
    thanks again
  • Thursday, October 01, 2009 1:08 AMnobugzMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    Constructors have nothing to do with a GUI.  They are a very basic feature of the C++ language, can't really get more basic than that.  Educate yourself, there are schools that can teach you what you need to know to write code like this.  We can't teach you in a forum.

    Hans Passant.
  • Thursday, October 01, 2009 4:40 AMcrescens2k Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Answer
    If you don't want to go to a school then there are always C++ language books to help you learn.
    Either way you will have to put time and effort into it.
    Then again the description that you have is so accurate it is hard to figure out what it really is there for.

    As a little example which I hope is easier to understand.
    A constructer is there to initialise the class that is all.

    class CMyClass
    {
    private:
      int a;
      int b;

    public:
      CMyClass() //default constructor
      {
        a = 0;
        b = 0;
      }

      CMyClass(int newa, int newb)
      {
        a = newa;
        b = newb;
      }
    };

    int main()
    {
      CMyClass mc; //this creates a CMyClass object calling the default constructor
      CMyClass mc2(1, 2); //this creates a CMyClass object calling the (int, int) constructor

      CMyClass* mc3 = new CMyClass(1,2); //shows how to do it using new

      delete mc3;

      return 0;
    }

    So as you can see from that, the job of the constructor is to set any member variables inside the class to sane values before you start to use it, (in this case a and b). The reason why this happens is who knows what kinds of weird values it will store when you create it. With the second constructor (yes, you can have more than one), you can give the member variables a known set of values when you create it. The constructor doesn't need to have the parameters I gave, so you can have 3 ints, a float and 2 doubles whatever you choose. The important thing is to create the object with the parenthases after it.

    Be careful though, this example is for native C++, it works the same for C++/CLI too, just change the syntax accordingly.
    Visit my (not very good) blog at http://c2kblog.blogspot.com/
  • Thursday, October 01, 2009 9:30 PMkzona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    My problem is that I don't have a school where I can learn c++. I didn't find a good portuguese on-line course and I'm not able to make it in english. So, I have to study by myself. I'm reading some articles and I could learn somethings of constructors and your example was helpful. I'm getting to put it in my program, but my problem is when I try to use the fuction from form1 in form2. form1 is ok, but in form2 when I try to use the fuction from form1, I got that it is undeclared. I'm going to continue studing. If you have some advices of articles, I would like to know.
    thanks 
  • Saturday, October 31, 2009 9:32 PMkzona Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I spend some time studing and I got to pass a number from one form to another form, but I'm having a problem, because I just get to pass one number. I used this:

    "Line1" Form1(double test):x1(test)
    {
    this->x1=y1;
    this->x2=y2;
    "Line2" this->label1->Text=y1.ToString();
    "Line3" this->label2->Text=y2.ToString();
    }

    Line2 is OK, it's giving me the number that I wrote, but Line3 give me just the number zero.
    When I try to change the "line1" i got somo errors

    thank you