回答編集履歴

3

double型の対応

2015/02/18 06:30

投稿

uooooo
uooooo

スコア9

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  {
26
26
 
27
- this.label1.Location = new Point(x, y);
27
+ this.label1.Location = new System.Drawing.Point(x, y);
28
28
 
29
29
  }
30
30
 

2

double型の対応

2015/02/18 06:30

投稿

uooooo
uooooo

スコア9

test CHANGED
@@ -10,9 +10,9 @@
10
10
 
11
11
  {
12
12
 
13
- this.label1 = new System.Windows.Form.Label();
13
+ this.label1 = new System.Windows.Form.Label();
14
14
 
15
- this.label1.Location = new System.Drawing.Point(X座標, Y座標);
15
+ this.label1.Location = new System.Drawing.Point(X座標, Y座標);
16
16
 
17
17
  }
18
18
 
@@ -24,7 +24,29 @@
24
24
 
25
25
  {
26
26
 
27
- this.label1.Location = new Point(x, y);
27
+ this.label1.Location = new Point(x, y);
28
+
29
+ }
30
+
31
+
32
+
33
+ private void HogeHoge()
34
+
35
+ {
36
+
37
+ double x = 1;
38
+
39
+ double y = 2;
40
+
41
+
42
+
43
+ int newX = checked((int)x);
44
+
45
+ int newY = checked((int)y);
46
+
47
+
48
+
49
+ this.ChangeLabelLocation(newX, newY);
28
50
 
29
51
  }
30
52
 
@@ -33,3 +55,7 @@
33
55
  と言った記法が可能です。コントロールの位置は対象のコントロールのLocationプロパティを変更すれば変わります。
34
56
 
35
57
  メソッド内に変数で座標指定されている場合には、上記サンプルのように外だしのメソッドから変更できるようにしてあげるといいのではないでしょうか。
58
+
59
+ Point構造体のXプロパティとYプロパティはそれぞれInt32で定義されているため、double型の座標はキャストをする必要があります。
60
+
61
+ 但しこの変換は縮小変換のため、上記サンプルのようにcheckedキーワードでオーバーフローチェックを入れることが望ましいです。(ところでこのトピックはWindowsフォームのControlの座標の話題でよろしいのでしょうか)

1

トピックの修正に対しての回答

2015/02/18 06:21

投稿

uooooo
uooooo

スコア9

test CHANGED
@@ -16,6 +16,20 @@
16
16
 
17
17
  }
18
18
 
19
+
20
+
21
+ // label1の位置を変更する
22
+
23
+ public void ChangeLabelLocation(int x, int y)
24
+
25
+ {
26
+
27
+ this.label1.Location = new Point(x, y);
28
+
29
+ }
30
+
19
31
  ```
20
32
 
21
- と言った記法が可能です。
33
+ と言った記法が可能です。コントロールの位置は対象のコントロールのLocationプロパティを変更すれば変わります。
34
+
35
+ メソッド内に変数で座標指定されている場合には、上記サンプルのように外だしのメソッドから変更できるようにしてあげるといいのではないでしょうか。