添加新控件时,是否可以将面板上的控件的Z顺序设置为始终位于顶部?我正在创建一个纸牌游戏,并且有一个面板,上面有卡的图片(每张卡都有一个新的图片框)。添加新图片后,它会自动转到面板的背面。有什么办法可以使它始终排在最前面吗?我已经尝试过“ SetChildIndex”,但是每个图片框的名称都与我正在重复使用的代码相同。任何帮助表示赞赏。谢谢
private void DealPlayerCard(int cardNum)
{
string Card = Classes.Deck.GetCurrentCard();
PictureBox pb = new PictureBox() { Width = 172, Height = 240, SizeMode = PictureBoxSizeMode.StretchImage };
pb.ImageLocation = @"Cards\" + Card + ".png";
int order = 0;
switch (cardNum)
{
case 1:
pb.Location = new Point(0, 0);
order = 5;
break;
case 2:
pb.Location = new Point(60, 0);
order = 4;
break;
case 3:
pb.Location = new Point(120, 0);
order = 3;
break;
case 4:
pb.Location = new Point(180, 0);
order = 2;
break;
case 5:
pb.Location = new Point(240, 0);
order = 1;
break;
}
AddPlayerCard(pb, order);
}
public void AddPlayerCard(PictureBox pb, int order)
{
pnlPlayer.Controls.Add(pb);
pnlPlayer.Controls.SetChildIndex(pb, order);
}
编辑
使用Sinatr的想法,我现在使用.Tag控件属性为每个控件提供z序号,然后每当我添加控件时-只需遍历每个控件并设置z-order = .Tag属性
private void DealPlayerCard(int cardNum)
{
string Card = Classes.Deck.GetCurrentCardPicture();
PictureBox pb = new PictureBox() { Width = 172, Height = 240, SizeMode = PictureBoxSizeMode.StretchImage };
pb.ImageLocation = @"Cards\" + Card + ".png";
switch (cardNum)
{
case 1:
pb.Location = new Point(0, 0);
pb.Tag = 5;
break;
case 2:
pb.Location = new Point(60, 0);
pb.Tag = 4;
break;
case 3:
pb.Location = new Point(120, 0);
pb.Tag = 3;
break;
case 4:
pb.Location = new Point(180, 0);
pb.Tag = 2;
break;
case 5:
pb.Location = new Point(240, 0);
pb.Tag = 1;
break;
}
AddPlayerCard(pb);
AddToHand("Player");
}
public void AddPlayerCard(PictureBox pb)
{
pnlPlayer.Controls.Add(pb);
foreach (Control Control in pnlPlayer.Controls)
{
pnlPlayer.Controls.SetChildIndex(Control, Int32.Parse(Control.Tag.ToString()));
}
}
您可以使用BringToFront()
每个Winform控件都可用的方法:
controlName.BringToFront();
这会将控件controlName
置于z顺序的顶部,强制其z顺序为0。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句