質問編集履歴
1
内容の修正
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
C# テキストボックスに入力
|
1
|
+
C# テキストボックスに入力した数値の範囲限定
|
body
CHANGED
@@ -1,4 +1,111 @@
|
|
1
|
+
迷路を自動生成するものをつくっていて、縦と横の長さ、出入り口の数の数値を入力し
|
2
|
+
フォーム内に設置したボタンをクリックすると
|
1
|
-
|
3
|
+
その数値に応じた長さの線が描画されるようにしているのですが、その数値の範囲を限定させたいのです。
|
4
|
+
|
2
|
-
|
5
|
+
範囲の限定についてですが、
|
3
|
-
|
6
|
+
・入力する数値の最大最小を決めて、その範囲外の数字や文字が入力されても実行画面では反応はないようにしたい。
|
7
|
+
・範囲内の数値が入力されたときのみ応答がある。
|
8
|
+
・範囲は最小2から最大50まで、出入り口は最小2から最大4までを考えている。
|
9
|
+
|
10
|
+
以下は現在のコードです。
|
11
|
+
```
|
12
|
+
private void CreateSoto()
|
13
|
+
{
|
14
|
+
|
15
|
+
private int tate;
|
16
|
+
private int yoko;
|
17
|
+
private int deiriguti;
|
18
|
+
private int x_start;
|
19
|
+
private int x_end;
|
20
|
+
private int y_start;
|
21
|
+
private int y_end;
|
22
|
+
private Image _img;
|
23
|
+
|
24
|
+
string s = this.textBox1.Text;
|
25
|
+
string a = this.textBox2.Text;
|
26
|
+
string d = this.textBox3.Text;
|
27
|
+
int result;
|
28
|
+
|
29
|
+
if (int.TryParse(s, out result))
|
30
|
+
{
|
31
|
+
tate = result;
|
32
|
+
}
|
33
|
+
if (int.TryParse(a, out result))
|
34
|
+
{
|
35
|
+
yoko = result;
|
36
|
+
}
|
37
|
+
if (int.TryParse(d, out result))
|
38
|
+
{
|
39
|
+
deiriguti = result;
|
40
|
+
}
|
41
|
+
|
42
|
+
|
43
|
+
|
44
|
+
//テキストボックス入力で枠線の値を変更
|
45
|
+
float x_start = (yoko * 20);
|
46
|
+
float y_start = (tate * 20);
|
47
|
+
float y_end = (tate * 20) + 10;
|
48
|
+
float x_end = (yoko * 20) + 10;
|
49
|
+
|
50
|
+
// pictureboxのサイズを変更
|
51
|
+
this.pictureBox1.Width = 10 + (int)x_end + 10;
|
52
|
+
this.pictureBox1.Height = 10 + (int)y_end + 10;
|
53
|
+
|
4
|
-
|
54
|
+
Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height);
|
55
|
+
//ImageオブジェクトのGraphicsオブジェクトを作成する
|
56
|
+
Graphics g = Graphics.FromImage(bmp);
|
57
|
+
|
58
|
+
|
59
|
+
//出入り口の数を設定
|
60
|
+
switch (deiriguti)
|
61
|
+
{
|
62
|
+
case 2:
|
63
|
+
//左の枠線
|
64
|
+
g.DrawLine(Black, 10, 30, 10, y_end);//x始点は固定
|
65
|
+
|
66
|
+
//右の枠線
|
67
|
+
g.DrawLine(Black, x_end, 10, x_end, y_end - 20);//y始点は固定
|
68
|
+
|
69
|
+
//上の枠線
|
70
|
+
g.DrawLine(Black, 10, 10, x_end, 10);//始点と終点yは固定
|
71
|
+
|
72
|
+
//下の枠線
|
73
|
+
g.DrawLine(Black, 10, y_end, x_end, y_end);
|
74
|
+
break;
|
75
|
+
case 3:
|
76
|
+
//左の枠線
|
77
|
+
g.DrawLine(Black, 10, 30, 10, y_end);//x始点は固定
|
78
|
+
|
79
|
+
//右の枠線
|
80
|
+
g.DrawLine(Black, x_end, 10, x_end, y_end - 20);//y始点は固定
|
81
|
+
|
82
|
+
//上の枠線
|
83
|
+
g.DrawLine(Black, 10, 10, x_end - 20, 10);//始点と終点yは固定
|
84
|
+
|
85
|
+
//下の枠線
|
86
|
+
g.DrawLine(Black, 10, y_end, x_end, y_end);
|
87
|
+
break;
|
88
|
+
case 4:
|
89
|
+
//左の枠線
|
90
|
+
g.DrawLine(Black, 10, 30, 10, y_end);//x始点は固定
|
91
|
+
|
92
|
+
//右の枠線
|
93
|
+
g.DrawLine(Black, x_end, 10, x_end, y_end - 20);//y始点は固定
|
94
|
+
|
95
|
+
//上の枠線
|
96
|
+
g.DrawLine(Black, 10, 10, x_end - 20, 10);//始点と終点yは固定
|
97
|
+
|
98
|
+
//下の枠線
|
99
|
+
g.DrawLine(Black, 30, y_end, x_end, y_end);
|
100
|
+
break;
|
101
|
+
}
|
102
|
+
|
103
|
+
//リソースを解放する
|
104
|
+
g.Dispose();
|
105
|
+
|
106
|
+
//PictureBox1に表示する
|
107
|
+
pictureBox1.Image = bmp;
|
108
|
+
_img = bmp;
|
109
|
+
}
|
110
|
+
|
111
|
+
```
|