迷路を自動生成するものをつくっていて、縦と横の長さ、出入り口の数の数値を入力し
フォーム内に設置したボタンをクリックすると
その数値に応じた長さの線が描画されるようにしているのですが、その数値の範囲を限定させたいのです。
範囲の限定についてですが、
・入力する数値の最大最小を決めて、その範囲外の数字や文字が入力されても実行画面では反応はないようにしたい。
・範囲内の数値が入力されたときのみ応答がある。
・範囲は最小2から最大50まで、出入り口は最小2から最大4までを考えている。
以下は現在のコードです。
private void CreateSoto() { private int tate; private int yoko; private int deiriguti; private int x_start; private int x_end; private int y_start; private int y_end; private Image _img; string s = this.textBox1.Text; string a = this.textBox2.Text; string d = this.textBox3.Text; int result; if (int.TryParse(s, out result)) { tate = result; } if (int.TryParse(a, out result)) { yoko = result; } if (int.TryParse(d, out result)) { deiriguti = result; } //テキストボックス入力で枠線の値を変更 float x_start = (yoko * 20); float y_start = (tate * 20); float y_end = (tate * 20) + 10; float x_end = (yoko * 20) + 10; // pictureboxのサイズを変更 this.pictureBox1.Width = 10 + (int)x_end + 10; this.pictureBox1.Height = 10 + (int)y_end + 10; Bitmap bmp = new Bitmap(this.pictureBox1.Width, this.pictureBox1.Height); //ImageオブジェクトのGraphicsオブジェクトを作成する Graphics g = Graphics.FromImage(bmp); //出入り口の数を設定 switch (deiriguti) { case 2: //左の枠線 g.DrawLine(Black, 10, 30, 10, y_end);//x始点は固定 //右の枠線 g.DrawLine(Black, x_end, 10, x_end, y_end - 20);//y始点は固定 //上の枠線 g.DrawLine(Black, 10, 10, x_end, 10);//始点と終点yは固定 //下の枠線 g.DrawLine(Black, 10, y_end, x_end, y_end); break; case 3: //左の枠線 g.DrawLine(Black, 10, 30, 10, y_end);//x始点は固定 //右の枠線 g.DrawLine(Black, x_end, 10, x_end, y_end - 20);//y始点は固定 //上の枠線 g.DrawLine(Black, 10, 10, x_end - 20, 10);//始点と終点yは固定 //下の枠線 g.DrawLine(Black, 10, y_end, x_end, y_end); break; case 4: //左の枠線 g.DrawLine(Black, 10, 30, 10, y_end);//x始点は固定 //右の枠線 g.DrawLine(Black, x_end, 10, x_end, y_end - 20);//y始点は固定 //上の枠線 g.DrawLine(Black, 10, 10, x_end - 20, 10);//始点と終点yは固定 //下の枠線 g.DrawLine(Black, 30, y_end, x_end, y_end); break; } //リソースを解放する g.Dispose(); //PictureBox1に表示する pictureBox1.Image = bmp; _img = bmp; }
入力された値が範囲内か判断すればいいだけでは無いですか?
丸々なサンプルを要求されても困るので例えば先に入力値の範囲判定していないサンプルをご自身で用意(提示)されてはどうでしょうか…それをどう改造すればいいか、というアドバイスの方が回答を得られやすいと思います。