0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

VC++開発の覚え書き②

Last updated at Posted at 2019-01-10

概要

 コントロールのグループ化とフォーム内画面遷移、ComboBoxの要素を変数から入力する。

内容

コントロールのグループ化

  1. パネルを配置
  2. パネル上にコントロールを配置
  3. this->Controls->Add()の確認
// 
// MainForm
// 
this->AutoScaleDimensions = System::Drawing::SizeF(8, 15);
this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
this->ClientSize = System::Drawing::Size(360, 640);

this->Controls->Add(this->SettingPanel);
this->Controls->Add(this->MenuPanel);

this->Name = L"MainForm";
this->Text = L"NK_HealthCare";
this->MenuPanel->ResumeLayout(false);
this->SettingPanel->ResumeLayout(false);
this->SettingPanel->PerformLayout();
this->ResumeLayout(false);

 this->Contorols->Add(コントロール)でコントロールを別のコントロール等に所属させることができます。これがコントロールのグループ化です。いわゆる親コントロールのEnableVisibleプロパティを変更することでまとめて操作することができます。

フォーム内画面遷移

this->SettingPanel->Visible = false;
this->SettingPanel->Enable = false;

 コンストラクタの中で隠しておきたい(画面遷移先)のコントロールをまとめたパネルを見えなくし(Visible = false)、無効化する(Enable = false)ことでユーザーの目線からはその画面は存在しないものとして扱える。Enable = falseをしていないとTabキーや十字キーでコントロール間を移動する際に画面から選択枠が消え、クリックした際にないはずのボタンの機能が呼び出されるなどの不具合の原因になります。
 画面を切り替えるにはこれの逆を行います。

// 隠していた画面の可視化、有効化
this->SettingPanel->Visible = true;
this->SettingPanel->Enable = true;

// 見せていた画面の不可視化、無効化
this->MenuPanel->Visible = false;
this->MenuPanel->Enable = false;

 これにより、あたかも別の画面に移動したかのように見せることができる。これを行うにはパネルを使用せずに行うこともできるが、コードが長くなるなど無駄が増えてしまうので、先のグループ化を行って一括で管理している。

ComboBoxの要素を変数から

 ここまでと関連性は特にないですが覚え書きなのでご容赦を。

char* cstr[2] = {
	"あいうえお",
	"い"
};
int a = 2;

array<String^>^ str = gcnew array<String^>(a);

for (int i = 0; i < 2; i++) {
	str[i] = gcnew String(cstr[i]);
}

this->comboBox1->Items->AddRange(str);

 System::String^配列 str にchar*型の文字列 cstr を変換して格納し、それをthis->comboBox1->Items->AddRange(str);のように与えることで変数の内容からコンボボックスを生成できる。int a = 2;は要素数を変数でも指定できることの確認である。

最後に

 前回に続きただの覚え書きなのでスマートなやり方、正しい使い方ではありませんのでその点はご容赦ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?