| <プログラム> Form1.h 抜粋 |
|
public:
Form1(void)
{
InitializeComponent();
comboBox1->Items->Clear(); //コンボボックス値クリア
comboBox1->Items->Insert(0,"Japan"); //Item[0]設定
comboBox1->Items->Insert(1,"America"); //Item[1]設定
comboBox1->Items->Insert(2,"Britain"); //Item[2]設定
comboBox1->Items->Add("German"); //アイテム追加
comboBox1->SelectedIndex = 2; //初期値設定
listBox1->Items->Clear(); //リストボックス値クリア
listBox1->Items->Insert(0,"青森");
listBox1->Items->Insert(1,"岩手");
listBox1->Items->Insert(2,"秋田");
listBox1->Items->Insert(3,"宮城");
listBox1->Items->Add("福島");
}
private: System::
Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
listBox1->Items[2] = comboBox1->SelectedItem; //コンボボックスス選択値をリストボックスItem[2]にセット
}
|
| <動作結果> Britainを選択してボタンをクリックするとItem[2]がBritainになる |
![]() |
■ リストボックスアイテムのインデックス番号とアイテムの表示
| リストボックスアイテで @ フォーカスをインデックス番号に設定して Aその インデックス番号とアイテムを表示する例です。 |
|
Form1.h 抜粋
<プログラム例>
public:
Form1(void)
{
//初期値設定
listBox1->Items->Clear(); //リストボックス値クリア
listBox1->Items->Insert(0,"青森");
listBox1->Items->Insert(1,"岩手");
listBox1->Items->Insert(2,"秋田");
listBox1->Items->Insert(3,"宮城");
listBox1->Items->Add("福島");
listBox1->SelectedIndex = 3;//選択するインデックス番号の選択
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
label1->Text = (listBox1->SelectedIndex).ToString(); //インデックス番号の表示
textBox1->Text = String::Format("選択アイテム = {0}",(listBox1->SelectedItem));
}
};
}
|
<動作結果>![]() |
■ リストボックス: インデックス番号に対するアイテムの表示・数値化
| ・インデックス番号を指定してインデックスの内容をラベルに表示する ・インデックスの内容を整数化して、その後文字列化してラベルに表示 |
|
<プログラム例>
Form1(void)
{
InitializeComponent();
//初期値設定
listBox1->Items->Clear(); //リストボックス値クリア
listBox1->Items->Insert(0,"123");
listBox1->Items->Insert(1,"456");
listBox1->Items->Insert(2,"789");
listBox1->Items->Insert(3,"111");
listBox1->Items->Insert(4,"222");
listBox1->Items->Insert(5,"333");
listBox1->Items->Insert(6,"444");
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
String^ str = (String^)(listBox1->Items[2]); //String^でのキャストが必要
label1->Text = str; //
int ix = Convert::ToInt32(listBox1->Items[2]); //Convert::で数値化
label4->Text = ix.ToString(); //整数を文字列化
}
|
<実行結果>![]() |
■ リッチテキストボックス: テキストの最後尾に文字列を追加する
| リッチテキストボックスのテキスト最後尾に文字列を追加する例です |
|
Form.h 抜粋 <プログラム例>
#pragma once
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
richTextBox1->Clear(); //リッチテキスト画面クリア
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->AppendText("古池や");
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->AppendText("蛙飛び込む");
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->AppendText("水の音");
}
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->AppendText("\n\n\n静けさや\n");
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->AppendText("岩にしみいる\n");
}
private: System::Void button6_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->AppendText("蝉の声");
}
};
}
|
<実行結果>![]() |
■ リッチテキストボックス: N番目の文字の後に文字列を追加
| リッチテキストの文字列のN番目の文字の後に、文字列を追加した例です ・\nも1文字としてカウントされます。 ・漢字も(2文字ではなくて)1文字としてカウントされます。 |
<プログラム例>
#pragma once
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
String^ Str = "12345\nABCDE";
richTextBox1->Text = Str;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = richTextBox1->Text->Insert(3,"***");
}
private: System::Void button2_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = richTextBox1->Text->Insert(11,"漢字"); //\nも、1文字としてカウントされる
}
private: System::Void button3_Click(System::Object^ sender, System::EventArgs^ e)
{
richTextBox1->Text = richTextBox1->Text->Insert(14,"+++"); //漢字も1文字としてしてカウントされる
}
};
}
|
<実行結果>![]() |
| リッチテキストボックスのN行目に文字列を追加する例です GetFirstCharIndexFromLine(N )メソッドでN行目の先頭文字のインデックスを取得します |
|
|
Form.h 抜粋 <プログラム例>
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
richTextBox1->Text = "000\n111\n222\n333\n444\n555\n666\n777\n888\n999";
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
int N;
N = richTextBox1->GetFirstCharIndexFromLine(5); //リッチテキストボックスの5行目の先頭のインデックスを取得
richTextBox1->Select(N,0); //N行目を選択
richTextBox1->SelectedText = "追加した文字列\n" ; //文字列をセット
}
};
}
|
|
| <実行結果> | |
<実行前>![]() |
<実行後>![]() |
| リッチテキストボックスの各行の文字列データとその長さを取得する例です。 |
|
<プログラム例>
//Form.h 抜粋
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
richTextBox1->Text = "0\n11\n222\n3333\n44444\n555555";
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
//文字列の String^ 配列の 生成と初期化
// Create a string array and store the contents of the Lines property.
array<String^>^ tempArray = gcnew array<String^>( richTextBox1->Lines->Length );
tempArray = richTextBox1->Lines;
label1->Text = String::Format("{0},{1},{2},{3},{4}", //各行の文字列の表示
tempArray[0],
tempArray[1],
tempArray[2],
tempArray[3],
tempArray[4]);
textBox1->Text = String::Format("{0},{1},{2},{3},{4}", //各行の文字列の長さの表示
tempArray[0]->Length,
tempArray[1]->Length,
tempArray[2]->Length,
tempArray[3]->Length,
tempArray[4]->Length);
}
};
}
|
<実行結果>![]() |
| リッチテキストボックスのN行目を削除する | |
Form.h 抜粋
<プログラム例>
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
for(int Gyou = 0; Gyou <10; Gyou++)
{
richTextBox1->AppendText("行番号: " + Gyou + "\n");
}
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
//文字列の String^ 配列の 生成と初期化
// Create a string array and store the contents of the Lines property.
array<String^>^ tempArray = gcnew array<String^>( richTextBox1->Lines->Length );
tempArray = richTextBox1->Lines;
int N = richTextBox1->GetFirstCharIndexFromLine(5); //リッチテキストボックスの5行目の先頭のインデックスを取得
String^ tempStr = tempArray[5];
int count = tempStr->Length; //countに改行コード\nは含まれない
richTextBox1->Text = richTextBox1->Text->Remove(N, count + 1); //改行コード分として+1を行う
}
};
}
|
|
| <実行結果> | |
<実行前>![]() |
<実行後>![]() |
■ リッチテキストボックス: 行番号を指定して複数の行を削除する
| 行を削除した場合、行番号が演算後すぐに変わるので注意が必要です。 3-5行目と6-7行目を同時に削除する場合、6-7行目の指定を指定は すでに削除した3-5行目分が減って行番号が3つ繰り上がったことを考慮する必要があります。 |
|||
|
<プログラム例> |
|||
| <実行結果> | |||
<実行前>![]() |
<行を詰めない場合>![]() |
<行を詰める場合>![]() |
<別々の行を詰める場合> ![]() |
■ リッチテキストテキストボックス: N行目から 所要の行数を摘出・表示する
| リッチテキストボックスで3行目から4行だけを摘出して表示する例です | |
|
<プログラム例>
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
String^ str = "0000\n1111\n2222\n3333\n4444\n5555\n6666\n7777\n8888\n9999";
richTextBox1->Text = str;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) //
{
int N_start = 3; //摘出開始行
int Num = 4; //摘出行数
//文字列の String^ 配列の 生成と初期化
// Create a string array and store the contents of the Lines property.
array<String^>^ tempArray = gcnew array<String^>( richTextBox1->Lines->Length );
tempArray = richTextBox1->Lines;
int N1 = richTextBox1->GetFirstCharIndexFromLine(N_start - 1); //3行目の先頭インデックスを取得
int L0 = 0 ; //コピーする文字列の数 含む各\n
String^ str;
for(int i = 0 ; i < Num; i++)
{
str = (String^)tempArray[N_start - 1 + i];
L0 = L0 + str->Length + 1; //文字数加算
}
richTextBox1->Select(N1,L0 ); //摘出文字選択
richTextBox1->Copy();
richTextBox1->Clear();
richTextBox1->Paste();
}
|
|
| <実行結果> | |
<実行前>![]() |
<実行後>![]() |
■ リッチテキストボックス: 行の文字列から行番号を取得する
| リッチテキストボックスのの行の文字列から行番号を取得する例です |
<プログラム例>
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
String^ Str1 = "aaaaa\nbbbbb\nccccc\nddddd\neeeee\nfffff\nggggg\nhhhhh\niiiii\njjjjj";
richTextBox1->Text = Str1;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
array<String^>^ tempArray = gcnew array<String^>(richTextBox1->Lines->Length); //行の配列宣言
tempArray = richTextBox1->Lines; //初期化
int i = 0;
while(1)
{
if(tempArray[i] == "eeeee")break;
i++;
}
textBox1->Text = String::Format("行番号:{0}、 文字列:{1}",i,tempArray[i]);
//行数検索OK
//行のオフセット数を読出し、加算
}
|
<実行結果>![]() |
■ リッチテキストボックス: 文字列から16進数を取得して加算する
| 文字列から コメント部分を削除後、リトルエンディアンでならんでいる1バイトの16進数×3個を、取得して0x010101を加算後表示するプログラムです。 |
|
<プログラム例>
namespace CWhiteForm {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
/// <summary>
/// Form1 の概要
/// </summary>
public ref class Form1 : public System::Windows::Forms::Form
{
static String^ Str0 = "0x01,0xAB,0xCD,;//本日は晴天なり\n"; //3番目の,も必須 カンマがないとarray1[2]が不定となる
////末尾の\nがないとコメ//ントが削除されない!!!
String^ Str00;
public:
Form1(void)
{
InitializeComponent();
//
//TODO: ここにコンストラクター コードを追加します
//
textBox1->Text = Str0;
}
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e)
{
myFilterFunc();
array<String^>^array1 = textBox1->Text->Split(','); // カンマ区切りで分割して配列に格納する
int value0 = Convert::ToInt32(array1[0],16)
+ (Convert::ToInt32(array1[1],16))*256
+ (Convert::ToInt32(array1[2],16))*256*256;
value0 = value0 + 0x010101;
array<String^>^array2 = gcnew array<String^>(3);
array2[0] = String::Format("0x{0:X},",value0%256);
array2[1] = String::Format("0x{0:X},",(value0/256)%256);
array2[2] = String::Format("0x{0:X},;",(value0/256/256)%256);
textBox2->Text = array2[0] + array2[1] + array2[2] + Str00;
textBox1->Text = Str0;
}
private: Void myFilterFunc()
{
int ix = 0;
int Num = 0;
String^ str = textBox1->Text; //テキストボックスに表示されている文字列を取得する。
int len = str->Length; //
try{
while(1)
{
//-----文字列リテラルが /*...*/ の前にある場合
if(str[ix] == '\"') //(例)printf("//abc"); printf("/*abc*/");など
{
while(1)
{
ix++;
if(str[ix] == '\"')break;
}
}
//----- 複数行コメント(/*....*/) の場合
if((str[ix] == '/') && (str[ix + 1] == '*'))
{
while(1)
{
if((str[ix + 2 + Num] == '*') && (str[ix + 3 + Num] == '/'))break;
Num++;
}
str = str->Remove(ix,4 + Num); //
Num = 0;
}
ix++;
}
}
catch(Exception^ )
{
}
textBox1->Clear(); //画面クリア
textBox1->Text = str;
ix = 0;
Num = 0;
try{
while(1)
{
//-----文字列リテラルが //...の前にある場合 //以下を削除しない
if(str[ix] == '\"') //(例)printf("//abc"); printf("/*abc*/");など
{
while(1)
{
ix++;
if(str[ix] == '\"')break;
}
}
//---- 一行コメントの場合、改行コードまでを削除
if((str[ix] == '/') && (str[ix + 1] == '/'))
{
while(1)
{
if(str[ix + 2 + Num] == '\n')break;
Num++;
}
Str00 = str->Substring(ix,2 + Num);
str = str->Remove(ix,2 + Num);
Num = 0;
}
ix++;
}
}
catch(Exception^ )
{
}
textBox1->Clear(); //画面クリア
textBox1->Text = str;
}
|
<実行結果>![]() |
■ リッチテキストボックス: 行頭の特定文字列に、文字列を追加する
| 行のはじめに文字列Aがある場合、この文字列に文字列Bを追加する。尚、文字列Aと行頭間のスペースは文字とみなさない。 また、行の途中に文字列Aがあっても文字列Bは追加しないものとする。 以下に、文字列Aとして”0x”、文字列Bとして”.byte ”の場合の例を紹介します。 |
|
<プログラム例>
.....
.....
.....
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
using namespace System::Text; //StringBuilderに必須
.....
.....
.....
private: System::Void button4_Click(System::Object^ sender, System::EventArgs^ e) //.byte を追加
{
//文字列の String^ 配列の 生成と初期化
// Create a string array and store the contents of the Lines property.
array<String^>^ tempArray = gcnew array<String^>( richTextBox1->Lines->Length );
tempArray = richTextBox1->Lines;
int i;
StringBuilder^ sb = gcnew StringBuilder();
int N = richTextBox1->Lines->Length; //テキストボックス内テキストの行数の取得
for(i = 0; i < N ; i++)
{
//文字列の先頭が、指定された文字列"0x"と一致するかどうかを判断
bool b = tempArray[i]->StartsWith( "0x" );
if(b == 1)
{
sb->Append(" .byte ");
}
sb->Append(tempArray[i]);
sb->Append("\n");
}
richTextBox1->Text = sb->ToString();
}
private: System::Void button5_Click(System::Object^ sender, System::EventArgs^ e) //行頭の空白除去
{
String^ str1 = (richTextBox1->Text)->Replace(" ",""); //空白除去
richTextBox1->Text = str1->Replace("//"," //"); //文字間隔整理
}
|
|
| 実行 結果 |
![]() ファイルを開く |
![]() ・空白の除去 ・//の前に空白を追加 |
|
![]() ”.byte ”を スペースの有無にかかわらず先頭に0xがある行だけに追加。 但し、行の途中にある0xの前には”.byte ”は追加されていない |
<プログラム例>
main() { }