0
我有國際象棋桌和我的元素現在正按照規則移動。但是當我拖出規則我的按鈕正在消失......我該如何解決它? (紅色按鈕顯示哪裏可以去我的元素) 例如,騎士現在按規則移動(如果我不通過紅色按鈕沒有問題),但是當我通過紅色的地方,如果我放棄在那裏騎士消失,紅色的地方變回原來的顏色(沒有更多的紅色地方,這表明我的騎士可以去哪裏)。我試圖進行調試,但因爲我是新的C#和調試我還沒有解決問題。如果你開導我的道路,我會很高興。我該如何解決它?謝謝在國際象棋桌上拖動問題
void btn_DragEnter(object sender, DragEventArgs e)
{
Button button = (Button)sender;
e.Effect = DragDropEffects.Move;
for (int x = 0; x <= 7; x++)
{
for (int y = 0; y <= 7; y++)
{
btn[x, y].Image = null;
if ((x + y) % 2 == 0)
btn[x, y].BackColor = Color.Black;
else
btn[x, y].BackColor = Color.White;
}
}
}
void btn_DragDrop(object sender, DragEventArgs e)
{
Button button = (Button)sender;
button.Image = (Bitmap)e.Data.GetData(DataFormats.Bitmap);
int[] dizi = (int[])button.Tag;
int x = dizi[0];
int y = dizi[1];
for (int a = 0; a <= 7; a++)
{
for (int b = 0; b <= 7; b++)
{
btn[a, b].AllowDrop = false;
}
}
if ((x + 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x + 1 <= 7))
{
btn[x + 1, y + 2].BackColor = Color.Red;
btn[x + 1, y + 2].AllowDrop = true;
}
if ((x + 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x + 1 <= 7))
{
btn[x + 1, y - 2].BackColor = Color.Red;
btn[x + 1, y - 2].AllowDrop = true;
}
if ((x - 1 >= 0 && y + 2 <= 7) && (y + 2 >= 0 && x - 1 <= 7))
{
btn[x - 1, y + 2].BackColor = Color.Red;
btn[x - 1, y + 2].AllowDrop = true;
}
if ((x - 1 >= 0 && y - 2 <= 7) && (y - 2 >= 0 && x - 1 <= 7))
{
btn[x - 1, y - 2].BackColor = Color.Red;
btn[x - 1, y - 2].AllowDrop = true;
}
if ((x + 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x + 2 <= 7))
{
btn[x + 2, y + 1].BackColor = Color.Red;
btn[x + 2, y + 1].AllowDrop = true;
}
if ((x + 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x + 2 <= 7))
{
btn[x + 2, y - 1].BackColor = Color.Red;
btn[x + 2, y - 1].AllowDrop = true;
}
if ((x - 2 >= 0 && y + 1 <= 7) && (y + 1 >= 0 && x - 2 <= 7))
{
btn[x - 2, y + 1].BackColor = Color.Red;
btn[x - 2, y + 1].AllowDrop = true;
}
if ((x - 2 >= 0 && y - 1 <= 7) && (y - 1 >= 0 && x - 2 <= 7))
{
btn[x - 2, y - 1].BackColor = Color.Red;
btn[x - 2, y - 1].AllowDrop = true;
}
}
好的謝謝亞歷克斯它幫助了一些。你是正確的DragEnter。所以我清除了DragEnter中的for循環。並且工作得很好,但是這一次當我移動騎士的時候,騎士的形象就停留在前一個位置。 (意味着每當我移動騎士先前的騎士圖像時,都會停留。) –
除了在DragEnter中清除for循環,請嘗試將其移動到DragDrop的末尾,或者將其放在DoDragDrop的調用之後。 – AlexDev
謝謝亞歷克斯,我非常感謝你。它解決了這個問題。在按鈕之後的mousedown事件.DoDragDrop ....語句我檢查按鈕backcolar是否是紅色,然後我將按鈕圖像分配給null否則我將騎士圖像分配給按鈕圖像。 –