very simple & strange error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave

    #1

    very simple & strange error

    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)

    {


    }

    }

    }


  • Jim

    #2
    Re: very simple & strange error

    I couldn't get KeyPress to work with non-characters either.. Look into
    ProcessDialogKe y. Here's a decent article to get you started:
    http://www.ondotnet.com/pub/a/dotnet...ys.html?page=1 . Page 2
    really has what you are looking for but the whole article is worth the read
    to get the basics down.

    Only problem for me was that it didn't work until I .Show() another form and
    close it.. Then the origional form would capture the keys.. Weird. I'm
    still trying to figure that one out, so please share the wealth if you
    stumble across something.

    good luck.


    "Dave" <davidhere40@ho tmail.com> wrote in message
    news:P82dnQAoIZ 3jPiiiRVn-sA@comcast.com. ..[color=blue]
    > 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[/color]
    program[color=blue]
    > 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,[/color]
    pressing[color=blue]
    > 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)
    >
    > {
    >
    >
    > }
    >
    > }
    >
    > }
    >
    >[/color]


    Comment

    • Dave

      #3
      Re: very simple &amp; strange error

      The answer seems to be that KeyPress is only supposed to work for regular
      characters. that's what that tutorial you gave me was saying. The only
      problem with using ProcessDialogKe y exclussively is that it doesn't know the
      difference between caps and lowercase characters. So, if you need caps and
      lowercase, then use keypress. if not, use processdialogke y. but if you need
      both non-character keys and caps/lowercase regular keys, just use both
      together. That's my solution. does that solve your problem?
      dave
      "Jim" <tranzparency@y ahoo.com> wrote in message
      news:%23QHGDN1q DHA.2188@TK2MSF TNGP09.phx.gbl. ..[color=blue]
      > I couldn't get KeyPress to work with non-characters either.. Look into
      > ProcessDialogKe y. Here's a decent article to get you started:
      > http://www.ondotnet.com/pub/a/dotnet...ys.html?page=1 . Page 2
      > really has what you are looking for but the whole article is worth the[/color]
      read[color=blue]
      > to get the basics down.
      >
      > Only problem for me was that it didn't work until I .Show() another form[/color]
      and[color=blue]
      > close it.. Then the origional form would capture the keys.. Weird. I'm
      > still trying to figure that one out, so please share the wealth if you
      > stumble across something.
      >
      > good luck.
      >
      >
      > "Dave" <davidhere40@ho tmail.com> wrote in message
      > news:P82dnQAoIZ 3jPiiiRVn-sA@comcast.com. ..[color=green]
      > > 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[/color]
      > program[color=green]
      > > when I press the escape button. But pressing the escape button doesn't[/color][/color]
      do[color=blue][color=green]
      > > anything if I put the code for the button into the program! infact,[/color]
      > pressing[color=green]
      > > 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[/color][/color]
      if[color=blue][color=green]
      > > instead of a button, I insert a label. it also doesn't work if I insert[/color][/color]
      a[color=blue][color=green]
      > > 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)
      > >
      > > {
      > >
      > >
      > > }
      > >
      > > }
      > >
      > > }
      > >
      > >[/color]
      >
      >[/color]


      Comment

      Working...