前田稔(Maeda Minoru)の超初心者のプログラム入門
![]()
![]()
public static string msg = "Text Message";
private void Button_Click(object sender, EventArgs e)
{ MyDialog Mydlg = new MyDialog();
DialogResult dr = Mydlg.ShowDialog();
if (dr == DialogResult.OK)
{
msg = Mydlg.str;
Invalidate();
}
}
|
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
Graphics g = e.Graphics;
g.DrawString(msg, Font, Brushes.Black, new PointF(10f, 10f));
}
|
class MyDialog : Form
{
TextBox txtBox;
public string str;
public MyDialog()
{
Text = "Dialog Text Input";
MaximizeBox = false;
MinimizeBox = false;
ControlBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
Width = 250;
Height = 130;
Button btnOK = new Button();
btnOK.Text = "OK";
btnOK.Location = new Point(30, ClientSize.Height - btnOK.Height - 5);
btnOK.Parent = this;
btnOK.TabIndex = 1;
btnOK.Click += new EventHandler(btnOK_Click);
btnOK.DialogResult = DialogResult.OK;
Button btnCancel = new Button();
btnCancel.Text = "Cancel";
btnCancel.Location = new Point(ClientSize.Width - btnCancel.Width - 30,
ClientSize.Height - btnCancel.Height - 5);
btnCancel.Parent = this;
btnCancel.TabIndex = 2;
btnCancel.DialogResult = DialogResult.Cancel;
txtBox = new TextBox();
txtBox.Parent = this;
txtBox.Location = new Point(10, 10);
txtBox.Width = ClientSize.Width - 20;
txtBox.TabIndex = 0;
}
void btnOK_Click(object sender, EventArgs e)
{
str = txtBox.Text;
}
}
|
void btnOK_Click(object sender, EventArgs e)
{
str = txtBox.Text;
}
|
if (dr == DialogResult.OK)
{
msg = Mydlg.str;
Invalidate();
}
|
![]()
パラメータでthisを渡します。
private void Button_Click(object sender, EventArgs e)
{
MyDialog Mydlg = new MyDialog(this);
Mydlg.ShowDialog();
}
|
class MyDialog : Form
{
Form parent;
public MyDialog(Form pform)
{
parent = pform;
Text = "MyDialogClass";
MaximizeBox = false;
MinimizeBox = false;
ShowInTaskbar = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
ControlBox = false;
Button btnOK = new Button();
btnOK.Parent = this;
btnOK.Text = "OK";
btnOK.Location = new Point(
(ClientSize.Width - btnOK.Width) / 2,
ClientSize.Height - btnOK.Height - 5);
btnOK.TabIndex = 1;
btnOK.DialogResult = DialogResult.OK;
TextBox textbox = new TextBox();
textbox.Parent = this;
textbox.Multiline = true;
textbox.ScrollBars = ScrollBars.Vertical;
textbox.WordWrap = true;
textbox.Location = new Point(10, 10);
textbox.Width = ClientSize.Width - 20;
textbox.Height = ClientSize.Height - 25 - btnOK.Height;
textbox.AcceptsTab = true;
textbox.TabIndex = 0;
textbox.TextChanged += new EventHandler(textbox_TextChanged);
}
void textbox_TextChanged(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
Form1.str = tb.Text;
parent.Invalidate();
}
}
|
![]()
string str;
Console.WriteLine("データをタイプして下さい?");
str= Console.ReadLine();
MessageBox.Show(str);
|
![]()