C# Geeignetes Key - Ereignis

Neue Frage »

Auf diesen Beitrag antworten »
InformaTiger C# Geeignetes Key - Ereignis

Hallo,
ich suche für das Projekt (siehe Anhang) ein geeignetes Key Ereignis welches mir mit folgendem Code die Location des aktiven Buttons verändert:

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
switch (e.KeyCode)
              {
                  case Keys.Left:
                      {
                          aVariablePosition = btn1.Location;
                          aVariablePosition.X--;
                          btn1.Location = aVariablePosition;
                          break;
                      }
                  case Keys.Right:
                      {
                          aVariablePosition = btn1.Location;
                          aVariablePosition.X++;
                          btn1.Location = aVariablePosition;
                          break;
                      }
                  case Keys.Up:
                      {
                          aVariablePosition = btn1.Location;
                          aVariablePosition.Y--;
                          btn1.Location = aVariablePosition;
                          break;
                      }
                  case Keys.Down:
                     {
                          aVariablePosition = btn1.Location;
                          aVariablePosition.Y++;
                          btn1.Location = aVariablePosition;
                          break;
                      }
              }


Im Anhang ist ein Screenshot des Projekts um besser zu verstehen was ich meine.

Wie im Bild zu sehen ist:
ein Button wurde bereits verschoben und jetzt da er aktiv ist, sollte es anhand des Codes von oben möglich sein den Button mit den Pfeiltasten zu verschieben.

smile

Lg
InformaTiger
 
Auf diesen Beitrag antworten »
eulerscheZahl

Nimm KeyDown oder KeyUp (gibt noch KeyPress, das reagiert aber nicht auf Pfeiltasten).

Also entweder dann Ereignis der aktiven Komponente auswerten, oder so:
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
        private void Form1_Load(object sender, EventArgs e)
        {
            this.KeyPreview = true;
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            MessageBox.Show(e.KeyCode.ToString());
        }

KeyPreview sorgt dafür, dass Form1 das Ereignis mitgeteilt bekommt (auch wenn du z.B. in eine TextBox schreibst).
Auf diesen Beitrag antworten »
InformaTiger

Ich habe das gerade so versucht, leider wechselt der Computer nur zwischen den Buttons...
vielleicht ist ja bei meinem Switch- Code etwas falsch. geschockt

Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

Landet das Programm im switch-Block?
 
Auf diesen Beitrag antworten »
InformaTiger

Ja!

Ok, jetzt funktioniert es... hab nur ne Kleinigkeit vergessen...

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
 if (btn2.Focused == true)
              {
                  switch (e.KeyCode)
                  {
                      case Keys.A:
                          {
                              aVariablePosition = btn2.Location;
                              aVariablePosition.X--;
                              btn2.Location = aVariablePosition;
                              break;
                          }
                      case Keys.D:
                          {
                              aVariablePosition = btn2.Location;
                              aVariablePosition.X++;
                              btn2.Location = aVariablePosition;
                              break;
                          }
                      case Keys.W:
                          {
                              aVariablePosition = btn2.Location;
                              aVariablePosition.Y--;
                              btn2.Location = aVariablePosition;
                              break;
                          }
                      case Keys.S:
                          {
                              aVariablePosition = btn2.Location;
                              aVariablePosition.Y++;
                              btn2.Location = aVariablePosition;
                              break;
                          }
                  }
              }


Habe einfach die Steuerung von den Richtungstasten aud A,S,D,W verlegt.

Danke! smile

edit: Bitte bessere mich aus, wenn das keine gute Variante ist...

Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

Ich habe dein Problem jetzt verstanden: Bei Pfeiltasten/Tab wird ein anderer Button aktiv, statt dass die Routine abgehandelt wird.
Das Auslagern auf WASD ist natürlich möglich, umgeht das Problem aber mehr, als es zu beheben.
Die Klasse KeyboardNavigation sollte das verhindern können, nur finde ich die bei mir nicht, System.Windows.Input enthält nur den namespace Manipulations.

Edit: gefunden, steht nur für WPF-Anwendungen zur Verfügung.
Auf diesen Beitrag antworten »
InformaTiger

Nun habe ich ein weiteres Problem...
Gibt es eine Eigenschaft die einem Panel sagt es soll keine anderen Steuerelemente annehmen?

smile

Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

Kannst du die Frage präzisieren, ich weiß nicht, worauf du hinauswillst?
Auf diesen Beitrag antworten »
InformaTiger

Ja, dass wenn wir wieder von meinem Beispiel sprechen (siehe Anhang) dann verändert sich die Location der Buttons wenn man sie in ein Panel zieht auf -1; -1 und ich möchte dass sie ihre ursprüngliche Form bspw. 415; 69 beibehalten. Damit ich die Location des Buttons mit der des Panels vergleichen kann.

