2016-01-04 20 views
5

ich WinForms bin mit. In meinem Formular habe ich ein Panel mit Schaltflächen, die das Panel bewegen. Mit den Tasten Auf und Ab bewegen Sie das Panel beispielsweise nach oben oder unten. Ich habe Schwierigkeiten, das Panel mit den entsprechenden Tasten nach links und rechts zu bewegen. Was mache ich falsch?mit Knopf klicken, C#

private void Up_btn_Click(object sender, EventArgs e) 
    { 
     if (panel1.Location.Y > -2000) 
     { 
      panel1.Location = new Point(panel1.Location.X, panel1.Location.Y - 80);   
     } 
    } 

    private void Down_btn_Click(object sender, EventArgs e) 
    { 
     if (panel1.Location.Y < 720) 
     { 
      panel1.Location = new Point(panel1.Location.X, panel1.Location.Y + 80); 
     } 
    } 

    private void Left_btn_Click(object sender, EventArgs e) 
    { 
     if (panel1.Location.X < 720) 
     { 
      panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + +55);    
     } 
    } 

    private void Right_btn_Click(object sender, EventArgs e) 
    { 
     if (panel1.Location.X < 720) 
     { 
      panel1.Location = new Point(panel1.Location.Y, panel1.Location.X -55); 
     } 
    } 

enter image description here

+2

Und was ist das Problem? –

+1

In Ihren letzten 2 Standorten ist die Reihenfolge von x und y falsch. –

+1

diese Zeile panel1.Location = neuer Punkt (panel1.Location.Y, panel1.Location.X + +55); dort ist doppeltes '+' Zeichen. – Ian

Antwort

5

(Ja, ich weiß, dass wir haben an einem Punkt unsere Mathe-Tests verderben oder einem anderen Grund Problem zu koordinieren!)

Problem

Point() ist immer (x, y) koordinieren. In Ihrem Code:

private void Left_btn_Click(object sender, EventArgs e) 
{ 
    if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + +55);    
    } 
} 

private void Right_btn_Click(object sender, EventArgs e) 
{ 
    if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.Y, panel1.Location.X -55); 
    } 
} 

Sie setzen X mit Y-Wert und umgekehrt koordinieren.

Randbemerkung: Es gibt eine Doppel + in der linken Schaltfläche klicken Ereignis auch ..

Schritt 1

Zuerst tut das Gegenteil:

private void Left_btn_Click(object sender, EventArgs e) 
{ 
    if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.X + 55 , panel1.Location.Y);    
    } 
} 

private void Right_btn_Click(object sender, EventArgs e) 
{ 
    if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.X - 55, panel1.Location.Y); 
    } 
} 

Schritt 2

Zweitens sehen links und rechts, wenn das, was Sie i beabsichtigt. Beachten Sie, dass bewegen links bedeutet, dass wir unsere X verringern und bewegend rechts wir unsere X.

Sollte es erhöhen nicht auf diese Weise getan werden?

private void Left_btn_Click(object sender, EventArgs e) //The name is Left 
{ 
    if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.X - 55 , panel1.Location.Y);    
    } 
} 

private void Right_btn_Click(object sender, EventArgs e) //The name is Right 
{ 
    if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.X + 55, panel1.Location.Y); 
    } 
} 
+0

Das Doppel + ist kein Problem, da es bedeutet, dass die Zahl positiv ist. – Phiter

+0

@PhiterFernandes Ja, es ist nur eine Randnotiz. – Ian

+0

Verwenden von 'panel1.Location.X + 55' Ihre linke Taste bewegt das Panel nach rechts! –

4

Sie vermischt die Koordinaten:

if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.Y , panel1.Location.X + 55);    
    } 

if (panel1.Location.X < 720) 
    { 
     panel1.Location = new Point(panel1.Location.X + 55, panel.Location.Y);    
    } 

Und das gleiche gilt für die linke Taste sein sollte.

6

In Ihren letzten zwei Methoden ist die Reihenfolge von x und y falsch.

Um links zu bewegen, Sie X abnehmen sollte:

panel1.Location = new Point(panel1.Location.X - 55, panel1.Location.Y); 

Um nach rechts bewegen, sollten Sie erhöhen X:

panel1.Location = new Point(panel1.Location.X + 55, panel1.Location.Y ,); 

Ich denke, auch wenn Sie Kriterien mit >-y mit bis und unten mit <y, wahrscheinlich müssen Sie eine solche Logik für links und rechts >-x und <x.