LoginSignup
0
0

More than 5 years have passed since last update.

C++ Builder XE4 > UI > TComboBox > 展開したときだけ横幅を伸ばす実装

Last updated at Posted at 2018-11-08
動作環境
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();
}
//---------------------------------------------------------------------------

動作例

ソフト起動直後

2018-11-08_15h02_38.png

コンボボックス展開時
2018-11-08_15h02_43.png

コンボボックス項目選択後
2018-11-08_15h02_48.png

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としての懸念事項

幅拡張した場合、右側にあるコンポーネントの表示とかぶるようになる。
それを防ぐには、右側のコンポーネントを隠すか、(奥から手間への)表示順番の変更などの処理が必要になるかもしれない。

いずれにせよ、これらの処理も含めて実装となるとメンテナンス性が悪くなる。

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