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

回答編集履歴

1

見直しキャンペーン中

2023/07/27 14:44

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,75 +1,75 @@
1
- あまり意味は分かっていませんが、こちらがズバリな感じです。
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)
3
-
4
- 入れ替え手順
5
- 1. `MyListView`クラスを`Form1`の下か別ファイル(MyListView.cs)に書く
6
- 1. 一度実行(ビルド)
7
- 1. デザイナで`listView1`を削除し、ツールボックスに新たに出た`MyListView`を追加(反映に少し時間がかかる場合あり)
8
- 1. `EmptyText`に好きなメッセージをいれる
9
- 1. `(Name)`が`myListView1`になっているので、`listView1`に変える
10
-
11
- 以上
12
-
13
- ```C#
14
- using System;
15
- using System.ComponentModel;
16
- using System.Drawing;
17
- using System.Windows.Forms;
18
-
19
- namespace Questions341965
20
- {
21
- public partial class Form1 : Form
22
- {
23
- public Form1()
24
- {
25
- InitializeComponent();
26
- }
27
-
28
- private void Form1_Load(object sender, EventArgs e) { }
29
-
30
- private void button1_Click(object sender, EventArgs e)
31
- {
32
- listView1.Items.Clear();
33
-
34
- var random = new System.Random();
35
- if (random.Next(0, 2) == 0)
36
- {
37
- listView1.Items.Add("項目");
38
- }
39
- }
40
- }
41
-
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.";
48
- }
49
-
50
- [DefaultValue("No data available.")]
51
- public string EmptyText { get; set; }
52
-
53
- protected override void WndProc(ref Message m)
54
- {
55
- base.WndProc(ref m);
56
- if (m.Msg == 0xF)
57
- {
58
- if (Items.Count == 0)
59
- {
60
- using (var g = Graphics.FromHwnd(Handle))
61
- {
62
- //ど真ん中
63
- //TextRenderer.DrawText(g, EmptyText, Font, ClientRectangle, ForeColor);
64
-
65
- // 上部中央
66
- TextRenderer.DrawText(g, EmptyText, Font, ClientRectangle, ForeColor,
67
- TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak);
68
- }
69
- }
70
- }
71
- }
72
- }
73
- }
74
- ```
1
+ あまり意味は分かっていませんが、こちらがズバリな感じです。
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)
3
+
4
+ 入れ替え手順
5
+ 1. `MyListView`クラスを`Form1`の下か別ファイル(MyListView.cs)に書く
6
+ 1. 一度実行(ビルド)
7
+ 1. デザイナで`listView1`を削除し、ツールボックスに新たに出た`MyListView`を追加(反映に少し時間がかかる場合あり)
8
+ 1. `EmptyText`に好きなメッセージをいれる
9
+ 1. `(Name)`が`myListView1`になっているので、`listView1`に変える
10
+
11
+ 以上
12
+
13
+ ```cs
14
+ using System;
15
+ using System.ComponentModel;
16
+ using System.Drawing;
17
+ using System.Windows.Forms;
18
+
19
+ namespace Questions341965
20
+ {
21
+ public partial class Form1 : Form
22
+ {
23
+ public Form1()
24
+ {
25
+ InitializeComponent();
26
+ }
27
+
28
+ private void Form1_Load(object sender, EventArgs e) { }
29
+
30
+ private void button1_Click(object sender, EventArgs e)
31
+ {
32
+ listView1.Items.Clear();
33
+
34
+ var random = new System.Random();
35
+ if (random.Next(0, 2) == 0)
36
+ {
37
+ listView1.Items.Add("項目");
38
+ }
39
+ }
40
+ }
41
+
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.";
48
+ }
49
+
50
+ [DefaultValue("No data available.")]
51
+ public string EmptyText { get; set; }
52
+
53
+ protected override void WndProc(ref Message m)
54
+ {
55
+ base.WndProc(ref m);
56
+ if (m.Msg == 0xF)
57
+ {
58
+ if (Items.Count == 0)
59
+ {
60
+ using (var g = Graphics.FromHwnd(Handle))
61
+ {
62
+ //ど真ん中
63
+ //TextRenderer.DrawText(g, EmptyText, Font, ClientRectangle, ForeColor);
64
+
65
+ // 上部中央
66
+ TextRenderer.DrawText(g, EmptyText, Font, ClientRectangle, ForeColor,
67
+ TextFormatFlags.HorizontalCenter | TextFormatFlags.WordBreak);
68
+ }
69
+ }
70
+ }
71
+ }
72
+ }
73
+ }
74
+ ```
75
75
  ![デザイナ画面](4f586971ea6b1efd641a270c99b801ea.png)