LoginSignup
0
0

More than 5 years have passed since last update.

C++ Builder 10.2 Tokyo > Ctrl+c, Ctrl+vのキー(3と22) | TComboBox >Ctrl+c, Ctrl+vで選択項目をコピーペーストする

Last updated at Posted at 2019-04-28
動作環境
RAD Studio 10.2 Tokyo Update 3

処理概要

  • 複数のTComboBox
  • 一つのTComboBoxからもう一つのTComboBoxへ設定をコピーしたい (Ctrl+C, Ctrl+V)

参考

実装

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;
    TComboBox *ComboBox2;
    void __fastcall ComboBox1KeyPress(TObject *Sender, System::WideChar &Key);
    void __fastcall FormShow(TObject *Sender);

private:    // ユーザー宣言
    signed int m_preserved_key; // Ctrl+C押下時に保存する
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)
{
}
//---------------------------------------------------------------------------

/*
Reference: Ctrl+CとCtrl+V
https://stackoverflow.com/questions/840144/how-to-disable-copy-paste-in-tedit
*/

void __fastcall TForm1::ComboBox1KeyPress(TObject *Sender, System::WideChar &Key)
{
    if (Key == 3) { // Ctrl+C
        // 1. 選択の保持
        TComboBox *cbPtr = (TComboBox*)Sender;
        m_preserved_key = cbPtr->ItemIndex;
        // 2. 入力のキャンセル
        Key = 0x00; // C入力としての処理を発生させないため
    }

    if (Key == 22) { // Ctrl+V
        if (m_preserved_key >=-1) {
            TComboBox *cbPtr = (TComboBox*)Sender;
            cbPtr->ItemIndex = m_preserved_key;
        }
    }
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    m_preserved_key = -10; // -10: 任意 (-1はコンボボックス非選択という意味があるため、-1以外の値)

    // 2つのコンポーネントに同じ処理を設定
    ComboBox1->OnKeyPress = ComboBox1KeyPress;
    ComboBox2->OnKeyPress = ComboBox1KeyPress;
}
//---------------------------------------------------------------------------

実行例

選択肢は多い
2019-04-28_14h22_25.png

上のGeordi La Forgeの設定をCtrl+C, Ctrl+Vにて下にコピーした後
2019-04-28_14h22_49.png

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