CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: C++ Windows forms with .cpp

Threaded View

  1. #1
    Join Date
    May 2009
    United States of America

    Question C++ Windows forms with .cpp

    I am still trying to learn how to make windows form applications in visual studio. I have created a very basic windows forms application. But, I am not sure how to call my functions from the header to use for when the button is pushed. There seems to be very little reference tutorials about using the windows form application in visual studio.

    As you can see I have a very simple window with a button named "button1" and a label named "label1".

    What I want to do is when you click the button it calls the function which will add die1 and die2 together in my random_dice_wfa.cpp part of my code. Which will then return the total dice roll number to be displayed in the label as the answer.

    #pragma once
    namespace random_dice_wfa {
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	/// <summary>
    	/// Summary for Form1
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class Form1 : public System::Windows::Forms::Form
    			//TODO: Add the constructor code here
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    			if (components)
    				delete components;
    	private: System::Windows::Forms::Button^  button1;
    	private: System::Windows::Forms::Label^  label1;
    		/// <summary>
    		/// Required designer variable.
    		/// </summary>
    		System::ComponentModel::Container ^components;
    #pragma region Windows Form Designer generated code
    		/// <summary>
    		/// Required method for Designer support - do not modify
    		/// the contents of this method with the code editor.
    		/// </summary>
    		void InitializeComponent(void)
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->label1 = (gcnew System::Windows::Forms::Label());
    			// button1
    			this->button1->Location = System::Drawing::Point(13, 13);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(75, 23);
    			this->button1->TabIndex = 0;
    			this->button1->Text = L"Roll Dice";
    			this->button1->UseVisualStyleBackColor = true;
    			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// label1
    			this->label1->AutoSize = true;
    			this->label1->Location = System::Drawing::Point(13, 43);
    			this->label1->Name = L"label1";
    			this->label1->Size = System::Drawing::Size(67, 13);
    			this->label1->TabIndex = 1;
    			this->label1->Text = L"[ Dice Data ]";
    			// Form1
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Name = L"Form1";
    			this->Text = L"Random Dice";
    #pragma endregion
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    				 int die1 = 5;
    				 int die2 = 2;
    				 int answer = mathadd(die1,die2);
    				 label1->Text = String::Format(L"You rolled: {0} and {1} | Total = {2}", die1, die2, answer);
    // random_dice_wfa.cpp : main project file.
    #include "stdafx.h"
    #include "Form1.h"
    using namespace random_dice_wfa;
    int main(array<System::String ^> ^args)
    	// Enabling Windows XP visual effects before any controls are created
    	// Create the main window and run it
    	Application::Run(gcnew Form1());
    	return 0;
    // Dice adding math
    int mathadd(int die1, int die2){
    	int answer = die1 + die2;
    	return answer;
    Last edited by Riven; September 7th, 2009 at 12:58 AM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)