如何添加然后选择我刚刚添加的动态控件

时间:2014-09-07 17:11:52

标签: vb.net

我在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

我所做的所有好事的名义是什么?

2 个答案:

答案 0 :(得分: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事件。

答案 1 :(得分:0)

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

这是在Sub Main事件之前

   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

这实际上解决了添加和选择对象的问题 感谢所有的建议和帮助