How to add a control like picturebox programatically and Allow user to move it.

Askaps

New member
Joined
Jun 5, 2012
Messages
3
Programming Experience
Beginner
Hello everyone,
This is my first post at this forum. I am searching my question that How to add a control like picturebox programatically and Allow user to move it with mouse? on google but cant find anything. Thats why I am here.
In my application user creates a picturebox and a label at runtime by selecting a file from drag drop. User can create as many files he wants. But the problem is that I dont know that how to move both picturebox and label to change their location in a flowlayoutpanel which contains these controls with mouse.
I know that how to add a control like picturebox programatically but dont know how to move it.
I add picturebox by:

dim picturebox as new picturebox
flowlayoutpanel1.controls.add(picturebox)

Please Reply Me As Soon As Possible.

Abhishek Pratap Singh
 
If you have pairs of Labels and PictureBoxes then, first things first, I'd suggest that you create a UserControl. You can add both controls to the UC and then you can use the UC as a single unit.

As for the question, you would need to use the AddHandler statement to handle events. Let's say that you add a Button to your form in the designer and, when the user clicks the Button, you want to display a message containing the Text of the Button. To do so, you would use a Click event handler like this:
VB.NET:
Private Sub Button1_Click(sender As Object, e As EventArgs) [B][U][COLOR="#FF0000"]Handles Button1.Click[/COLOR][/U][/B]
    MessageBox.Show(Button1.Text)
End Sub
Note the Handles clause, which indicates that this method handles the specified event of the object assigned to the specified field.

With objects created at run time, you generally can't do that. You still have to write the event handler at design time but you don;t attach it to the event until run time. As such, the equivalent event handler would look like this:
VB.NET:
Private Sub Button_Click(sender As Object, e As EventArgs)
    Dim btn = DirectCast(sender, Button)

    MessageBox.Show(btn.Text)
End Sub
Notice that I have changed the name to something more generic, given that it doesn't apply to a specific field now. You can use a different name if it's more appropriate, obviously. Notice also the lack of a Handles clause. Finally, notice the use of the 'sender' parameter to get access to the object that raised the event. That way, you can use this same method to handle the event for multiple objects and you will still know which one raised the event at run time.

To actually wire up the method to the event, you use AddHandler:
VB.NET:
Dim btn As New Button

btn.Text = GetButtonText()
AddHandler btn.Click, AddressOf Button_Click

Controls.Add(btn)
Just note that, any time you use AddHandler, you should have a corresponding RemoveHandler to detach the event handler to facilitate releasing resources. You would do that when you destroy the control. If the control isn't destroyed until the form is closed then you should handle the FormClosed event and do it there. That said, if it's the main form then all process resources are released when it's closed anyway so you can get away without it there.
 
First of all thanks for your reply.
But my problem is still not solved.
I have used :
dim p as new picturebox
addhandler p.click, addressof pictureboxclick
flowlayoutpanel1.controls.add(p)
for user to click on the picturebox.

bu what i am asking that what to insert in picturebox_mouseup, picturebox_mousedown and picturebox_mousemove events. So that they move in flowlayoutpanel with mouse and auto arrange by property of flowlayoutpanel.

Please Reply Me ASAP.
Thank You.
 
It's time for you to learn about Drag & Drop. Here's a primer I wrote:

Drag & Drop in Windows Forms

It's best to learn the principles with simple cases first and then apply them to more complex cases. Your case isn't that complex though. You would be dropping on PictureBox on another. You would then call Controls.GetChildIndex on the FlowLayoutPanel to get the index of the control you dropped on and then call Controls.SetChildIndex to move the control that you dragged.
 
Definitely useful information there. Keep in mind that the user is likely to be able to drop on another PictureBox and also empty space in the FlowLayoutPanel. You'll either have to allow for both or else code to disallow one or the other, depending on the behaviour you want.
The code 'targets' another contained control (GetChildAtPoint method), the dropped control takes that place.

To target a blank area in FLP one could perhaps write a GetChildNearPoint method. Another approach could be to use the FLP.DragOver event to capture last control 'touched' by GetChildAtPoint, and drop at that ones index.
 
The code 'targets' another contained control (GetChildAtPoint method), the dropped control takes that place.

To target a blank area in FLP one could perhaps write a GetChildNearPoint method. Another approach could be to use the FLP.DragOver event to capture last control 'touched' by GetChildAtPoint, and drop at that ones index.

I was more getting at that you'd have to handle the DragDrop, etc, events of both the container and the contained controls. If I'm not mistaken, dropping on a PictureBox in a FLP would not raise the FLP's DragDrop event and your code only handles the event of the container. Maybe that's not the case and I just missed it because I did look fairly quickly. Not criticising, just commenting. :)
 
jmcilhinney said:
Maybe that's not the case
No, it is not. It works automagically :)
 
Back
Top