2

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.

0

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

3 Comments

Casting it to the user control type (PictureLoader) only allows me to access the properties of the control itself. I need to get access to a picture box on the user control (in this scenario, the sender is the picture box within the user control).
You should be able to get to any controls on your user control via the FindControl method. You'll need to cast the returned control to a PictureBox.
The user control is created dynamically for every returned result from the query to the database, correct me if I am wrong, but do you not need to pass the controls name to the FindControl method? Every picture box within the user control would have the same name... The sender object is the picture box that has my custom properties I need to read, I just cant figure out how to access them from the event handler. There has to be a simple way that I just don't know about due to my minimal knowledge of .net.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.