| | |
Gui, Visual Studio 05, Loop + Events + Stuck
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
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 ++
And here is my .CPP file
Thanks for help. I think i'm missing the basic concept of events.
Axar
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 ++
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
// 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
Last edited by 0o0; Mar 28th, 2009 at 4:46 pm.
•
•
•
•
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
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
So, i created a worker that im guessing suppose to work
Now the problem is that when i'm doing my "WORK" (count function) i'm suppose to change the label values however, the program doesn't do anything there and exists worker when i try to change the value of my label. i checked with break point and all my form values are Error : Cannot obtain value written in them.
thanks once again.
Now the problem is that when i'm doing my "WORK" (count function) i'm suppose to change the label values however, the program doesn't do anything there and exists worker when i try to change the value of my label. i checked with break point and all my form values are Error : Cannot obtain value written in them.
thanks once again.
C++ Syntax (Toggle Plain Text)
#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 /// /// '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 // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~Form1() { if (components) { delete components; } } private: System::Windows::Forms::Button^ btnStart; private: System::Windows::Forms::Label^ lblCount; private: System::ComponentModel::BackgroundWorker^ backgroundWorker1; private: /// <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->backgroundWorker1 = (gcnew System::ComponentModel::BackgroundWorker()); // Create backgroundWorker1 // this->backgroundWorker1->DoWork += gcnew DoWorkEventHandler(this, &Form1::Worker); this->btnStart = (gcnew System::Windows::Forms::Button()); this->lblCount = (gcnew System::Windows::Forms::Label()); this->SuspendLayout(); // // btnStart // this->btnStart->Location = System::Drawing::Point(46, 61); 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(43, 24); 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(177, 118); 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 private: System::Void btnStart_Click(System::Object^ sender, System::EventArgs^ e) { //Add Worker Thread Handler this->backgroundWorker1->DoWork += gcnew DoWorkEventHandler(this, &Form1::Worker); if(glbl == false){ glbl=true; this->btnStart->Text="Stop"; this->backgroundWorker1->RunWorkerAsync(); }else{ this->btnStart->Text="Start"; glbl=false; } } public: private: System::Void Worker(System::Object^ sender, System::ComponentModel::DoWorkEventArgs^ e) { count(); } void count(){ for(int i=0;i<1000;i++){ if(glbl == true){ this->lblCount->Text=L"Counting: "+i; }else break; } this->btnStart->Text="Start"; glbl=false; } }; }
Last edited by 0o0; Mar 29th, 2009 at 6:14 pm.
No, you won't be able to update a GUI from a worker by calling it directly. You need to use message passing. i.e. call SendMessage() or PostMessage() to send a user defined message to the GUI thread. You'll need to catch the message in your GUI thread and do your GUI stuff.
•
•
Join Date: Mar 2009
Posts: 6
Reputation:
Solved Threads: 0
I was looking at background workers and came across this auricle
http://www.codeproject.com/KB/cpp/Ba...r_Threads.aspx
the Author has two programs
Synchronous Example and Asynchronous
in Synchronous method he does in C#
im wondering if there is a way to do this in C++?
http://www.codeproject.com/KB/cpp/Ba...r_Threads.aspx
the Author has two programs
Synchronous Example and Asynchronous
in Synchronous method he does in C#
c# Syntax (Toggle Plain Text)
m_fmProgress.lblDescription.Invoke((MethodInvoker) delegate() { m_fmProgress.lblDescription.Text = "Processing file " + i.ToString() + " of " + iCount.ToString(); m_fmProgress.progressBar1.Value = Convert.ToInt32( i * ( 100.0 / iCount ) ); });
im wondering if there is a way to do this in C++?
![]() |
Other Threads in the C++ Forum
- Previous Thread: parsing of text document into sentences
- Next Thread: input, search, output
Views: 627 | Replies: 6
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary bitmap c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll encryption error file forms fstream function functions game getline givemetehcodez google graph homeworkhelper iamthwee ifstream input int integer java lazy lib linkedlist linux loop looping loops map math matrix memory microsoft newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort string strings struct studio system template templates test text tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






