日々のコンピュータ情報の集積と整理

Dr.ウーパのコンピュータ備忘録

記事「.NET Framework 1.1でSelectedTextを使用したテキスト挿入法は32767文字の壁を越えられるか?検証してみた」で使用したソースコード

記事「.NET Framework 1.1でSelectedTextを使用したテキスト挿入法は32767文字の壁を越えられるか?検証してみた」で使用したソースコードを以下に掲載します。


// C#のソース
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace textBox_addText_measure
{
 /// <summary>
 /// Form1 の概要の説明です。
 /// </summary>
 public class Form1 : System.Windows.Forms.Form
 {
  /// <summary>
  /// 必要なデザイナ変数です。
  /// </summary>
  private System.ComponentModel.Container components = null;

  public Form1()
  {
   //
   // Windows フォーム デザイナ サポートに必要です。
   //
   InitializeComponent();

   //
   // TODO: InitializeComponent 呼び出しの後に、コンストラクタ コードを追加してください。
   //
  }

  /// <summary>
  /// 使用されているリソースに後処理を実行します。
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if (components != null) 
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Windows フォーム デザイナーで生成されたコード

  /// <summary>
  /// デザイナー サポートに必要なメソッドです。このメソッドの内容を
  /// コード エディターで変更しないでください。
  /// </summary>
  private void InitializeComponent()
  {
   this.textBox1 = new System.Windows.Forms.TextBox();
   this.button1 = new System.Windows.Forms.Button();
   this.textBox_result = new System.Windows.Forms.TextBox();
   this.label1 = new System.Windows.Forms.Label();
   this.comboBox1 = new System.Windows.Forms.ComboBox();
   this.checkBox1 = new System.Windows.Forms.CheckBox();
   this.SuspendLayout();
   // 
   // textBox1
   // 
   this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) 
    | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
   this.textBox1.Location = new System.Drawing.Point(12, 12);
   this.textBox1.MaxLength = 100000000;
   this.textBox1.Multiline = true;
   this.textBox1.Name = "textBox1";
   this.textBox1.ScrollBars = System.Windows.Forms.ScrollBars.Both;
   this.textBox1.Size = new System.Drawing.Size(1042, 363);
   this.textBox1.TabIndex = 0;
   this.textBox1.Text = "";
   this.textBox1.TextChanged += new System.EventHandler(this.textBox1_TextChanged);
   // 
   // button1
   // 
   this.button1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
   this.button1.Location = new System.Drawing.Point(934, 381);
   this.button1.Name = "button1";
   this.button1.Size = new System.Drawing.Size(120, 30);
   this.button1.TabIndex = 1;
   this.button1.Text = "測定開始";
   this.button1.Click += new System.EventHandler(this.button1_Click);
   // 
   // textBox_result
   // 
   this.textBox_result.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left) 
    | System.Windows.Forms.AnchorStyles.Right)));
   this.textBox_result.Location = new System.Drawing.Point(12, 417);
   this.textBox_result.Multiline = true;
   this.textBox_result.Name = "textBox_result";
   this.textBox_result.ScrollBars = System.Windows.Forms.ScrollBars.Both;
   this.textBox_result.Size = new System.Drawing.Size(1042, 144);
   this.textBox_result.TabIndex = 2;
   this.textBox_result.Text = "";
   // 
   // label1
   // 
   this.label1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
   this.label1.AutoSize = true;
   this.label1.Location = new System.Drawing.Point(12, 402);
   this.label1.Name = "label1";
   this.label1.Size = new System.Drawing.Size(29, 15);
   this.label1.TabIndex = 3;
   this.label1.Text = "結果";
   // 
   // comboBox1
   // 
   this.comboBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
   this.comboBox1.Location = new System.Drawing.Point(680, 387);
   this.comboBox1.Name = "comboBox1";
   this.comboBox1.Size = new System.Drawing.Size(162, 20);
   this.comboBox1.TabIndex = 4;
   // 
   // checkBox1
   // 
   this.checkBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
   this.checkBox1.Location = new System.Drawing.Point(848, 391);
   this.checkBox1.Name = "checkBox1";
   this.checkBox1.Size = new System.Drawing.Size(71, 16);
   this.checkBox1.TabIndex = 5;
   this.checkBox1.Text = "doEvents";
   // 
   // Form1
   // 
   this.AutoScaleBaseSize = new System.Drawing.Size(5, 12);
   this.ClientSize = new System.Drawing.Size(1066, 573);
   this.Controls.Add(this.checkBox1);
   this.Controls.Add(this.comboBox1);
   this.Controls.Add(this.label1);
   this.Controls.Add(this.textBox_result);
   this.Controls.Add(this.button1);
   this.Controls.Add(this.textBox1);
   this.Name = "Form1";
   this.Text = "Form1";
   this.Load += new System.EventHandler(this.Form1_Load);
   this.ResumeLayout(false);

  }

  #endregion

  private System.Windows.Forms.TextBox textBox1;
  private System.Windows.Forms.Button button1;
  private System.Windows.Forms.TextBox textBox_result;
  private System.Windows.Forms.Label label1;
  private System.Windows.Forms.ComboBox comboBox1;
  private System.Windows.Forms.CheckBox checkBox1;

  /// <summary>
  /// アプリケーションのメイン エントリ ポイントです。
  /// </summary>
  [STAThread]
  static void Main() 
  {
   Application.Run(new Form1());
  }

  // ログ記録
  void writeLog(String logText)
  {
   textBox_result.SelectionStart = textBox_result.Text.Length;
   textBox_result.SelectionLength = 0;
   textBox_result.SelectedText = logText + "\r\n";
  }

  // 測定タイプ
  public enum MeasureType
  {
   Text,               // テキストボックスのテキストにテキストボックスのテキスト + 追加のテキストを代入する方法
   Selection,          // テキストボックスの Selection にテキストを挿入する方法
   AppendText,         // AppendText メソッドを使用する方法
  };

  // テキスト追加処理を実行
  private void /*TimeSpan*/ measure(MeasureType measureType, bool doEvents, int MaxLoop)
  {
   textBox1.Text = "";
   //Stopwatch sw = new Stopwatch();
   //sw.Start();

   for (int i = 0; i < MaxLoop; i++)
   {
    String s = i + ":" + testText + "\r\n";
    switch (measureType)
    {
     case MeasureType.Text:
      textBox1.Text = textBox1.Text + s;
      break;
     case MeasureType.Selection:
      textBox1.SelectionStart = textBox1.Text.Length;
      textBox1.SelectionLength = 0;
      textBox1.SelectedText = s;
      break;
     case MeasureType.AppendText:
      textBox1.AppendText(s);
      break;
    }

    if (doEvents)
    {
     System.Windows.Forms.Application.DoEvents();
    }
   }

   //sw.Stop();
   // return sw.Elapsed;
  }

  private void button1_Click(object sender, EventArgs e)
  {
   const int MaxLoop = 100;
   MeasureType measureType = (MeasureType)comboBox1.SelectedItem;
   bool doEvents = checkBox1.Checked;

   
   for (int i = 1; i <= 1; i++) // 別目的の処理の名残
   {
    measure(measureType, doEvents, MaxLoop);

    writeLog("測定タイプ=," + measureType + ", DoEvents=," + doEvents + ", " + i + "回目");
   }
  }

  String testText = "";   // テストに使用する文字列
  private void Form1_Load(object sender, EventArgs e)
  {
   // 1000 文字分のテキストを作成
   for (int i = 0; i < 1000; i++)
   {
    testText += (i % 10).ToString();
   }

   comboBox1.DataSource = Enum.GetValues(typeof(MeasureType));
  }

  private void textBox1_TextChanged(object sender, System.EventArgs e)
  {
   this.Text = "文字数:" + textBox1.Text.Length;
  }
 }
}