0

我只是想在我的表单中添加三个控件。前两个出现,但第三个没有。我不明白为什么会发生这种行为。我曾尝试使用 .bringToFront(),但最终得到了相同的结果。我也尝试使用 Me.controls.setChildIndex() 但这也无济于事。

我很茫然。我一定做错了什么。请帮忙。

谢谢。

这是我的代码:

' Add the label
' ------------------
  Dim menu_label As New Label
  menu_label.Text = "Menu"
  menu_label.Location = New Point(50, 20)
  menu_label.Width = 50
  menu_label.Font = New Font(main_font, main_font_size, FontStyle.Regular)
  menu_label.ForeColor = Color.White
  Me.Controls.Add(menu_label)

' create the image
' ---------------
  Dim logo As New PictureBox
  Dim logo_image As Image
  logo_image = My.Resources.logo
  logo.Image = logo_image
  logo.Width = 30
  logo.Height = 30
  logo.Left = 5
  logo.Top = 0
  Me.Controls.Add(logo)

' add a line
' ----------
  Dim line As New Panel
  line.Height = 1
  line.Width = Me.Width
  line.BackColor = Color.Red
  line.Location = New Point(0, 32)
  Me.Controls.Add(line)


' end code
' ---------------------------------

无论我将它们放在哪个顺序中,都只会显示前两项。所以我要么只得到标志和菜单标签,要么只得到标志和线条,或者只得到线条和菜单标签。真是疯了!

所以我尝试了:

Me.Controls.SetChildIndex(logo, 0)
Me.Controls.SetChildIndex(menu_label, 1)
Me.Controls.SetChildIndex(line, 2)

无论哪个项目被分配到第 2 级,都不会出现。

4

1 回答 1

0

我也进行了测试,对我来说效果很好。将表单背景颜色更改为黑色,以便您可以更好地查看结果:

布局故障排除测试

Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    ' Add the label
    ' ------------------
    Dim menu_label As New Label
    menu_label.Text = "Menu"
    menu_label.Location = New Point(50, 20)
    menu_label.Width = 50
    'menu_label.Font = New Font(main_font, main_font_size, FontStyle.Regular)
    menu_label.ForeColor = Color.White
    Me.Controls.Add(menu_label)

    ' create the image
    ' ---------------
    Dim logo As New PictureBox
    Dim logo_image As Image
    'logo_image = My.Resources.logo
    'logo.Image = logo_image
    logo.BackColor = Color.Yellow
    logo.Width = 30
    logo.Height = 30
    logo.Left = 5
    logo.Top = 0
    Me.Controls.Add(logo)

    ' add a line
    ' ----------
    Dim line As New Panel
    line.Height = 1
    line.Width = Me.Width
    line.BackColor = Color.Red
    line.Location = New Point(0, 32)
    Me.Controls.Add(line)

    Me.BackColor = Color.Black
End Sub

你的表格上还有什么?也许你可以张贴截图?

于 2013-06-21T15:20:19.603 回答