LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4, 10.2 Tokyo > 設定 > TComboBoxの保存 > ItemsとItemIndexの保存 > TMemo + TEdit

Last updated at Posted at 2017-11-02
動作環境
C++ Builder XE4
RAD Studio 10.2 Tokyo Update 2 (追記: 2018/01/05)

TComboBoxの設定を如何に保存、読込みするか。

TComboBoxではItemsとItemIndexを扱うことになる。
Itemsを書換えたとき、ItemIndexはクリアされる。
そのため、Itemsを先に、ItemIndexを次に読込む必要がある。

TMemoにItemsを、TEditにItemIndexを読込んで、それをTComboBoxに設定する方法は思いつく。

Unit1.h
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <System.Classes.hpp>
#include <Vcl.Controls.hpp>
#include <Vcl.StdCtrls.hpp>
#include <Vcl.Forms.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published:    // IDE で管理されるコンポーネント
    TButton *B_save;
    TComboBox *CMB_profile;
    TLabel *Label1;
    TEdit *E_ItemIndex;
    TLabel *File;
    TMemo *M_Items;
    TButton *B_load;
    void __fastcall B_saveClick(TObject *Sender);
    void __fastcall B_loadClick(TObject *Sender);
private:    // ユーザー宣言
public:     // ユーザー宣言
    __fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
Unit1.cpp
//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
    : TForm(Owner)
{
    CMB_profile->Items->Clear();
    CMB_profile->Items->Add(L"DeltaFlyer");
    CMB_profile->Items->Add(L"DeltaFlyer2");
    CMB_profile->Items->Add(L"Voyager");
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_saveClick(TObject *Sender)
{
    M_Items->Lines->Assign(CMB_profile->Items);
    E_ItemIndex->Text = IntToStr(CMB_profile->ItemIndex);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::B_loadClick(TObject *Sender)
{
    CMB_profile->Items->Assign(M_Items->Lines);
    CMB_profile->ItemIndex = StrToIntDef(E_ItemIndex->Text, -1);
}
//---------------------------------------------------------------------------

qiita.png

qiita.png

備考

項目に空白が入っていても問題ない。

一つのTComboBoxに対して、二つのコンポーネントが余分に必要になる。

TComboBoxの使用しそうにないプロパティを使う方法も考えられるが、それをすると、そのプロパティを使用することになった時に対処が必要になる。

0
1
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
1