teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

1

見直しキャンペーン中

2023/07/22 10:04

投稿

TN8001
TN8001

スコア10108

answer CHANGED
@@ -1,57 +1,57 @@
1
- 古い行の削除にも需要はあると思いますので回答しておきます。
2
-
3
- [c# - Delete a specific line in a .NET RichTextBox - Stack Overflow](https://stackoverflow.com/questions/1329347/delete-a-specific-line-in-a-net-richtextbox/20983817#20983817)
4
- で、書式を維持したままの行削除を確認できました。
5
-
6
- ```C#
7
- using System;
8
- using System.Drawing;
9
- using System.Linq;
10
- using System.Windows.Forms;
11
-
12
- namespace Questions281426
13
- {
14
- public partial class Form1 : Form
15
- {
16
- private Timer timer1;
17
- private RichTextBox richTextBox1;
18
- private int c;
19
-
20
- public Form1()
21
- {
22
- InitializeComponent();
23
-
24
- timer1 = new Timer
25
- {
26
- Enabled = true,
27
- Interval = 1000,
28
- };
29
- timer1.Tick += Timer1_Tick;
30
-
31
- richTextBox1 = new RichTextBox
32
- {
33
- Dock = DockStyle.Fill,
34
- };
35
- Controls.Add(richTextBox1);
36
- }
37
-
38
- private void Timer1_Tick(object sender, EventArgs e)
39
- {
40
- // なんか追加
41
- richTextBox1.SelectionStart = richTextBox1.TextLength;
42
- richTextBox1.SelectionLength = 0;
43
- richTextBox1.SelectionColor = ++c % 2 == 0 ? Color.Red : Color.Blue;
44
- richTextBox1.AppendText($"あああああ{c}\n");
45
-
46
- // 10行以上あったら先頭から削除
47
- while(10 < richTextBox1.Lines.Count())
48
- {
49
- //[c# - Delete a specific line in a .NET RichTextBox - Stack Overflow](https://stackoverflow.com/questions/1329347/delete-a-specific-line-in-a-net-richtextbox/20983817#20983817)
50
- richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(0);
51
- richTextBox1.SelectionLength = richTextBox1.Lines[0].Length + 1;
52
- richTextBox1.SelectedText = string.Empty;
53
- }
54
- }
55
- }
56
- }
1
+ 古い行の削除にも需要はあると思いますので回答しておきます。
2
+
3
+ [c# - Delete a specific line in a .NET RichTextBox - Stack Overflow](https://stackoverflow.com/questions/1329347/delete-a-specific-line-in-a-net-richtextbox/20983817#20983817)
4
+ で、書式を維持したままの行削除を確認できました。
5
+
6
+ ```cs
7
+ using System;
8
+ using System.Drawing;
9
+ using System.Linq;
10
+ using System.Windows.Forms;
11
+
12
+ namespace Questions281426
13
+ {
14
+ public partial class Form1 : Form
15
+ {
16
+ private Timer timer1;
17
+ private RichTextBox richTextBox1;
18
+ private int c;
19
+
20
+ public Form1()
21
+ {
22
+ InitializeComponent();
23
+
24
+ timer1 = new Timer
25
+ {
26
+ Enabled = true,
27
+ Interval = 1000,
28
+ };
29
+ timer1.Tick += Timer1_Tick;
30
+
31
+ richTextBox1 = new RichTextBox
32
+ {
33
+ Dock = DockStyle.Fill,
34
+ };
35
+ Controls.Add(richTextBox1);
36
+ }
37
+
38
+ private void Timer1_Tick(object sender, EventArgs e)
39
+ {
40
+ // なんか追加
41
+ richTextBox1.SelectionStart = richTextBox1.TextLength;
42
+ richTextBox1.SelectionLength = 0;
43
+ richTextBox1.SelectionColor = ++c % 2 == 0 ? Color.Red : Color.Blue;
44
+ richTextBox1.AppendText($"あああああ{c}\n");
45
+
46
+ // 10行以上あったら先頭から削除
47
+ while(10 < richTextBox1.Lines.Count())
48
+ {
49
+ //[c# - Delete a specific line in a .NET RichTextBox - Stack Overflow](https://stackoverflow.com/questions/1329347/delete-a-specific-line-in-a-net-richtextbox/20983817#20983817)
50
+ richTextBox1.SelectionStart = richTextBox1.GetFirstCharIndexFromLine(0);
51
+ richTextBox1.SelectionLength = richTextBox1.Lines[0].Length + 1;
52
+ richTextBox1.SelectedText = string.Empty;
53
+ }
54
+ }
55
+ }
56
+ }
57
57
  ```