回答編集履歴

1

見直しキャンペーン中

2023/07/27 14:44

投稿

TN8001
TN8001

スコア9862

test CHANGED
@@ -1,149 +1,75 @@
1
1
  あまり意味は分かっていませんが、こちらがズバリな感じです。
2
-
3
2
  [c# - Display empty text when there are no items in ListView Windows forms - Stack Overflow](https://stackoverflow.com/questions/41076244/display-empty-text-when-there-are-no-items-in-listview-windows-forms)
4
3
 
5
-
6
-
7
4
  入れ替え手順
8
-
9
5
  1. `MyListView`クラスを`Form1`の下か別ファイル(MyListView.cs)に書く
10
-
11
6
  1. 一度実行(ビルド)
12
-
13
7
  1. デザイナで`listView1`を削除し、ツールボックスに新たに出た`MyListView`を追加(反映に少し時間がかかる場合あり)
14
-
15
8
  1. `EmptyText`に好きなメッセージをいれる
16
-
17
9
  1. `(Name)`が`myListView1`になっているので、`listView1`に変える
18
-
19
-
20
10
 
21
11
  以上
22
12
 
23
-
24
-
25
- ```C#
13
+ ```cs
26
-
27
14
  using System;
28
-
29
15
  using System.ComponentModel;
30
-
31
16
  using System.Drawing;
32
-
33
17
  using System.Windows.Forms;
34
18
 
35
-
36
-
37
19
  namespace Questions341965
38
-
39
20
  {
40
-
41
21
  public partial class Form1 : Form
42
-
43
22
  {
44
-
45
23
  public Form1()
46
-
47
24
  {
48
-
49
25
  InitializeComponent();
50
-
51
26
  }
52
-
53
-
54
27
 
55
28
  private void Form1_Load(object sender, EventArgs e) { }
56
29
 
57
-
58
-
59
30
  private void button1_Click(object sender, EventArgs e)
60
-
61
31
  {
62
-
63
32
  listView1.Items.Clear();
64
33
 
34
+ var random = new System.Random();
35
+ if (random.Next(0, 2) == 0)
36
+ {
37
+ listView1.Items.Add("項目");
38
+ }
39
+ }
40
+ }
65
41
 
66
-
67
- var random = new System.Random();
68
-
69
- if (random.Next(0, 2) == 0)
70
-
71
- {
72
-
73
- listView1.Items.Add("項目");
74
-
75
- }
76
-
42
+ // [c# - Display empty text when there are no items in ListView Windows forms - Stack Overflow](https://stackoverflow.com/questions/41076244/display-empty-text-when-there-are-no-items-in-listview-windows-forms)
43
+ public class MyListView : ListView
44
+ {
45
+ public MyListView()
46
+ {
47
+ EmptyText = "No data available.";
77
48
  }
78
49
 
79
- }
80
-
81
-
82
-
83
- // [c# - Display empty text when there are no items in ListView Windows forms - Stack Overflow](https://stackoverflow.com/questions/41076244/display-empty-text-when-there-are-no-items-in-listview-windows-forms)
84
-
85
- public class MyListView : ListView
86
-
87
- {
88
-
89
- public MyListView()
90
-
91
- {
92
-
93
- EmptyText = "No data available.";
94
-
95
- }
96
-
97
-
98
-
99
50
  [DefaultValue("No data available.")]
100
-
101
51
  public string EmptyText { get; set; }
102
52
 
103
-
104
-
105
53
  protected override void WndProc(ref Message m)
106
-
107
54
  {
108
-
109
55
  base.WndProc(ref m);
110
-
111
56
  if (m.Msg == 0xF)
112
-
113
57
  {
114
-
115
58
  if (Items.Count == 0)
116
-
117
59
  {
118
-
119
60
  using (var g = Graphics.FromHwnd(Handle))
120
-
121
61
  {
122
-
123
62
  //ど真ん中
124
-
125
63
  //TextRenderer.DrawText(g, EmptyText, Font, ClientRectangle, ForeColor);
126
64
 
127
-
128
-
129
65
  // 上部中央
130
-
131
66
  TextRenderer.DrawText(g, EmptyText, Font, ClientRectangle, ForeColor,
132
-
133
67
  TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak);
134
-
135
68
  }
136
-
137
69
  }
138
-
139
70
  }
140
-
141
71
  }
142
-
143
72
  }
144
-
145
73
  }
146
-
147
74
  ```
148
-
149
75
  ![デザイナ画面](4f586971ea6b1efd641a270c99b801ea.png)