Jay Online
Jay Online

Reputation: 23

Access properties of controls on a programatically created user control VB .NET

After taking a few years off from programming, I decided to start learning vb.net. I have created a user control that contains a picture box. I added some custom properties to the picture box that will hold general string data from a database.

My problem is that the user control is programatically created during run time, and during this time a DoubleClick event handler is added for the picture box that is within the user control.

I need to be able to set the custom properties for the picture box during the creation of the user control, so that when the control (picture box) is double clicked I can read these values but am unsure on how to access them.

The picture box is the entire size of the user control, or I would just add the custom properties right to the user control and add the DoubleClick event handler to that. However, double clicking needs to be done on the picture box since it takes up the entire user control, unless anyone has an idea to trigger the DoubleClick event of the user control when the picture box is double clicked.

Here is a bit of code I am using to add the user control to the form programatically -

hb_item = New PictureLoader

With hb_item
    .Name = "item_" & i
    .Left = itemLeft
    .Top = itemTop
    .SetImageSizeMode = ImageLayout.Stretch
    .SetLoadingImageSizeMode = ImageLayout.Stretch
    .Size = New Size(100, 126)
    .SetImage = BlobToImage(sql_reader("ThumbImage"))
    .Visible = True
    .SetHighlight(True)
    .SetHighlightColor = Color.GreenYellow
    .TextColor = Color.White
    .CircleColor = Color.GreenYellow

    '--- THIS UPDATES ONE OF THE CUSTOM PROPERTIES FOR THE PICTURE BOX
    '--- CONTAINED WITHIN THE USER CONTROL
    .SetID = "test"

    AddHandler .picMainClick, AddressOf frmHome.HBItem_Click
    AddHandler .picMainDoubleClick, AddressOf frmHome.HBItem_DoubleClick
End With

Here is the event handler code I am trying to access the picture box's custom properties from

Public Sub HBItem_DoubleClick(ByVal sender As System.Object, ByVal e As EventArgs) Handles MyBase.DoubleClick

    With sender
        '--- THIS IS WHERE I WANT TO READ THE DATA IN THE CUSTOM PROPERTIES 
        '--- OF THE PICTURE BOX... SOMETHING SIMILAR TO THE FOLLOWING -

        ' Database_ID is one of the custom properties of the sender (picMain 
        ' control on the user control)
        MessageBox.Show(.Database_ID) 
    End With
End Sub

EDIT: Got it all worked out, thanks for everything. All that was needed was casting the sender to the actual picture box like stated, I was just looking way to deeply into things. A simple one line of code is all that was needed in the event handler -

Dim pb As xPictureBox = CType(sender, xPictureBox)

Then all the custom properties could be accessed using pb.property_here.

Upvotes: 2

Views: 2289

Answers (1)

Tim
Tim

Reputation: 28530

sender is of type System.Object - you need to cast (convert) sender to the type it actually is (in your case, your custom user control), i.e.:

Dim myControl As MyCustomControl = CType(sender, MyCustomControl)

With myControl
    MessageBox.Show(.Database_ID)
End With

Upvotes: 1

Related Questions