LoginSignup
0
1

More than 5 years have passed since last update.

C++ Builder XE4 > TMemo > Strings[0]とTextの違い

Posted at
動作環境
C++ Builder XE4

状況

C++ Builder XE4 > クリップボードの文字列中の"/"を"/"に変換するツール v0.1, v0.2
のv0.1にて実装した処理で、長さのある文字列が途中で途切れた。

その理由を整理するため、プログラムしてみた。

code

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 で管理されるコンポーネント
    TMemo *Memo1;
    TButton *B_execute;
    TEdit *Edit1;
    TEdit *Edit2;
    TLabel *Label1;
    TLabel *Label2;
    TEdit *Edit3;
    TLabel *Label3;
    void __fastcall B_executeClick(TObject *Sender);
    void __fastcall FormShow(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)
{
}
//---------------------------------------------------------------------------

static const String kTemplateString =
L"123456789" // 0..4
L"123456789"
L"123456789"
L"123456789"
L"123456789"
L"123456789" // 5..
L"123456789";

void __fastcall TForm1::B_executeClick(TObject *Sender)
{
    // 1. Copy to clipboard
    Edit1->Text = kTemplateString;
    Edit1->SelectAll();
    Edit1->CopyToClipboard();

    // 2. Paste from clipboard
    Memo1->PasteFromClipboard();

    // 3. Copy only first string
    Edit2->Text = Memo1->Lines->Strings[0];

    // 4. Copy all the strings
    Edit3->Text = Memo1->Lines->Text;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
    Edit1->Text = L"";
    Memo1->Lines->Clear();
    Edit2->Text = L"";
    Edit3->Text = L"";
}
//---------------------------------------------------------------------------

実行

qiita.png

失敗の理由

  • Memoから文字列を取出すとき、TextでなくStrings[0]としてしまっていた
  • Memoの幅より長さの短い文字列でテストしていた
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