私は中級のC++プログラマーです。しかし、VisualStudioGUIプログラミングは初めてです。他のユーザーにも発生する以前のエラーを実際に検索したと思いますが、それ以上先に進むことはできません。さらに助けてくれてありがとう。
小さな画面で連続カウンターを見たいだけです。スタートボタンを押すと、ストップボタンが押されるまでカウンターがスタートしなければなりません。
フォームプロジェクトをデバッグするとウィンドウが表示されますが、[スタート]ボタンを押すとエラーが発生します。
An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on.
/ * * /のコード部分の選択も試しましたが、やはりやりたいことができません。
#include <windows.h>
using namespace System;
using namespace System::Threading;
int formcounter, xx;
#pragma once
namespace denemeform {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
formcounter=0;
}
protected:
~Form1()
{
if (components)
{
delete components;
}
}
private: System::Windows::Forms::Button^ button1;
protected:
private: System::Windows::Forms::Button^ button2;
private: System::Windows::Forms::TextBox^ textBox1;
private:
System::ComponentModel::Container ^components;
#pragma region Windows Form Designer generated code
void InitializeComponent(void)
{
this->button1 = (gcnew System::Windows::Forms::Button());
this->button2 = (gcnew System::Windows::Forms::Button());
this->textBox1 = (gcnew System::Windows::Forms::TextBox());
this->SuspendLayout();
this->button1->Location = System::Drawing::Point(27, 24);
this->button1->Name = L"button1";
this->button1->Size = System::Drawing::Size(75, 23);
this->button1->TabIndex = 0;
this->button1->Text = L"&Start";
this->button1->UseVisualStyleBackColor = true;
this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
this->button2->Location = System::Drawing::Point(166, 24);
this->button2->Name = L"button2";
this->button2->Size = System::Drawing::Size(75, 23);
this->button2->TabIndex = 1;
this->button2->Text = L"Sto&p";
this->button2->UseVisualStyleBackColor = true;
this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
this->textBox1->Location = System::Drawing::Point(82, 71);
this->textBox1->Name = L"textBox1";
this->textBox1->Size = System::Drawing::Size(100, 20);
this->textBox1->TabIndex = 2;
this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(269, 139);
this->Controls->Add(this->textBox1);
this->Controls->Add(this->button2);
this->Controls->Add(this->button1);
this->Name = L"Form1";
this->Text = L"Form1";
this->ResumeLayout(false);
this->PerformLayout();
}
#pragma endregion
void Test() //test code:: when "Start" is pushed, this code is what executes
{
int formcounter=0; //test variable
while(xx)
{
formcounter++;
this->textBox1->Text = formcounter.ToString(); //sets first textbox to 'a'
Sleep(100); //pause for 1 second before continuing
}
/*formcounter++;
if (this->InvokeRequired)
this->Invoke(gcnew MethodInvoker(this, &Form1::Test));
else
this->textBox1->Text = formcounter.ToString();
Sleep(1000);*/
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
xx=1;
/*Thread^ oThread = gcnew Thread( gcnew ThreadStart( this, &Form1::Test ) );
oThread->Start();*/
Thread^ tThread = gcnew Thread(gcnew ThreadStart(this, &Form1::Test));
tThread->Start();
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
xx=0;
}
};
}