private void PBoxJigsaw1_MouseUp(object sender, MouseEventArgs e)
{
if (sender != null && sender.GetType() == typeof(PictureBox))
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
PictureBox answer = (PictureBox)sender;
if (answer.Location.X < pnlJigsaw5.Location.X && answer.Location.Y > pnlJigsaw5.Location.Y)
{
if (answer.Location.X + answer.Width > pnlJigsaw5.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw5.Location.X + pnlJigsaw5.Width)
{
answer.Location = pnlJigsaw5.Location;
}
}
}
else if (answer.Location.X < pnlJigsaw1.Location.X && answer.Location.Y > pnlJigsaw1.Location.Y)
{
if (answer.Location.X + answer.Width > pnlJigsaw1.Location.X)
{
if ((answer.Location.X + answer.Width) < pnlJigsaw1.Location.X + pnlJigsaw1.Width)
{
answer.Location = pnlJigsaw1.Location;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我在 c# 上创建了一个拼图,用户将图片框拖动到面板中以创建图像。我想知道是否有可能检查一个面板是否包含某些东西,因为在许多图片框可以进入一个面板的那一刻,它们看起来好像消失了,尽管它们只是彼此落后。我只是想让一次面板中只能有一个图片框。谢谢
您可以检查panel您要删除的pictureBox内容是否包含任何内容controls
if(panel.Controls.Count > 0)
{
// Panel contains items inside
// Ignore Panel
}
Run Code Online (Sandbox Code Playgroud)
EDIT1:您是否尝试将此代码放在事件的顶部?像这样
private void PBoxJigsaw1_MouseUp(object sender, MouseEventArgs e)
{
if(panel.Controls.Count > 0)
{
return; // Panel already contains a control, stop executing the code
}
if (sender != null && sender.GetType() == typeof(PictureBox))
{
....
Run Code Online (Sandbox Code Playgroud)
这会使图片框消失吗?