Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Visual J# / April 2005

Tip: Looking for answers? Try searching our database.

Arrow keys

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Uday Kanetkar - 15 Apr 2005 07:12 GMT
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
changed.  How can I fix that?
Lars-Inge T?nnessen [VJ# MVP] - 17 Apr 2005 00:01 GMT
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

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.