Add control to top of flowlayoutpanel vb.net

I've done some reading up on trying to add a handler with button click in flowlayoutpanel in general but can't seem to get it. I searched the forums and can't find anything, if this problem has been posted before, Im sry in advance

Im trying to add a handler with button click in flowlayoutpanel

the handler is being added to a picture boxes that are being created dynamically as the user download's images.

I just want to add a process.start["IMAGE_PATH"]

to the click event. How would i do this?

thank you any advice is appreciated

Last edited by ahostbr; Sep 26th, 2008 at 12:29 AM.
  • Sep 25th, 2008, 10:30 PM

    Re: add handler with button click in flowlayoutpanel
    First you have to have written the event handler. The easiest way to create an event handler is to let the IDE do it for you. Add a PictureBox to your form and use the drop-down lists at the top of the code window or the Properties window to add a handler for the desired event. Now delete the control and the method gets left behind, minus the Handles clause. You can now change the name of the method if you like and it's available to handle any event that has the appropriate signature.

The next thing to realise is that the 'sender' parameter is always a reference to the object that raised the event. That means that, if you're using this method to handle the same event for multiple PictureBoxes, you can always get a reference to the one that raised the event from the sender. So, in your event handler, you'll need to get the sender, cast it as type PictureBox and then get the ImageLocation property, which you can then pass to Process.Start. Note that this assumes that you have loaded the Image in the first place by calling the PictureBox's Load method.

Finally, you'll need to attach the method to the appropriate event when you create the PictureBox, which you'd do like so:

vb.net Code:

  1. Dim pb As New PictureBox
  2. AddHandler pb.Click, AddressOf YourEventHandler

where YourEventHandler is the method you created earlier.

-
  • Sep 25th, 2008, 10:37 PM

    Thread Starter Lively Member

    -

    Re: add handler with button click in flowlayoutpanel

    thank you for such a quick reply. Could i give each picture box a name and call them this way?

any sample code you can provide for getting the sender & image location, if not?

thanks for your help already, i have read all of your replies about this subject that i can find.

-
  • Sep 25th, 2008, 10:43 PM

    Re: add handler with button click in flowlayoutpanel

    Originally Posted by ahostbr

Could i give each picture box a name and call them this way?

Why would you need to? As I have already said, the sender IS the PictureBox that raised the event, so if you've already got the PictureBox why would you need another way to get it? Anyway, how would you know what name to use unless you got it from the sender?

Originally Posted by ahostbr

any sample code you can provide for getting the sender & image location, if not?

You don't need to "get" it. You've already got it. It's a parameter of the method that handles the event. Just go ahead and use it. As I have already said, you use its ImageLocation property and pass that to Process.Start.

- Sep 25th, 2008, 11:15 PM

Thread Starter Lively Member

-

Re: add handler with button click in flowlayoutpanel

im sry but i dont understand [new to VB, php programmer]

can you explain or show me how to

"get the sender, cast it as type PictureBox and then get the ImageLocation property"

as you said above, I just dont understand how to cast the sender...

when i type sender. in the event handler, theres nothing about imagelocation. but i have to cast it 1st right?

- Sep 25th, 2008, 11:31 PM

Re: add handler with button click in flowlayoutpanel

vb.net Code:

  1. Public Class Form1
  2. Private Sub Form1_Load[
  3. ByVal sender As System.Object,
  4. ByVal e As System.EventArgs
  5. ] Handles MyBase.Load
  6. AddHandler PictureBox1.Click, AddressOf PictureBox_Click
  7. End Sub
  8. Private Sub PictureBox_Click[
  9. ByVal sender As System.Object,
  10. ByVal e As System.EventArgs
  11. ]
  12. Dim pb As PictureBox = DirectCast[sender, PictureBox]
  13. MessageBox.Show[pb.ImageLocation]
  14. End Sub
  15. End Class -
  16. Sep 25th, 2008, 11:32 PM

    Re: add handler with button click in flowlayoutpanel

    Originally Posted by ahostbr

when i type sender. in the event handler, theres nothing about imagelocation. but i have to cast it 1st right?

