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

回答編集履歴

1

見直しキャンペーン中

2023/07/22 07:37

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,71 +1,71 @@
1
- YAmaGNZさんの言っていることをコードにしただけですが^^;
2
-
3
- ```C#
4
- using System;
5
- using System.Linq;
6
- using System.Windows.Forms;
7
-
8
- namespace Questions269084
9
- {
10
- public partial class Form1 : Form
11
- {
12
- private readonly ListBox listBox;
13
- private readonly TextBox textBox;
14
-
15
- public Form1()
16
- {
17
- InitializeComponent();
18
-
19
- textBox = new TextBox
20
- {
21
- Dock = DockStyle.Fill,
22
- Multiline = true,
23
- };
24
- Controls.Add(textBox);
25
-
26
- listBox = new ListBox
27
- {
28
- Dock = DockStyle.Left,
29
- SelectionMode = SelectionMode.MultiSimple,
30
- Width = 50,
31
- };
32
- listBox.Items.AddRange(new string[] { "A", "B", "C", "D", "E", "A", "A", });
33
- listBox.SelectedIndexChanged += ListBox_SelectedIndexChanged;
34
- Controls.Add(listBox);
35
- }
36
-
37
- private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
38
- {
39
- var text = "選択された\r\n";
40
- foreach(var item in listBox.SelectedItems)
41
- {
42
- text += item + "\r\n";
43
- }
44
-
45
- text += "選択されていない\r\n";
46
- for(var i = 0; i < listBox.Items.Count; i++)
47
- {
48
- if(!listBox.GetSelected(i))
49
- {
50
- text += listBox.Items[i] + "\r\n";
51
- }
52
- }
53
-
54
- textBox.Text = text;
55
- }
56
-
57
- // 大体同じ
58
- //private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
59
- //{
60
- // var text = "選択された\r\n";
61
- // text += string.Join("\r\n", listBox.SelectedItems.Cast<object>());
62
-
63
- // text += "\r\n選択されていない\r\n";
64
- // var unSelected = listBox.Items.Cast<object>().Where((x, i) => !listBox.GetSelected(i));
65
- // text += string.Join("\r\n", unSelected);
66
-
67
- // textBox.Text = text;
68
- //}
69
- }
70
- }
1
+ YAmaGNZさんの言っていることをコードにしただけですが^^;
2
+
3
+ ```cs
4
+ using System;
5
+ using System.Linq;
6
+ using System.Windows.Forms;
7
+
8
+ namespace Questions269084
9
+ {
10
+ public partial class Form1 : Form
11
+ {
12
+ private readonly ListBox listBox;
13
+ private readonly TextBox textBox;
14
+
15
+ public Form1()
16
+ {
17
+ InitializeComponent();
18
+
19
+ textBox = new TextBox
20
+ {
21
+ Dock = DockStyle.Fill,
22
+ Multiline = true,
23
+ };
24
+ Controls.Add(textBox);
25
+
26
+ listBox = new ListBox
27
+ {
28
+ Dock = DockStyle.Left,
29
+ SelectionMode = SelectionMode.MultiSimple,
30
+ Width = 50,
31
+ };
32
+ listBox.Items.AddRange(new string[] { "A", "B", "C", "D", "E", "A", "A", });
33
+ listBox.SelectedIndexChanged += ListBox_SelectedIndexChanged;
34
+ Controls.Add(listBox);
35
+ }
36
+
37
+ private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
38
+ {
39
+ var text = "選択された\r\n";
40
+ foreach(var item in listBox.SelectedItems)
41
+ {
42
+ text += item + "\r\n";
43
+ }
44
+
45
+ text += "選択されていない\r\n";
46
+ for(var i = 0; i < listBox.Items.Count; i++)
47
+ {
48
+ if(!listBox.GetSelected(i))
49
+ {
50
+ text += listBox.Items[i] + "\r\n";
51
+ }
52
+ }
53
+
54
+ textBox.Text = text;
55
+ }
56
+
57
+ // 大体同じ
58
+ //private void ListBox_SelectedIndexChanged(object sender, EventArgs e)
59
+ //{
60
+ // var text = "選択された\r\n";
61
+ // text += string.Join("\r\n", listBox.SelectedItems.Cast<object>());
62
+
63
+ // text += "\r\n選択されていない\r\n";
64
+ // var unSelected = listBox.Items.Cast<object>().Where((x, i) => !listBox.GetSelected(i));
65
+ // text += string.Join("\r\n", unSelected);
66
+
67
+ // textBox.Text = text;
68
+ //}
69
+ }
70
+ }
71
71
  ```