Hello,
I'm new to gui programming althou i've done things before in c++ i've always encountered this error and never really tried to fix it.
Lets for say I have a counter label and a button that says "Start" when one clicks start the label changes values from 0 to 999 (does't matter how fast) . SO i have an Event click in my Form1.h (all generated by VS 05) that calls function Count and the functions counts as long as global varaible is set to false .
the only way this global variable will be false is if the persons clicks the event again (or any other button to stop) and the count should stop where it is if the variable goes false and the varaible is turned back on when the start is pressed.
basicly my point being i wrote the program but the gui doesn't take event in the middle of my loop or in other words i want it to run in the background so i can do my other stuff in meanwhile also
like be able to click more buttons that do other stuff..
Here is my .h class files generated by VS ++
#pragma once
#include <iostream>
#include <windows.h>
bool glbl = false;
namespace Test {
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
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: Add the constructor code here
//
}
void count10(){
for(int i=0;i<1000;i++){
if(glbl == true){
this->lblCount->Text=L"Counting: "+i;
this->Refresh();
}else
break;
}
}
protected:
/// <summary>
/// Clean up any resources being used.
/// </summary>
~Form1()
{
if (components)
{
delete components;
}
}
public: System::Windows::Forms::Button^ btnStart;
public: System::Windows::Forms::Label^ lblCount;
public:
/// <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->btnStart = (gcnew System::Windows::Forms::Button());
this->lblCount = (gcnew System::Windows::Forms::Label());
this->SuspendLayout();
//
// btnStart
//
this->btnStart->Location = System::Drawing::Point(12, 42);
this->btnStart->Name = L"btnStart";
this->btnStart->Size = System::Drawing::Size(75, 23);
this->btnStart->TabIndex = 0;
this->btnStart->Text = L"Start";
this->btnStart->UseVisualStyleBackColor = true;
this->btnStart->Click += gcnew System::EventHandler(this, &Form1::btnStart_Click);
//
// lblCount
//
this->lblCount->AutoSize = true;
this->lblCount->Location = System::Drawing::Point(29, 9);
this->lblCount->Name = L"lblCount";
this->lblCount->Size = System::Drawing::Size(35, 13);
this->lblCount->TabIndex = 1;
this->lblCount->Text = L"label1";
//
// Form1
//
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(115, 86);
this->Controls->Add(this->lblCount);
this->Controls->Add(this->btnStart);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
public: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) {
if(glbl == false){
glbl=true;
this->btnStart->Text="Stop";
count10();
glbl=false;
this->btnStart->Text="Start";
}else{
this->btnStart->Text="Start";
glbl=false;
}
}
};
}
And here is my .CPP file
// Test.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
using namespace Test;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Test::Form1 f1;
f1.ShowDialog();
// Create the main window and run it
//Application::Run(gcnew Form1());
return 0;
}
Thanks for help. I think i'm missing the basic concept of events.
Axar