C#-为什么即使设置了父项,我的动态标签也不是透明的?
我正在写一个WinForms应用程序。在这个应用程序中,我生成动态C#-为什么即使设置了父项,我的动态标签也不是透明的?,c#,.net,winforms,dynamic,controls,C#,.net,Winforms,Dynamic,Controls,我正在写一个WinForms应用程序。在这个应用程序中,我生成动态标签,图片框和文本框控件 通过将图像拖放到图片框,添加的文本框将打开。输入一些文本并按Enter键,将触发以下方法 private void tb_Tile_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Enter) { TextBox tb_Tile = sender a
标签
,图片框
和文本框
控件
通过将图像
拖放到图片框
,添加的文本框
将打开。输入一些文本并按Enter键,将触发以下方法
private void tb_Tile_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
TextBox tb_Tile = sender as TextBox;
Tile tb_Tag = tb_Tile.Tag as Tile;
//add function that overgives the given name to the matrix i.e. GiveNameToMatrix()
tb_Tile.Visible = false;
Label lbl_Tile = Controls.Find("Label" + tb_Tag.X + tb_Tag.Y, true).FirstOrDefault() as Label;
lbl_Tile.Visible = true;
//find picture box by tag or sth and then make this pB the parent
PictureBox pb_Tile = (PictureBox)gb_gridBox.Controls["Tile" + tb_Tag.X + tb_Tag.Y];
pb_Tile.BackgroundImage = pb_Tile.Image;
lbl_Tile.Parent = pb_Tile;
// pb_Tile.Visible = false;
if (pb_Tile.HasChildren)
{
lbl_Tile.Text = tb_Tile.Text; //parent has to be set to PictureBox
lbl_Tile.Visible = true;
lbl_Tile.ForeColor = Color.Black;
lbl_Tile.BackColor = Color.Transparent;
lbl_Tile.Location = pb_Tile.Location;
lbl_Tile.Refresh();
pb_Tile.Refresh();
gb_gridBox.Controls.Add(lbl_Tile);
lbl_Tile.BringToFront();
}
}
}
我希望标签.文本
显示在图片盒
上。这就是为什么我将PictureBox
设置为Label
的父项,将Label.BackColor
设置为透明。但是标签
就消失在图片盒
后面
有没有人知道如何解决这个问题,或者可以给我一个提示,告诉我在图片盒前面显示文本的另一种可能性
提前感谢。我看到的问题是:
lbl_Tile.Location = pb_Tile.Location;
关于财产的文件:
获取或设置控件左上角相对于其容器左上角的坐标
在您的情况下,pb_Tile
是lbl_Tile
的容器,因此要获得所需的位置,您应该使用
lbl_Tile.Location = new Point(0, 0);
您还应该删除这一行
gb_gridBox.Controls.Add(lbl_Tile);
因为它会更改标签的父项parent.Controls.Add(child)
和child.parent=parent
执行相同的操作。我看到的问题是:
lbl_Tile.Location = pb_Tile.Location;
关于财产的文件:
获取或设置控件左上角相对于其容器左上角的坐标
在您的情况下,pb_Tile
是lbl_Tile
的容器,因此要获得所需的位置,您应该使用
lbl_Tile.Location = new Point(0, 0);
您还应该删除这一行
gb_gridBox.Controls.Add(lbl_Tile);
因为它会更改标签的父项parent.Controls.Add(child)
和child.parent=parent
执行相同的操作。您是否尝试过调整控件?例如,lbl_Tile.BringToFront()代码>是否尝试设置gb\u gridBox.Controls.SetChildIndex(lbl\u tile,0)代码>?谢谢!是的,我使用了BringToFront(),正如您在最后一行中看到的。设置子索引会将标签带到PictureBox前面,但它不会变得透明。您是否尝试过调整控件?例如,lbl_Tile.BringToFront()代码>是否尝试设置gb\u gridBox.Controls.SetChildIndex(lbl\u tile,0)代码>?谢谢!是的,我使用了BringToFront(),正如您在最后一行中看到的。设置子索引会将标签带到PictureBox前面,但它不会变得透明
my标签正在移动到gb\u gridBox
Groupbox的0,0坐标:-/因此Groupbox似乎仍然是容器,但它不应该是容器,因为您确实是lbl\u Tile.Parent=pb\u Tile代码>。哎呀,你应该删除这一行gb\u gridBox.Controls.Add(lbl\u Tile)
因为它再次更改了父级
。好的,但是如何添加lbl\u磁贴
?请参阅更新。您不需要添加它,因为设置Parent
属性将其添加到Parent.Controls
集合中。在你的例子中,topb_Tile.Controls
。捕捉得好,我没有注意到它与新点(0,0)
my标签正在移动到gb\u gridBox
Groupbox的0,0坐标:-/因此Groupbox似乎仍然是容器,但它不应该是容器,因为您确实是lbl\u Tile.Parent=pb\u Tile代码>。哎呀,你应该删除这一行gb\u gridBox.Controls.Add(lbl\u Tile)
因为它再次更改了父级
。好的,但是如何添加lbl\u磁贴
?请参阅更新。您不需要添加它,因为设置Parent
属性将其添加到Parent.Controls
集合中。在您的情况下,请转到pb\u Tile.Controls
。