smile

Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

Kannst du nicht einfach die Location des Panels draufaddieren?
Auf diesen Beitrag antworten »
InformaTiger

Ich habe leider keine Ahnung ob und wie das geht geschockt

Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

ich meinte etwa so:
int eigentlichX = button1.Location.X + panel1.Location.X;
Bei mir steht aber auch nicht -1;-1 in Button1.Location, sondern die relative Position im Panel.

Tauchen die Koordinaten -1;-1 zur Entwurfs- oder Laufzeit auf?

Verstehe ich das eigentlich richtig, dass du Buttons zum Anzeigen von Bildern zweckentfremdest? Dafür gibt es eigentlich die PictureBox.
Auf diesen Beitrag antworten »
InformaTiger

Zu den Panels: Ich verwende die Panels nur als Orientierungspunkt für den Benutzer (Weißer Hintergrund, FixedSingle Border).

Zu den Koordinaten: Die Koordinaten tauchen in der Entwurfszeit bei den Buttons auf.

Aber: Ich glaube der Code den du hier geschrieben hast macht für mich in diesem Zusammenhang wenig Sinn...

code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
if ((btn3.Location.X == pnl5.Location.X) && (btn3.Location.Y == pnl5.Location.Y) && (btn15.Location.X == pnl1.Location.X) && (btn15.Location.Y == pnl1.Location.Y) && (btn11.Location.X == pnl9.Location.X) && (btn11.Location.Y == pnl9.Location.Y) && (btn5.Location.X == pnl13.Location.X) && (btn5.Location.Y == pnl13.Location.Y) && (btn6.Location.X == pnl2.Location.X) && (btn6.Location.Y == pnl2.Location.Y) && (btn1.Location.X == pnl6.Location.X) && (btn1.Location.Y == pnl6.Location.Y) && (btn12.Location.X == pnl10.Location.X) && (btn12.Location.Y == pnl10.Location.Y) && (btn13.Location.Y == pnl10.Location.Y) && (btn9.Location.X == pnl14.Location.X) && (btn9.Location.Y == pnl14.Location.Y) && (btn14.Location.X == pnl3.Location.X) && (btn14.Location.Y == pnl3.Location.Y) && (btn2.Location.X == pnl7.Location.X) && (btn2.Location.Y == pnl7.Location.Y) && (btn10.Location.X == pnl11.Location.X) && (btn10.Location.Y == pnl11.Location.Y) && (btn16.Location.X == pnl15.Location.X) && (btn16.Location.Y == pnl15.Location.Y) && (btn8.Location.X == pnl4.Location.X) && (btn8.Location.Y == pnl4.Location.Y) && (btn13.Location.X == pnl8.Location.X) && (btn13.Location.Y == pnl8.Location.Y) && (btn4.Location.X == pnl12.Location.X) && (btn4.Location.Y == pnl12.Location.Y) && (btn7.Location.X == pnl16.Location.X) && (btn7.Location.Y == pnl16.Location.Y))
              {
                  MessageBox.Show("Super der Code zum nächsten Level ist: GjBqa8");
                  Level2 aAnzeigeObjekt = new Level2();
                  aAnzeigeObjekt.Show();
                  this.Close();
              }
              else
              {
                  MessageBox.Show("Leider, nicht richtig!");
              }


Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

Eine Zeile mit 1351 Zeichen?!
Und dann noch Indizes, die wild durcheinander gehen.
Das muss auch anders gehen, ich rate zu einem Array von Anzeigeelementen.
Dürfte jetzt ein gewisser Aufwand sein, das alles nochmal umzustellen, zahlt sich aber durch den einfacheren Umgang wieder aus.

Hast 'ne Mail.
Auf diesen Beitrag antworten »
InformaTiger

Naja, damit kenne ich mich leider gar nich aus... oder ich versteh nicht was du meinst.

Wink

Lg
InformaTiger
Auf diesen Beitrag antworten »
eulerscheZahl

Irgendwo ist noch ein kleiner Fehler drin, aber ich denke man kann erkennen, wie es im Prinzip funktioniert:
evtl. in Zeile29 den Pfad ändern.

edit: ich sollte abends nicht mehr programmieren... Läuft jetzt.
code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;

