概要
cscの作法、調べてみた。
数値入力inbox書いてみた。
写真
サンプルコード
using System;
using System.Windows.Forms;
using System.Text;
using System.IO;
using System.Drawing;
namespace App
{
public partial class SubForm: Form {
NumericUpDown numericUpDown1;
public int InputValue {
get;
private set;
}
public SubForm() {
Text = "数値入力";
Button btnOK = new Button();
btnOK.Location = new Point(60, 160);
btnOK.Text = "OK";
btnOK.Click += btnOK_Click;
Button btnCancel = new Button();
btnCancel.Location = new Point(160, 160);
btnCancel.Text = "CANCEL";
numericUpDown1 = new NumericUpDown();
numericUpDown1.Minimum = 100;
numericUpDown1.Maximum = 200;
numericUpDown1.Value = 150;
numericUpDown1.Location = new Point(80, 40);
Controls.AddRange(new Control[] {
btnOK,
btnCancel,
numericUpDown1
});
btnOK.DialogResult = DialogResult.OK;
btnCancel.DialogResult = DialogResult.Cancel;
this.AcceptButton = btnOK;
this.CancelButton = btnCancel;
}
private void btnOK_Click(object sender, EventArgs e) {
InputValue = (int) numericUpDown1.Value;
}
}
public partial class MainForm: Form {
public MainForm() {
Text = "数値入力";
Button btn1 = new Button();
btn1.Location = new Point(80, 100);
btn1.Text = "数値入力";
btn1.Click += btn1_Click;
Controls.AddRange(new Control[] {
btn1
});
}
private void btn1_Click(object sender, EventArgs e) {
using (SubForm subForm = new SubForm())
{
if (subForm.ShowDialog() == DialogResult.OK)
{
int result = subForm.InputValue;
MessageBox.Show(string.Format("入力された数値は{0}です。", result), "結果");
}
else
{
MessageBox.Show("入力をキャンセルしました。", "通知");
}
}
}
[STAThread]
public static void Main() {
Application.Run(new MainForm());
}
}
}
以上。