That's right, which you do using DirectCast, e.g. DirectCast[sender, PictureBox]. If you have Option Strict turned Off then you could use late-binding but it's better to cast regardless.

- Sep 25th, 2008, 11:49 PM

Thread Starter Lively Member

-

Re: add handler with button click in flowlayoutpanel

Ok it says when i pass process.start["pb.ImageLocation"] that" process could not start because no filename was provided."

And then when i pass it msgbox["pb.ImageLocation.tostring"] i get "object reference not set to an instance of an object."

And if i do msgbox["pb.ImageLocation"]

i get no errors but a blank box...

Heres a my code from a testing app i made without my download part.

Code:

Private Sub Button2_Click_2[ByVal sender As System.Object, ByVal e As System.EventArgs] Handles Button2.Click 'Dim invalidF As String = 0 Try FolderBrowserDialog1.ShowNewFolderButton = False FolderBrowserDialog1.ShowDialog[] For Each foundFile As String In My.Computer.FileSystem.GetFiles[FolderBrowserDialog1.SelectedPath] 'Dim foundFile As String 'Select Case IO.Path.GetExtension[foundFile] ' Case ".jpg", ".png", ".gif", ".Jpeg", ".Png", "Gif", "bmp", ".Bmp", ".icon", ".ico", "Icon", ".Ico" Try Dim image As Image = Nothing ' Check if textbox has a value ' Check if image exists image = image.FromFile[foundFile] If Not image Is Nothing Then Dim pbo As New PictureBox[] With pbo .Size = New Size[100, 100] .SizeMode = PictureBoxSizeMode.StretchImage AddHandler pbo.Click, AddressOf PictureBox1_MouseUp .Image = image.GetThumbnailImage[100, 100, Nothing, New IntPtr[]] End With FlowLayoutPanel1.Controls.Add[pbo] End If Catch MessageBox.Show["An error occured"] End Try 'Case Else 'invalidF = 1 'End Select Next Catch err As Exception If DialogResult.Cancel = True Then Return End If End Try 'If invalidF = "1" Then 'MsgBox["Invalid FileTypes Selected...These Files Were Not Added"] 'End If End Sub Private Sub PictureBox1_MouseUp[ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs] Handles PictureBox1.MouseUp Dim pb As PictureBox = DirectCast[sender, PictureBox] 'MessageBox.Show[pb.ImageLocation] Try MsgBox[pb.ImageLocation] Catch ex As Exception MsgBox[ErrorToString] End Try End Sub

Edit: Ive tested with this and it gave the correct filepath and name, And also loaded the picture into the picture, box

Code:

PictureBox1.ImageLocation = "C:\Users\root\Pictures\2.jpg" MsgBox[PictureBox1.ImageLocation.ToString] Last edited by ahostbr; Sep 26th, 2008 at 12:06 AM.

- Sep 26th, 2008, 12:16 AM

Re: add handler with button click in flowlayoutpanel

So when I said this before: Note that this assumes that you have loaded the Image in the first place by calling the PictureBox's Load method.

How do I change the order of controls in FlowLayoutPanel?

For basic control ordering, the simplest way to control the order of controls in the flowlayoutPanel is to set the flowlayoutPanel TabStop property to true. Set the controls tabstop property to True and set the tab order to the order in which you want the controls to appear.

What is the difference between FlowLayoutPanel and Tablelayoutpanel?

TableLayout will keep your controls in a grid pattern, and will resized to fill the form [if docked to the parent container]. A FlowLayout will place controls in a [by default] horizontal line, and wrap the controls to the next line if they don't all fit. The wrapping should happen dynamically as the panel is resized.

What is the use of flow layout panel in VB net?

The FlowLayoutPanel control arranges its contents in a horizontal or vertical flow direction. You can wrap the control's contents from one row to the next, or from one column to the next. Alternately, you can clip instead of wrap its contents.

What is the use of FlowLayoutPanel in C#?

The FlowLayoutPanel arranges its contents in a specific flow direction: horizontal or vertical. Its contents can be wrapped from one row to the next, or from one column to the next. Alternately, its contents can be clipped instead of wrapped.

Chủ Đề