I build a very simple program that I put at the end of this post. This
program is just supposed to put a button on the screen and exit the program
when I press the escape button. But pressing the escape button doesn't do
anything if I put the code for the button into the program! infact, pressing
escape only works if you take away the little piece of code:
"this.Controls. Add(this.button 1);"!! I was totally confused at why this
happened and would really like to fix it. any ideas? Ps: the code works if
instead of a button, I insert a label. it also doesn't work if I insert a
textbox.
thanks
dave
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using Microsoft.Direc tX;
using Microsoft.Direc tX.Direct3D;
using System.Threadin g;
namespace re
{
public class Form1 : Form
{
private System.Windows. Forms.Button button1;
public Form1()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
this.button1.Lo cation = new System.Drawing. Point(670, 220);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
this.KeyUp += new KeyEventHandler (OnKeyPress);
this.Controls.A dd(this.button1 );
}
public void OnKeyPress(obje ct sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Escape)
Application.Exi t();
}
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
}
}
}
program is just supposed to put a button on the screen and exit the program
when I press the escape button. But pressing the escape button doesn't do
anything if I put the code for the button into the program! infact, pressing
escape only works if you take away the little piece of code:
"this.Controls. Add(this.button 1);"!! I was totally confused at why this
happened and would really like to fix it. any ideas? Ps: the code works if
instead of a button, I insert a label. it also doesn't work if I insert a
textbox.
thanks
dave
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;
using Microsoft.Direc tX;
using Microsoft.Direc tX.Direct3D;
using System.Threadin g;
namespace re
{
public class Form1 : Form
{
private System.Windows. Forms.Button button1;
public Form1()
{
this.button1 = new System.Windows. Forms.Button();
this.SuspendLay out();
this.button1.Lo cation = new System.Drawing. Point(670, 220);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "button1";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
this.KeyUp += new KeyEventHandler (OnKeyPress);
this.Controls.A dd(this.button1 );
}
public void OnKeyPress(obje ct sender, KeyEventArgs e)
{
if(e.KeyCode == Keys.Escape)
Application.Exi t();
}
static void Main()
{
Application.Run (new Form1());
}
private void button1_Click(o bject sender, System.EventArg s e)
{
}
}
}
Comment