Thread: VS2008 and c++ program

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    307

    VS2008 and c++ program

    making a windows form application, have the buttons and the form made, now i need to do the "progrmming" of the thing...

    let say button1 is labeled add

    when i click button 1, i want it to goto a function, and do something,

    lets say the following is my add function

    Code:
    void add(int &x)
        {
             x++;
             return x;
        }
    where does the "programming" go in the project?

    //edit//

    correction, where does my functions go that i am going to call from button presses? (i am taking a console c++ program i made, and making it GUI)
    Last edited by Crossfire; 01-11-2013 at 05:04 PM.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Not sure quite what you mean -- so apologies if this is too basic.

    VS2008 will have created a Form1.h (or whatever your form is called .h) for you and generated code in there to do what you've drawn in the designed view. If you right click on the form and go to "code view" it'll show the generated code for the GUI. Get back to designer view the same way.

    If you double click on the button, it should take you back to code view and put you inside some generated code, still in Form1.h:

    Code:
    private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
          // Add event handler code here.
    }
    If you look around the generated code, you'll find generated code that sets button1_Click to be the handler for a click on that button. That function will be called every time the button is clicked. IN the properties pane for each control (e.g. button, text box) there's an Events dropdown where you can pick other events (e.g. MouseOver), and VS will generate quite a bit of the code for you.

    So back to your original question....

    Form1.h is just a giant class declaration and definitions. It contains objects for each control (e.g. button) on the form. If you want, you can write all your code in Form1.h. I prefer to put any 'interesting' code in a Form1.cpp file and try to keep Form1.h for code generated by VS.

    You can't use your "add" function directly as an event handler, because they have to have to have the form of the example above (i.e. take Object^ and EventArgs^ as arguments). That can just be a wrapper though, and call straight through to your add function.

    How you organise this depends on how much existing code you have. If you have a program that you want to rewrite as a windows forms program, then functions can go in Form1 -- just declare them in Form1.h and define them in Form1.cpp. Likewise with member variables.

    If you have code that you want to reuse (e.g. algorithmic stuff that is unrelated to the interface) then you can do it in the 'obvious' way. If you have a collection of functions in a cpp file and declarations in a header file, you can #include the header in Form1.h or Form1.cpp, add the cpp file to the project, and use it. If you have a class hierarchy or similar, you can create and manipulate objects in the usual way. Objects that needs to be persistent between event handlers will need to be members of Form1.

    With any amount of reuse, you'll most likely run into language issues (Windows Forms aren't quite C++, they're C++/CLI C++/CLI - Wikipedia, the free encyclopedia, which has some differences) and managed/unmanaged code mixing issues. Plenty of resources about that on the net though.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    yeah i have a single .cpp program i wrote, needing to take all the functions that do mostly math calculations, then uses those numbers to do something...

    ie, click on button 1, calls function to do formula, returns to button, then process the info, button done, then the same for button 2, etc...

    the buttons already send the data that is hardcoded in (ie X already equals 10 so it sends 10 when button clicked)
    i need to have the formula computed by a function (instead of making 320 copies, one for each button)

    could do it 320 times, but rather not!

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Ahh, I gotcha. I think. There must be a reason you have 320 buttons though -- you must be doing 320 different things, right?

    It's easy to tie lots of button click events to a single handler. Something like (might not be exactly right as I don't have MSVC in front of me right now)

    Code:
    System::EventHandler buttonClickEv = gcnew System::EventHandler(this, &test::button1_Click);
    this->button1->Click += buttonClickEv;
    this->button2->Click += buttonClickEv;
    this->button3->Click += buttonClickEv;
    ....
    However.... that's not very nice, still 320 lines of code. You might be able to select all the buttons in designer view and change the click event handler in properties -- in which case VS would generate all that for you.

    The natural solution that springs to mind is to have an array of Buttons. You can declare arrays in C++/CLI with cli::array: Arrays (C++ Component Extensions). I think you'd want an array of managed references (Button^) then a loop to go through the array initialising the buttons. I don't think there's any "nice" way -- but at least would save you having to longhand initialise everything. There's a partial example here: Creating array of buttons in Visual C++

    I think you'd have to write code to set the position and size of each button too. I very much doubt that Designer View will be able to figure out what you've done, so will either get upset and stop working with errors, or display no buttons.

    Assuming you don't really want identical behaviour for 320 buttons, you can read the Object/EventArgs in the event handler and figure out what button was clicked. E.g. get the Button's coordinates and translate that back to something meaningful, or read the Button's text field and do something based on that. Neither of those feel like good robust solutions, but I can't think of anything better off the top of my head.

    Not sure if any of that is good advice - it's the route I'd try to take, but in practice it'll probably be a bit fiddly.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    each button represents a different relay, when the button is clicked, a formula has to be computed, then sent to the external card.

    ie press 1 for relay one, so 1 is sent to the following

    Code:
    int GetBox(int Relay)
    {
     int V = Relay;
     int Box = 1;
     do 
      {
       if (V > 8)
        {
         Box++;
         V = V - 8;
        }
      }while (V > 8 );
     return Box;
    }
    then relay and box is sent to
    Code:
    int GetChannel(int Relay,int Box)
    {
     int Channel = Relay-((Box-1)*8);
     return Channel;
    }
    then channel and box sent to make mask (dont worry, array is being changed also)
    Code:
    void SetMask(int Channel, int Box)
    {
     int MaskHolder;
     switch (Channel)
      {
       case 1:
       MaskHolder = 1;
       break;
     
       case 2:
       MaskHolder = 2;
       break;
       case 3: 
       MaskHolder = 4;
       break;
     
       case 4:
       MaskHolder = 8;
       break;
     
       case 5:
       MaskHolder = 16;
       break;
     
       case 6:
       MaskHolder = 32;
       break;
     
       case 7:
       MaskHolder = 64;
       break;
     
       case 8:
       MaskHolder = 128;
       break;
      }
     Boxes[Box].Word = Boxes[Box].Word ^ MaskHolder;
    }
    then all sent to the relay controller in a 6 pass write set up...

    Code:
    void SetRelays(int Box)
    {
     if (Simulation == 0)
      {
       int Bank,BankBox;
       if(Box >= 1 && Box <= 8){ Bank = 8; BankBox = Box-1; }
       if(Box >= 9 && Box <= 16){ Bank = 16; BankBox = Box-9; } 
       if(Box >= 17 && Box <= 24){ Bank = 32; BankBox = Box-17; }
       if(Box >= 25 && Box <= 32){ Bank = 64; BankBox = Box-25; }
       if(Box >= 33 && Box <= 40){ Bank = 128; BankBox = Box-33; }
       
       WriteAllDigital(Boxes[Box].Word);
       SetAnalogChannel(1);
       ClearAnalogChannel(1);
       WriteAllDigital(Bank+BankBox);
       SetAnalogChannel(2);
       ClearAnalogChannel(2);
       ClearAllDigital();
      }
    }
    i want to call these functions, instead of putting all of this onto each button!

    dont want all this code, 320 times!

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    307
    OK bagging windows forms, doing much more, and better in QT C++ Gui App.

    ok if i include a source code in each form, will the form use the function calls to the "added" source?

    AND lets say i get X in one form, how do i send it to the next form?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. VS2010 vs VS2008
    By zacs7 in forum General Discussions
    Replies: 29
    Last Post: 06-13-2010, 02:40 AM
  2. Configuring VS2008 C++ to C
    By Akkernight in forum C Programming
    Replies: 8
    Last Post: 02-28-2009, 04:17 AM
  3. C->Cdll->C#dll under VS2008
    By Opariti in forum C Programming
    Replies: 2
    Last Post: 01-27-2009, 04:53 AM
  4. MASM In VS2008
    By valaris in forum Tech Board
    Replies: 1
    Last Post: 01-13-2009, 09:11 PM
  5. Need VS2008 Verification
    By rags_to_riches in forum C++ Programming
    Replies: 7
    Last Post: 10-14-2008, 04:51 AM