all groups > vj# > april 2005 >
You're in the

vj#

group:

Arrow keys


Arrow keys Uday Kanetkar
4/14/2005 11:12:04 PM
vj#:
We have developed a game by using keys to move a button
in the form area. We are using the keypress event for
move the button. We would like to use arrow keys to move
the button. Instead when we use arrow keys the focus is
Re: Arrow keys Lars-Inge Tønnessen [VJ# MVP]
4/16/2005 12:00:00 AM
Hello Uday,

Please make a lost focus handler. Use this handler to set focus back to the
button. Here is an example I have made for you.

package KeyApp;
import System.Drawing.*;
import System.Collections.*;
import System.ComponentModel.*;
import System.Windows.Forms.*;
import System.Data.*;

public class Form1 extends System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Button button2;
private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected void Dispose(boolean disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
super.Dispose(disposing);
}

private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.set_Location(new System.Drawing.Point(136, 96));
this.button1.set_Name("button1");
this.button1.set_TabIndex(0);
this.button1.set_Text("button1");
this.button1.add_KeyUp( new
System.Windows.Forms.KeyEventHandler(this.ButtKeyHandler) );
this.button1.add_Leave( new System.EventHandler(this.focusme) );
//
// button2
//
this.button2.set_Location(new System.Drawing.Point(136, 176));
this.button2.set_Name("button2");
this.button2.set_TabIndex(1);
this.button2.set_Text("button2");
this.button2.add_Click( new System.EventHandler(this.button2_Click) );
//
// Form1
//
this.set_AutoScaleBaseSize(new System.Drawing.Size(5, 13));
this.set_ClientSize(new System.Drawing.Size(616, 486));
this.get_Controls().Add(this.button2);
this.get_Controls().Add(this.button1);
this.set_Name("Form1");
this.set_Text("Form1");
this.add_KeyUp( new
System.Windows.Forms.KeyEventHandler(this.KeyHandler) );
this.ResumeLayout(false);
}

/**
* The main entry point for the application.
*/
/** @attribute System.STAThread() */
public static void main(String[] args)
{
Application.Run(new Form1());
}

private void KeyHandler (Object sender, System.Windows.Forms.KeyEventArgs
e)
{
ButtKeyHandler( sender, e );
}

private void ButtKeyHandler (Object sender,
System.Windows.Forms.KeyEventArgs e)
{
// Left
if ( e.get_KeyValue() == 39 )
{
this.button1.set_Location( new System.Drawing.Point(
this.button1.get_Location().get_X() + 5,
this.button1.get_Location().get_Y()) );
}

// down
if ( e.get_KeyValue() == 40 )
{
this.button1.set_Location( new System.Drawing.Point(
this.button1.get_Location().get_X(),
this.button1.get_Location().get_Y() + 5) );
}

// Right
if ( e.get_KeyValue() == 37 )
{
this.button1.set_Location( new System.Drawing.Point(
this.button1.get_Location().get_X() - 5,
this.button1.get_Location().get_Y()) );
}

// Up
if ( e.get_KeyValue() == 38 )
{
this.button1.set_Location( new System.Drawing.Point(
this.button1.get_Location().get_X(),
this.button1.get_Location().get_Y() - 5) );
}
this.button1.Focus();
}

// Sets the focus back to the button.
private void focusme (Object sender, System.EventArgs e)
{
this.button1.Focus();
}

private void button2_Click (Object sender, System.EventArgs e)
{
System.Windows.Forms.MessageBox.Show("I'm button 2");
}
}



Regards,
Lars-Inge Tønnessen

AddThis Social Bookmark Button