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

回答編集履歴

1

見直しキャンペーン中

2023/07/17 13:12

投稿

TN8001
TN8001

スコア10111

answer CHANGED
@@ -1,57 +1,57 @@
1
- .NET Coreではデザイナはまだプレビューなので悩ましいですね(もう間もなく来るはずです)
2
-
3
- .NET Frameworkで作ってみるとわかりますが
4
- `this.Controls.Add(this.label1);`
5
- が抜けいていますので表示されません。最後のあたりに追加してください。
6
-
7
- 座標を正しく表示するには、LoadかLocationChangedイベントで値を設定することになると思います。
8
-
9
- ```C#
10
- using System;
11
- using System.Drawing;
12
- using System.Windows.Forms;
13
-
14
- namespace test01
15
- {
16
- public partial class Form1 : Form
17
- {
18
- private Label label1;
19
-
20
- public Form1()
21
- {
22
- InitializeComponent();
23
-
24
- // Form1.Designer.csでもいいですけど注意が必要です
25
- this.label1 = new Label();
26
- this.label1.AutoSize = true;
27
- this.label1.Location = new Point(50, 150);
28
- this.label1.Name = "label1";
29
- this.label1.Size = new Size(100, 50);
30
- this.label1.TabIndex = 1;
31
-
32
- this.Controls.Add(this.label1);
33
-
34
- this.Load += Form1_Load; // 開いた時の位置が分かればいい場合
35
- this.LocationChanged += Form1_LocationChanged; // 常に今の値が欲しい場合
36
- }
37
-
38
- private void Form1_Load(object sender, EventArgs e)
39
- {
40
- int xxx = this.Location.X;
41
- int yyy = this.Location.Y;
42
- string x1 = xxx.ToString("0");
43
- string y1 = yyy.ToString("0");
44
- this.label1.Text = x1 + "," + y1;
45
- }
46
-
47
- private void Form1_LocationChanged(object sender, EventArgs e)
48
- {
49
- int xxx = this.Location.X;
50
- int yyy = this.Location.Y;
51
- string x1 = xxx.ToString("0");
52
- string y1 = yyy.ToString("0");
53
- this.label1.Text = x1 + "," + y1;
54
- }
55
- }
56
- }
1
+ .NET Coreではデザイナはまだプレビューなので悩ましいですね(もう間もなく来るはずです)
2
+
3
+ .NET Frameworkで作ってみるとわかりますが
4
+ `this.Controls.Add(this.label1);`
5
+ が抜けいていますので表示されません。最後のあたりに追加してください。
6
+
7
+ 座標を正しく表示するには、`Load``LocationChanged`イベントで値を設定することになると思います。
8
+
9
+ ```cs
10
+ using System;
11
+ using System.Drawing;
12
+ using System.Windows.Forms;
13
+
14
+ namespace test01
15
+ {
16
+ public partial class Form1 : Form
17
+ {
18
+ private Label label1;
19
+
20
+ public Form1()
21
+ {
22
+ InitializeComponent();
23
+
24
+ // Form1.Designer.csでもいいですけど注意が必要です
25
+ this.label1 = new Label();
26
+ this.label1.AutoSize = true;
27
+ this.label1.Location = new Point(50, 150);
28
+ this.label1.Name = "label1";
29
+ this.label1.Size = new Size(100, 50);
30
+ this.label1.TabIndex = 1;
31
+
32
+ this.Controls.Add(this.label1);
33
+
34
+ this.Load += Form1_Load; // 開いた時の位置が分かればいい場合
35
+ this.LocationChanged += Form1_LocationChanged; // 常に今の値が欲しい場合
36
+ }
37
+
38
+ private void Form1_Load(object sender, EventArgs e)
39
+ {
40
+ int xxx = this.Location.X;
41
+ int yyy = this.Location.Y;
42
+ string x1 = xxx.ToString("0");
43
+ string y1 = yyy.ToString("0");
44
+ this.label1.Text = x1 + "," + y1;
45
+ }
46
+
47
+ private void Form1_LocationChanged(object sender, EventArgs e)
48
+ {
49
+ int xxx = this.Location.X;
50
+ int yyy = this.Location.Y;
51
+ string x1 = xxx.ToString("0");
52
+ string y1 = yyy.ToString("0");
53
+ this.label1.Text = x1 + "," + y1;
54
+ }
55
+ }
56
+ }
57
57
  ```