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

回答編集履歴

3

double型の対応

2015/02/18 06:30

投稿

uooooo
uooooo

スコア9

answer CHANGED
@@ -11,7 +11,7 @@
11
11
  // label1の位置を変更する
12
12
  public void ChangeLabelLocation(int x, int y)
13
13
  {
14
- this.label1.Location = new Point(x, y);
14
+ this.label1.Location = new System.Drawing.Point(x, y);
15
15
  }
16
16
 
17
17
  private void HogeHoge()

2

double型の対応

2015/02/18 06:30

投稿

uooooo
uooooo

スコア9

answer CHANGED
@@ -4,15 +4,28 @@
4
4
 
5
5
  private void InitializeComponent()
6
6
  {
7
- this.label1 = new System.Windows.Form.Label();
7
+ this.label1 = new System.Windows.Form.Label();
8
- this.label1.Location = new System.Drawing.Point(X座標, Y座標);
8
+ this.label1.Location = new System.Drawing.Point(X座標, Y座標);
9
9
  }
10
10
 
11
11
  // label1の位置を変更する
12
12
  public void ChangeLabelLocation(int x, int y)
13
13
  {
14
- this.label1.Location = new Point(x, y);
14
+ this.label1.Location = new Point(x, y);
15
15
  }
16
+
17
+ private void HogeHoge()
18
+ {
19
+ double x = 1;
20
+ double y = 2;
21
+
22
+ int newX = checked((int)x);
23
+ int newY = checked((int)y);
24
+
25
+ this.ChangeLabelLocation(newX, newY);
26
+ }
16
27
  ```
17
28
  と言った記法が可能です。コントロールの位置は対象のコントロールのLocationプロパティを変更すれば変わります。
18
- メソッド内に変数で座標指定されている場合には、上記サンプルのように外だしのメソッドから変更できるようにしてあげるといいのではないでしょうか。
29
+ メソッド内に変数で座標指定されている場合には、上記サンプルのように外だしのメソッドから変更できるようにしてあげるといいのではないでしょうか。
30
+ Point構造体のXプロパティとYプロパティはそれぞれInt32で定義されているため、double型の座標はキャストをする必要があります。
31
+ 但しこの変換は縮小変換のため、上記サンプルのようにcheckedキーワードでオーバーフローチェックを入れることが望ましいです。(ところでこのトピックはWindowsフォームのControlの座標の話題でよろしいのでしょうか)

1

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

2015/02/18 06:21

投稿

uooooo
uooooo

スコア9

answer CHANGED
@@ -7,5 +7,12 @@
7
7
  this.label1 = new System.Windows.Form.Label();
8
8
  this.label1.Location = new System.Drawing.Point(X座標, Y座標);
9
9
  }
10
+
11
+ // label1の位置を変更する
12
+ public void ChangeLabelLocation(int x, int y)
13
+ {
14
+ this.label1.Location = new Point(x, y);
15
+ }
10
16
  ```
11
- と言った記法が可能です。
17
+ と言った記法が可能です。コントロールの位置は対象のコントロールのLocationプロパティを変更すれば変わります。
18
+ メソッド内に変数で座標指定されている場合には、上記サンプルのように外だしのメソッドから変更できるようにしてあげるといいのではないでしょうか。