回答編集履歴

1

見直しキャンペーン中

2023/07/17 13:12

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -1,113 +1,57 @@
1
1
  .NET Coreではデザイナはまだプレビューなので悩ましいですね(もう間もなく来るはずです)
2
2
 
3
-
4
-
5
3
  .NET Frameworkで作ってみるとわかりますが
6
-
7
4
  `this.Controls.Add(this.label1);`
8
-
9
5
  が抜けいていますので表示されません。最後のあたりに追加してください。
10
6
 
7
+ 座標を正しく表示するには、`Load`か`LocationChanged`イベントで値を設定することになると思います。
11
8
 
12
-
13
- 座標を正しく表示するには、LoadかLocationChangedイベントで値を設定することになると思います。
14
-
15
-
16
-
17
- ```C#
9
+ ```cs
18
-
19
10
  using System;
20
-
21
11
  using System.Drawing;
22
-
23
12
  using System.Windows.Forms;
24
13
 
25
-
26
-
27
14
  namespace test01
28
-
29
15
  {
30
-
31
16
  public partial class Form1 : Form
32
-
33
17
  {
34
-
35
18
  private Label label1;
36
19
 
37
-
38
-
39
20
  public Form1()
40
-
41
21
  {
42
-
43
22
  InitializeComponent();
44
23
 
45
-
46
-
47
24
  // Form1.Designer.csでもいいですけど注意が必要です
48
-
49
25
  this.label1 = new Label();
50
-
51
26
  this.label1.AutoSize = true;
52
-
53
27
  this.label1.Location = new Point(50, 150);
54
-
55
28
  this.label1.Name = "label1";
56
-
57
29
  this.label1.Size = new Size(100, 50);
58
-
59
30
  this.label1.TabIndex = 1;
60
-
61
-
62
31
 
63
32
  this.Controls.Add(this.label1);
64
33
 
65
-
66
-
67
34
  this.Load += Form1_Load; // 開いた時の位置が分かればいい場合
68
-
69
35
  this.LocationChanged += Form1_LocationChanged; // 常に今の値が欲しい場合
70
-
71
36
  }
72
37
 
73
-
74
-
75
38
  private void Form1_Load(object sender, EventArgs e)
76
-
77
39
  {
78
-
79
40
  int xxx = this.Location.X;
80
-
81
41
  int yyy = this.Location.Y;
82
-
83
42
  string x1 = xxx.ToString("0");
84
-
85
43
  string y1 = yyy.ToString("0");
86
-
87
44
  this.label1.Text = x1 + "," + y1;
88
-
89
45
  }
90
46
 
91
-
92
-
93
47
  private void Form1_LocationChanged(object sender, EventArgs e)
94
-
95
48
  {
96
-
97
49
  int xxx = this.Location.X;
98
-
99
50
  int yyy = this.Location.Y;
100
-
101
51
  string x1 = xxx.ToString("0");
102
-
103
52
  string y1 = yyy.ToString("0");
104
-
105
53
  this.label1.Text = x1 + "," + y1;
106
-
107
54
  }
108
-
109
55
  }
110
-
111
56
  }
112
-
113
57
  ```