namespace Slider
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public Bitmap CutImage(Bitmap original, int startX, int startY, int breiteX, int breiteY)
        {
            return original.Clone(new Rectangle(startX, startY, breiteX, breiteY), PixelFormat.DontCare);
        }

        const int teileX = 3;
        const int teileY = 3;
        PictureBox[,] pic = new PictureBox[teileX, teileY];
        Point freiesFeld = new Point(teileX - 1, teileY - 1);
        int[,] wasIstWo = new int[teileX, teileY];

        private void Form1_Load(object sender, EventArgs e)
        {
            Random r = new Random();
            string[] auswahl = { "Chrysanthemum", "Desert", "Hydrangeas", "Jellyfish", "Koala", "Lighthouse", "Penguins", "Tulips" };
            string bildPfad = @"C:\Users\Public\Pictures\Sample Pictures\" + auswahl[r.Next(auswahl.Length)] + ".jpg";
            Bitmap bild = new Bitmap(bildPfad);
            this.KeyPreview = true;
            for (int i = 0; i < teileX; i++)
                for (int j = 0; j < teileY; j++)
                {
                    pic[i, j] = new PictureBox();
                    pic[i, j].Parent = this;
                    pic[i, j].Left = i * (0 + bild.Width / teileX);
                    pic[i, j].Top = j * (0 + bild.Height / teileY);
                    pic[i, j].Width = bild.Width / teileX;
                    pic[i, j].Height = bild.Height / teileY;
                    pic[i, j].Image = CutImage(bild, bild.Width / teileX * i, bild.Height / teileY * j, bild.Width / teileX, bild.Height / teileY);
                    wasIstWo[i, j] = i * teileY + j;
                }
            pic[teileX - 1, teileY - 1].Visible = false;
            mischen(teileX * teileX * teileY * teileY);
        }

        private void mischen(int p)
        {
            Random r = new Random();
            for (int i = 0; i < p; i++)
                bewegen(r.Next(4));
        }

        public enum richtung { LEFT, RIGHT, UP, DOWN }

        private void bewegen(int richt)
        {
            int tmp, nachbarX, nachbarY;
            int[,] offsetVector = { { 1, 0 }, { -1, 0 }, { 0, 1 }, { 0, -1 } };
            switch (richt)
            {
                case (int)richtung.LEFT:
                    if (freiesFeld.X == teileX - 1) return;
                    break;
                case (int)richtung.RIGHT:
                    if (freiesFeld.X == 0) return;
                    break;
                case (int)richtung.UP:
                    if (freiesFeld.Y == teileY - 1) return;
                    break;
                case (int)richtung.DOWN:
                    if (freiesFeld.Y == 0) return;
                    break;
            }
            nachbarX = wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]] / teileY;
            nachbarY = wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]] % teileY;
            if (richt >= 2)
            {
                tmp = pic[nachbarX, nachbarY].Top;
                pic[nachbarX, nachbarY].Top = pic[teileX - 1, teileY - 1].Top;
                pic[teileX - 1, teileY - 1].Top = tmp;
            }
            else
            {
                tmp = pic[nachbarX, nachbarY].Left;
                pic[nachbarX, nachbarY].Left = pic[teileX - 1, teileY - 1].Left;
                pic[teileX - 1, teileY - 1].Left = tmp;
            }
            tmp = wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]];
            wasIstWo[freiesFeld.X + offsetVector[richt, 0], freiesFeld.Y + offsetVector[richt, 1]] = wasIstWo[freiesFeld.X, freiesFeld.Y];
            wasIstWo[freiesFeld.X, freiesFeld.Y] = tmp;
            freiesFeld.X += offsetVector[richt, 0];
            freiesFeld.Y += offsetVector[richt, 1];

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Left: //von links in das freie Feld schieben
                    bewegen((int)richtung.LEFT);
                    break;
                case Keys.Right:
                    bewegen((int)richtung.RIGHT);
                    break;
                case Keys.Up:
                    bewegen((int)richtung.UP);
                    break;
                case Keys.Down:
                    bewegen((int)richtung.DOWN);
                    break;
            }
            pic[teileX - 1, teileY - 1].Visible = TestGeloest();
        }

        private bool TestGeloest()
        {
            for (int i = 0; i < teileX; i++)
                for (int j = 0; j < teileY; j++)
                    if (wasIstWo[i, j] != i * teileY + j) return false;
            return true;
        }
    }
}
Auf diesen Beitrag antworten »
InformaTiger

Nun das Problem daran ist mal natürlich wieder, dass ich das leider nicht verstehe... vielleicht wäre es mit Kommentare übersichtlicher. Mein Auge ist so von meinen Programmierkonventionen geprägt, dass ich andern Code leider kaum verstehen kann. unglücklich
Zumal verstehe ich nicht was enum ist. Vom Code verstanden habe ich nur eines: Dass der Code die Bilder durch Zufall auswählt, man Sie durch bewegen(); verschieben kann und sonst leider nicht viel...

PS: Wenn es möglich ist kannst du mir das gegebenenfalls über Email erklären. Nächste Woche bin ich leider voll ausgebucht -.-

Lg
InformaTiger
 
Neue Frage »
Antworten »


Verwandte Themen

Die Beliebtesten »
Die Größten »
Die Neuesten »