トップ回答者
Windowsフォーム間でのデータのやり取りの仕方

質問
-
Visual C++ 2005 Express (C++/CLI)を使用して,Windowsプログラムを作っております.
メインフォーム(Form1.h)で確保した配列を別のフォーム(Grah.h)で使用し,グラフ化したいと考えております.
そこで,配列を別のフォーム(Grah.h)でデータを受け取るには,どのようにすればよいのでしょうか?
下記のサイトに近い質問がありましたが,回答が得られませんでした.
http://forums.microsoft.com/MSDN-JA/ShowPost.aspx?PostID=642739&SiteID=7&pageid=0#645913
とある本(日経BPソフトプレス Visual C++2005 アプリケーション開発入門)では,
クラスを作り,プロパティ設定を使い,get() set()を作るのようですが,
別のフォーム(Graph.h)に出来上がったクラスにプロパティ設定を行うことで,2つのフォーム間でデータ(配列)を
やり取りすることができるのでしょうか?
Private:
array<double>^ S11log ;
int i,t,h;
double start,stop;public:
/*プロパティ設定*/
property array<double>^S11log {
array<double> get () { teturn this -> S11log;}
void set ( array<double> value ) { this -> S11log = value; }
}
すべての返信
-
ご返答ありがとうございます.
今,試してみたのですが, 多重に定義されていますとエラーを出してしまいました.
メイン(Form1.h)にPublic として,配列を定義した場合, サブ(Graph.h)で新たに,プロパティで設定すると駄目なのでしょうか?
このように書いて,ビルドしました.
メイン(Form1.h)
public:
array<double>^ S11log;
サブ(Graph.h)
private:
array<double>^ S11log; <-が不要?
property array<double>^ S11log {
array<double>^ get(){retrun this-> S11log;}
void set ( array<double>^ value ) { this -> S11log = value; }
}
publicとprivateの認識は,各クラス間をまたいで使用できるのがpublicでく
クラス内での使用がprivateの認識でよいのでしょうか?
-
プロパティ名と、メンバ変数名が同じではどちらか区別がつかなくなるのでエラーになります。
一般的には、公開しないメンバ変数名を変えます。
たとえば、
private:
array<double>^ S11log_;
public:
property array<double>^ S11log
{
array<double>^ get(){ return this->S11log_; }
void set(array<double>^ value) { this->S11log_ = value; }
}
てな感じで。
MFCっぽく m_S11log; としてもいいですけど。
-
ご回答ありがとうございます.
プロパティの定義を行うため, メインフォーム(Form1.h)上に下記の形と同様に記載し,ビルドしたところ,
プロパティの定義数だけの数が 再定義されましたとエラーを出してしまいます.
コンパイル時に何か設定が必要なのでしょうか?
ご回答お願いします.
蒼の洞窟 さんからの引用 プロパティ名と、メンバ変数名が同じではどちらか区別がつかなくなるのでエラーになります。
一般的には、公開しないメンバ変数名を変えます。
たとえば、
private:
array<double>^ S11log_;
public:
property array<double>^ S11log
{
array<double>^ get(){ return this->S11log_; }
void set(array<double>^ value) { this->S11log_ = value; }
}
てな感じで。
MFCっぽく m_S11log; としてもいいですけど。
-
どこがおかしいのかわからなすぎるのでサンプル。
Form1,Form2をそれぞれ作って、ボタンのみ配置。
Form1.hの先頭に
#include "Form2.h"
を追加し、ボタンイベント+メンバ変数/プロパティの定義を追加
private:
array<int>^ myData_;
public:
property array<int>^ myData
{
array<int>^ get() { return this->myData_; }
void set(array<int>^ value) { this->myData_ = value; }
}
private:
System::Void button1_Click(System:: Object^ sender, System::EventArgs^ e)
{
this->myData_ = gcnew array<int>(5);
for (int i = 0; i < this->myData_->Length; ++i)
this->myData_[ i ] = i;Form2 f;
f.myData = this->myData_;
f.ShowDialog();
}Form2.hのボタンイベント+メンバ変数/プロパティの定義を追加
private:
array<int>^ myData_;
public:
property array<int>^ myData
{
array<int>^ get() { return this->myData_; }
void set(array<int>^ value) { this->myData_ = value; }
}
private:
System::Void button1_Click(System:: Object^ sender, System::EventArgs^ e)
{
String^ s;
for each (int n in this->myData_)
{
s += n.ToString() + L"\n";
}
MessageBox:: Show(s);
}で、Form2のボタンイベントで 0~4を表示するメッセージボックスが表示できます。
-
サンプルとご返答ありがとうございます.
作っていただいた,サンプル(myDataだったのをabcに変えました.)を新しいプロジェクトでビルドを行いました.
本当に情けないのですが, 同様の再定義のエラーが出ました.
今回行った,問題となるソースと エラーの文を載せておきます.
Form1.h の内容
#pragma once
#include "Form2.h"
namespace TEST {using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
public ref class Form1 : public System::Windows::Forms::Form
{
private:
array<int>^ abc;
public:
property array<int>^ abc
{
array<int>^ get() { return this->abc ; }
void set(array<int>^ value) { this->abc = value; }
}public:
Form1(void){
省略
}
#pragma endregion
private: System::Void button1_Click(System:bject^ sender, System::EventArgs^ e) {
this->abc = gcnew array<int>(5);
for (int i = 0; i < this-> abc -> Length; ++i)
this->abc [ i ] = i;Form2 f;
f.abc = this->abc ;
f.ShowDialog();
}
};
}Form2.hの内容
#pragma once
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System:ata;
using namespace System:rawing;
namespace TEST {public ref class Form2 : public System::Windows::Forms::Form
{
private:
array<int>^ abc;
public:
property array<int>^ abc
{
array<int>^ get() { return this->abc; }
void set(array<int>^ value) { this->abc = value; }
}
public:
Form2(void)
{ 省略
}
#pragma endregion
private: System::Void button1_Click(System:bject^ sender, System::EventArgs^ e) {
String^ s;
for each (int n in this->abc )
{
s += n.ToString() + L"\n";
}
MessageBox:: Show(s);
}
};
}上記の記載を行った結果,下記のエラーが出てきました.
Form2.cpp
1>c:\documents and settings\test\my documents\work\test\test\Form2.h(28) : error C2086: 'abc' : 再定義されました。
1>TEST.cpp
1>c:\documents and settings\test\my documents\work\test\test\Form2.h(28) : error C2086: 'abc' : 再定義されました。
1>c:\documents and settings\test\my documents\work\test\test\Form1.h(29) : error C2086: 'abc' : 再定義されました。
1>c:\documents and settings\test\my documents\work\test\test\Form1.h(102) : error C3872: '0x3000': この文字を識別子で使用することはできません
1>c:\documents and settings\test\my documents\work\test\test\Form1.h(102) : error C3872: '0x3000': この文字を識別子で使用することはできません
1>c:\documents and settings\test\my documents\work\test\test\Form1.h(102) : error C2065: ' i ' : 定義されていない識別子です。
1>c:\documents and settings\test\my documents\work\test\test\Form1.h(102) : error C3262: 無効な配列のインデックスです。-1 次元が指定されました。1 次元の 'cli::array<Type> ^' が選択されています。
1> with
1> [
1> Type=int
1> ]
1>c:\documents and settings\test\my documents\work\test\test\Form1.h(105) : error C2248: 'TEST::Form2::abc' : private メンバ (クラス 'TEST::Form2' で宣言されている) にアクセスできません。
1> c:\documents and settings\test\my documents\work\test\test\Form2.h(25) : 'TEST::Form2::abc' の宣言を確認してください。
1> c:\documents and settings\test\my documents\work\test\test\Form2.h(22) : 'TEST::Form2' の宣言を確認してください。
1>コードを生成中...
1>ビルドログは "file://c:\Documents and Settings\test\My Documents\Work\TEST\TEST\Debug\BuildLog.htm" に保存されました。
1>TEST - エラー 8、警告 0