* U K I Y A H O N P O *
Nel mezzo del cammin di nostra vita mi ritrovai per una selva oscura,
che la diritta via era smarrita.
リロード   新規 下位ページ作成 編集 凍結 差分 添付 コピー 名前変更   ホーム 一覧 検索 最終更新 バックアップ リンク元   ヘルプ   最終更新のRSS
浮子屋商店もよろしく。

C#コード断片/共通/テキストボックスに文字列を追記 のバックアップの現在との差分(No.2)


テキストボックスに文字列を追記

ログ出力など、行単位でどんどん追記する場合に。

  0
  1
  2
  3
  4
  5
  6
public void out(string text){
    txtBox.SelectionStart=txtBox.Text.Length;
    txtBox.SelectionLength=0;
    txtBox.SelectedText=text+"\r\n";
    txtBox.SelectionStart=txtBox.Text.Length;
    txtBox.SelectionLength=0;
}

URL B I U SIZE Black Maroon Green Olive Navy Purple Teal Gray Silver Red Lime Yellow Blue Fuchsia Aqua White
応用編。以下のコードを追加。
  • スレッドセーフにした(Invoke)
  • 一定のサイズを超えたらクリア
  0
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
#spanend
#spanadd
private delegate void OutPutDelegate(string text);
#spanend
#spanadd
OutPutDelegate outdel;
#spanend
#spanadd
 
#spanend
#spanadd
public frmMain() {
#spanend
#spanadd
    outdel=new OutPutDelegate(_output);
#spanend
#spanadd
}
#spanend
#spanadd
 
#spanend
#spanadd
private void _output(string text) {
#spanend
#spanadd
    if (this.InvokeRequired) {
#spanend
#spanadd
        Invoke(outdel, new object[] { text });
#spanend
#spanadd
        return;
#spanend
#spanadd
    }
#spanend
#spanadd
    if (txtMain.Text.Length > 32767) {
#spanend
#spanadd
        txtMain.Text = "";
#spanend
#spanadd
    }
#spanend
#spanadd
    txtMain.SelectionStart = txtMain.Text.Length;
#spanend
#spanadd
    txtMain.SelectionLength = 0;
#spanend
#spanadd
    txtMain.SelectedText = text + "\r\n";
#spanend
#spanadd
    txtMain.SelectionStart = txtMain.Text.Length;
#spanend
#spanadd
    txtMain.SelectionLength = 0;
#spanend
#spanadd
}
#spanend
#spanadd