【问题标题】:How do I add then select the dynamic control I just added如何添加然后选择我刚刚添加的动态控件
【发布时间】:2014-09-14 21:38:30
【问题描述】:

我在 VB.net 中有一个应用程序,它将一个图片框添加到另一个图片框控件中的选定鼠标位置。我需要创建一个点击事件来选择那个新的图片框,这样我就可以将它拖放到一个新位置,以防第一个错误或使用按键事件,这些事件我稍后会编写代码,但我不能弄清楚如何选择任何动态控件。

在vb6中有一种方法可以选择控件的索引,但是在VB.net中没有这种动物。

我尝试了对照组,但由于某种原因我没有从他们那里得到结果。

这是我目前的代码

Private Sub PictureBox1_Click(sender As System.Object, 
          e As System.EventArgs) Handles PictureBox1.Click

    Dim pb As New PictureBox
    pb.BackColor = Color.Blue
    Me.PictureBox1.Controls.Add(pb)
    pb.Size = New Size(64, 110)
    pb.Location = New Point(Cursor.Position.X - 64, Cursor.Position.Y - 110)
    pb.Visible = True

End Sub

以所有好事的名义,我在这里做错了什么?

【问题讨论】:

  • 听起来您指的是 VB6 控件数组。如果你给你的新图片框起一个名字,你可以通过Controls(picBoxName)引用它;如果要创建多个,请使用 List 来存储名称 (List(Of String)) 或控件引用 (List(of PictureBox))。如果您希望新控件响应事件,您还需要查找 AddHandler
  • .net 中的某些控件旨在托管子控件。这包括 PictureBoxTextBoxButton 等控件。改为将新 PB 添加到表单控件集合中。

标签: vb.net


【解决方案1】:

您需要提前编写一个通用事件处理程序,使用sender 参数来引用引发事件的对象。

Private Sub PictureBoxes_Click(sender As Object, e As EventArgs)
    Dim pb = DirectCast(sender, PictureBox)

    'Use pb here.
End Sub

在运行时创建控件时,使用AddHandler 语句将方法附加到事件。

Dim pb As New PictureBox

AddHandler pb.Click, AddressOf PictureBoxes_Click

也就是说,如果您想实现拖放操作,那么您应该处理的不是 Click 事件。

【讨论】:

  • 感谢各位大佬的指点,拖拽功能在后面,现在我只是在尝试解决刚刚创建的动态控件,这一切都是一个好的开始。
  • 顺便说一句,每当你使用AddHandler时,你应该写一个对应的RemoveHandler语句,以便在你处理完对象后将方法与事件分离。
  • 你看,这部分是问题的一部分。我尝试了上面发布的代码,它确实选择了我想要选择的对象,但随后它阻止了添加新对象的进一步尝试。有一些方法我设法从您的代码中获得了部分效果,但我可以说在这个主题上还没有解决。
  • 您能否更具体地了解实际发生的情况? “它阻止了添加新对象的进一步尝试”并没有太多意义。
  • Plutonix,我将如何在事件中使用动态名称以及我将使用哪个事件来触发它进行选择?
【解决方案2】:

这一点代码花了一些时间,但我能够做到我目前为止的目标......

这是在副主赛事之前

   Public Class dynamicPB                                     'create a picturebox element which 
                                                              'can be called anytime
        Inherits PictureBox                                   'sets the type of control to a 
                                                              'picturebox

    Public Sub New()                                       'sets the function of the new box to 
                                                           'default values
        MyBase.BackColor = Color.AliceBlue
        MyBase.BorderStyle = Windows.Forms.BorderStyle.Fixed3D
        MyBase.Height = 50
        MyBase.Width = 26
    End Sub
End Class

在实际主类中

Private Sub <control_event> (blah...) Blah...
    Dim intPosAdj_X As Integer = 13                         'get an offset for the cursor 
    Dim intPosAdj_Y As Integer = 25

        Dim newPictureBox As New dynamicPB                  'sets the click of the mouse into a           
                                                            'mode of drawing a new PB
        With newPictureBox                                      'clean use of the code
        AddHandler newPictureBox.Click, _
            AddressOf PictureBox_Click                      'establishes events for the mouse 
                                                            'activity on the objects
        AddHandler newPictureBox.MouseEnter, _
            AddressOf PictureBox_MouseEnter
        AddHandler newPictureBox.MouseLeave, _
            AddressOf PictureBox_MouseLeave
        pbName += 1                                         'gives a unique name to the 
                                                            'picturebox in an "array" style
            .Location = New System.Drawing.Point _
                (xPos - intPosAdj_X, yPos - intPosAdj_Y)        'establish where the box goes 
                                                                'and center the object on the 
                                                                'mouse pointer

            .Visible = True                                     'ensures that the box is visible
            .Name = pbName                                      'names the new control
        End With
        Controls.Add(newPictureBox)                             'add control to form

    End Sub
Private Sub PictureBox_Click(sender As System.Object, e As System.EventArgs)
     Dim dblMouseClick As Double = CType(DirectCast _
         (e, System.Windows.Forms.MouseEventArgs).Button, MouseButtons) _
                                                                'make it simple to manipulate        
                                                                'the event by putting the long 
                                                                'code into a variable
        If dblMouseClick = MouseButtons.Left Then
            MsgBox("Left CLick")
        ElseIf dblMouseClick = MouseButtons.Right Then
            MsgBox("right click")
        Else
            MsgBox("Center")
        End If

这实际上解决了添加和能够选择对象的问题 谢谢大家的建议和帮助

【讨论】:

    猜你喜欢
    • 2014-10-31
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2011-01-27
    • 2012-05-30
    • 1970-01-01
    • 1970-01-01
    • 2011-01-28
    相关资源
    最近更新 更多