動作環境
C++ Builder XE4
処理概要
- TComboBoxがある
- Items定義文字列が長い (例: 30文字)
- 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 で管理されるコンポーネント
TComboBox *ComboBox1;
TButton *Button1;
void __fastcall ComboBox1DropDown(TObject *Sender);
void __fastcall ComboBox1Select(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)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DropDown(TObject *Sender)
{
ComboBox1->Width = 200; // 拡張サイズ
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1Select(TObject *Sender)
{
ComboBox1->Width = 57; // 元のサイズ
// これがないとコンボボックス内の表示内容が後ろの文字列となる (例: 34567890)
Button1->SetFocus();
}
//---------------------------------------------------------------------------
動作例
ソフト起動直後
v0.2
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 で管理されるコンポーネント
TComboBox *ComboBox1;
TButton *Button1;
void __fastcall ComboBox1DropDown(TObject *Sender);
void __fastcall ComboBox1Select(TObject *Sender);
void __fastcall ComboBox1KeyPress(TObject *Sender, System::WideChar &Key);
private: // ユーザー宣言
// コンボボックスの幅変更関連
void __fastcall ComboWidth_SetNormal(void);
void __fastcall ComboWidth_SetExpanded(void);
//
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)
{
}
//---------------------------------------------------------------------------
// { コンボボックスの幅変更関連
void __fastcall TForm1::ComboWidth_SetExpanded(void)
{
ComboBox1->Width = 200; // 拡張サイズ
}
void __fastcall TForm1::ComboWidth_SetNormal(void)
{
ComboBox1->Width = 57; // 元のサイズ
// これがないとコンボボックス内の表示内容が後ろの文字列のままとなる (例: 34567890)
Button1->SetFocus();
}
// } コンボボックスの幅変更関連
//---------------------------------------------------------------------------
void __fastcall TForm1::ComboBox1DropDown(TObject *Sender)
{
ComboWidth_SetExpanded();
}
void __fastcall TForm1::ComboBox1Select(TObject *Sender)
{
// 項目選択後
ComboWidth_SetNormal();
}
void __fastcall TForm1::ComboBox1KeyPress(TObject *Sender, System::WideChar &Key)
{
// [Esc]キーの対応
ComboWidth_SetNormal();
}
//---------------------------------------------------------------------------
備考 > UIとしての懸念事項
幅拡張した場合、右側にあるコンポーネントの表示とかぶるようになる。
それを防ぐには、右側のコンポーネントを隠すか、(奥から手間への)表示順番の変更などの処理が必要になるかもしれない。
いずれにせよ、これらの処理も含めて実装となるとメンテナンス性が悪くなる。