Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Informatik in der Schule (http://www.informatikerboard.de/board/board.php?boardid=21)
--- C# Geeignetes Key - Ereignis (http://www.informatikerboard.de/board/thread.php?threadid=1437)


Geschrieben von InformaTiger am 02.04.2013 um 12:18:

  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



Geschrieben von eulerscheZahl am 02.04.2013 um 13:09:

 

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).



Geschrieben von InformaTiger am 02.04.2013 um 13:18:

 

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



Geschrieben von eulerscheZahl am 02.04.2013 um 13:20:

 

Landet das Programm im switch-Block?



Geschrieben von InformaTiger am 02.04.2013 um 13:26:

 

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



Geschrieben von eulerscheZahl am 02.04.2013 um 14:47:

 

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.



Geschrieben von InformaTiger am 02.04.2013 um 15:10:

 

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

smile

Lg
InformaTiger



Geschrieben von eulerscheZahl am 02.04.2013 um 15:33:

 

Kannst du die Frage präzisieren, ich weiß nicht, worauf du hinauswillst?



Geschrieben von InformaTiger am 02.04.2013 um 15:57:

 

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



Geschrieben von eulerscheZahl am 02.04.2013 um 16:09:

 

Kannst du nicht einfach die Location des Panels draufaddieren?



Geschrieben von InformaTiger am 02.04.2013 um 16:10:

 

Ich habe leider keine Ahnung ob und wie das geht geschockt

Lg
InformaTiger



Geschrieben von eulerscheZahl am 02.04.2013 um 16:23:

 

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.



Geschrieben von InformaTiger am 02.04.2013 um 16:38:

 

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



Geschrieben von eulerscheZahl am 02.04.2013 um 16:50:

 

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.



Geschrieben von InformaTiger am 02.04.2013 um 17:01:

 

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

Wink

Lg
InformaTiger


Forensoftware: Burning Board, entwickelt von WoltLab GmbH