C++ > CLI:リストボックス

基本プロジェクトにリストボックスを配置します。


選択項目表示用にラベルも配置します。
ラベルはフォントサイズを24にしておきます。


リストボックスを選択し、プロパティの Items の(コレクション)をクリックします。


すると、文字列コレクションエディターが表示されるので、今回は適当に

りす
すいか
からす
すずめ
めだか

と、入力します。

リストボックスをダブルクリックするとコードが自動作成されるので、
そこに、次のコードを追加します。

label1->Text = listBox1->SelectedItem->ToString();


ビルドして実行してみましょう。
選択した項目がラベルに表示されます。

MyForm.cpp

#pragma comment(linker, "/SUBSYSTEM:WINDOWS /ENTRY:mainCRTStartup")
#include "MyForm.h"

using namespace Project1;

[STAThreadAttribute]
int main(){
 MyForm ^form1 = gcnew MyForm;
 form1->ShowDialog();
 return 0;
}

MyForm.h

#pragma once

namespace Project1 {

 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>
 /// MyForm の概要
 /// </summary>
 public ref class MyForm : public System::Windows::Forms::Form
 {
 public:
  MyForm(void)
  {
   InitializeComponent();
   //
   //TODO: ここにコンストラクター コードを追加します
   //
  }

 protected:
  /// <summary>
  /// 使用中のリソースをすべてクリーンアップします。
  /// </summary>
  ~MyForm()
  {
   if (components)
   {
    delete components;
   }
  }
 private: System::Windows::Forms::ListBox^  listBox1;
 private: System::Windows::Forms::Label^  label1;
 protected:

 protected:


 protected:

 private:
  /// <summary>
  /// 必要なデザイナー変数です。
  /// </summary>
  System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  void InitializeComponent(void)
  {
   this->listBox1 = (gcnew System::Windows::Forms::ListBox());
   this->label1 = (gcnew System::Windows::Forms::Label());
   this->SuspendLayout();
   //
   // listBox1
   //
   this->listBox1->FormattingEnabled = true;
   this->listBox1->ItemHeight = 12;
   this->listBox1->Items->AddRange(gcnew cli::array< System::Object^  >(5) {L"りす", L"すいか", L"からす", L"すずめ", L"めだか"});
   this->listBox1->Location = System::Drawing::Point(79, 58);
   this->listBox1->Name = L"listBox1";
   this->listBox1->Size = System::Drawing::Size(120, 88);
   this->listBox1->TabIndex = 0;
   this->listBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &MyForm::listBox1_SelectedIndexChanged);
   //
   // label1
   //
   this->label1->AutoSize = true;
   this->label1->Font = (gcnew System::Drawing::Font(L"MS UI Gothic", 24, System::Drawing::FontStyle::Regular, System::Drawing::GraphicsUnit::Point,
    static_cast<System::Byte>(128)));
   this->label1->Location = System::Drawing::Point(87, 180);
   this->label1->Name = L"label1";
   this->label1->Size = System::Drawing::Size(92, 33);
   this->label1->TabIndex = 1;
   this->label1->Text = L"label1";
   //
   // MyForm
   //
   this->AutoScaleDimensions = System::Drawing::SizeF(6, 12);
   this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
   this->ClientSize = System::Drawing::Size(284, 261);
   this->Controls->Add(this->label1);
   this->Controls->Add(this->listBox1);
   this->Name = L"MyForm";
   this->Text = L"Hello C++/CLI World !!";
   this->ResumeLayout(false);
   this->PerformLayout();

  }
#pragma endregion

 private: System::Void listBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
     label1->Text = listBox1->SelectedItem->ToString();
    }
 };
}
 

 

 

 

 

 

最終更新:2013年09月16日 22:19