picturebox doesnt recognize keyevents without another listbox !?!

Mar 31 2004 3:28 PM
Hi there, I tried to solve my problem for the last five days and I didnt got any solution. I try to implement a little windows game, look at the pic. there is an "game screen" where you can see your character and at the bottom are two listboxes (this is a old game from the times of windows 3.1, and I want to renew it). In the game, if you press one of the cursor keys, your character move in that direction. If something happens(fighting,spelling or using), the listbox at the bottom will show a message. I implemented a picturebox for the "game screen" and two listboxes for the "news". If I add the keyevent handler only to the "game screen" and the whole form, and start the program, it doesnt response to the pressed keys !!! (Thats absolutly unlogical !!) Now, when I add a keyevent handler to one of the listboxes and start the program and press a key, a message is generated and showed in the "news", as it should. But the negativ effect is, that, if you press up and down several times, the selection in the listbox make the same movement which is very nasty and absolutly not intended !! Can someone explain me, why I have to add tgis "extra key handler" and why without it the picturebox and the form doesnt response ? I thought it is because the picturebox cannot be focused or selected, so I tried to use another listbox in which I draw Images instead, but this made only more problems. Is there any class which can be focused and selected, in which I can only draw (through a Graphic) ? In the picturebox I can only show Images, so I have to rebuild the "game scene" on every movement to a new complete Image. Repainting only the regions where really something moved is more efficient. Maybe someone can tell me a solution or write my own class ! Thanks a lot !! Here's the source code: using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace Spiel_Test { public class Spiel : System.Windows.Forms.Form { private System.ComponentModel.Container components = null; ListBox News_Box,Stats_Box; PictureBox Game_PictureBox; protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Graphics dc = e.Graphics; } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } private void InitializeComponent() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Spiel)); this.Game_PictureBox = new PictureBox(); this.News_Box = new System.Windows.Forms.ListBox(); this.Stats_Box = new System.Windows.Forms.ListBox(); this.SuspendLayout(); this.Game_PictureBox.Location = new System.Drawing.Point(0, 0); this.Game_PictureBox.Name = "Picture_Box"; this.Game_PictureBox.Size = new System.Drawing.Size(400, 300); this.Game_PictureBox.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown); // // News_Box // this.News_Box.Location = new System.Drawing.Point(0, 300); this.News_Box.Name = "News_Box"; this.News_Box.ScrollAlwaysVisible = true; this.News_Box.Size = new System.Drawing.Size(300, 93); this.News_Box.TabIndex = 0; ///////////////////////////////// //remove the next two // and recompile !!!!! //this.News_Box.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown); //////////////////////////////// // Stats_Box // this.Stats_Box.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.Stats_Box.Location = new System.Drawing.Point(300, 300); this.Stats_Box.Name = "Stats_Box"; this.Stats_Box.Size = new System.Drawing.Size(100, 93); this.Stats_Box.TabIndex = 0; //this.Stats_Box.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown); // // Spiel // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.AutoScroll = true; this.AutoScrollMinSize = new System.Drawing.Size(400, 400); this.ClientSize = new System.Drawing.Size(400, 400); this.Controls.Add(this.Game_PictureBox); this.Controls.Add(this.News_Box); this.Controls.Add(this.Stats_Box); this.Name = "Spiel"; this.Text = "Spiel"; this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.OnKeyDown); this.ResumeLayout(false); } public void OnKeyDown(object sender, KeyEventArgs e) { string key = e.KeyCode.ToString(); //Action performed from News_Box int Max_News_Box_Items=3; if (News_Box.Items.Count > Max_News_Box_Items) { News_Box.Items.Add("You pressed " + key); News_Box.Items.RemoveAt(0); } else News_Box.Items.Add("You moved " + key); //Action performed from Stats_Box Stats_Box.Items.Clear(); Stats_Box.Items.Add("test"); Stats_Box.Items.Add("tes2"); } //Standard-Konstruktor public Spiel() { InitializeComponent(); } // Der Haupteinstiegspunkt für die Anwendung. [STAThread] static void Main() { Application.Run(new Spiel()); } } }

Answers (2)