从 Panel 访问 Label 对象,全部在 ToolStripMenuItem 中
Access a Label object from a Panel, all within a ToolStripMenuItem
我正在尝试从 Panel 对象移动 Label 对象,在这种情况下,我有一个父面板,其中有 Panel 和 Label 对象作为子对象,它们是动态创建的。 objective是从ToolStripMenuItem执行时,Panel对象移动时,Label对象也移动
我编写了以下代码,但我想我无法移动 Label 对象。据我了解,我正在做的是生成与我需要的标签名称相同的变量,但我需要的是引用现有对象,而不是新对象。 (这是正确的吗?)
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
Dim posX = clickedPanel.Location.X + 120
Dim posY = clickedPanel.Location.Y
Dim namelabel As New Label With {
.Name = "Label" & clickedPanel.Name.Last
}
namelabel.Location = New Point((posX), posY)
End Sub
你能指导我吗?
注意:我忘记了什么,在这种情况下,如果我移动Panel1,Label1也会移动,如果我移动Panel2,Label2也会移动,等等
这是代码,在面板内动态创建标签,并一起移动。
Private Sub NavButton15_ElementClick(sender As Object, e As NavElementEventArgs) Handles NavButton15.ElementClick
Dim pos As Int32 = 50
Dim poslabel As Int16 = 26
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Dim counter as Int16 = 1
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\Example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & counter
}
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Panel " & counter,
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & counter
}
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
End Sub
这是 ToolStripMenuItem,在其中移动带有标签的面板。
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub
我正在尝试从 Panel 对象移动 Label 对象,在这种情况下,我有一个父面板,其中有 Panel 和 Label 对象作为子对象,它们是动态创建的。 objective是从ToolStripMenuItem执行时,Panel对象移动时,Label对象也移动
我编写了以下代码,但我想我无法移动 Label 对象。据我了解,我正在做的是生成与我需要的标签名称相同的变量,但我需要的是引用现有对象,而不是新对象。 (这是正确的吗?)
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
Dim posX = clickedPanel.Location.X + 120
Dim posY = clickedPanel.Location.Y
Dim namelabel As New Label With {
.Name = "Label" & clickedPanel.Name.Last
}
namelabel.Location = New Point((posX), posY)
End Sub
你能指导我吗?
注意:我忘记了什么,在这种情况下,如果我移动Panel1,Label1也会移动,如果我移动Panel2,Label2也会移动,等等
这是代码,在面板内动态创建标签,并一起移动。
Private Sub NavButton15_ElementClick(sender As Object, e As NavElementEventArgs) Handles NavButton15.ElementClick
Dim pos As Int32 = 50
Dim poslabel As Int16 = 26
Dim posY As Int16 = 330
Dim posX As Int16 = 3
Dim counter as Int16 = 1
Panel1.AutoScrollPosition = New Point(0, 0)
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.BorderStyle = BorderStyle.FixedSingle,
.BackgroundImage = Image.FromFile("C:\Example.bmp"),
.BackgroundImageLayout = ImageLayout.Stretch,
.ContextMenuStrip = CntxtMnuStrpSection,
.Name = "Panel" & counter
}
Dim labela As New Label With {
.AutoSize = True,
.Location = New Point((poslabel), 12),
.Text = "Panel " & counter,
.ForeColor = Color.White,
.BackColor = Color.Transparent,
.Font = New Font(Me.Font, FontStyle.Bold),
.Name = "Label" & counter
}
pb.Location = New Point(pos, 20)
Panel1.Controls.Add(pb)
pb.Controls.Add(labela)
End Sub
这是 ToolStripMenuItem,在其中移动带有标签的面板。
Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem.Click
Dim clickedPanel = DirectCast(DirectCast(DirectCast(sender, ToolStripMenuItem).Owner, ContextMenuStrip).SourceControl, Panel)
clickedPanel.Location = New Point((clickedPanel.Location.X + 120), clickedPanel.Location.Y)
End Sub