Emulate Picture Box double click

W

wkaibigan

I have the following code

Private Sub PictureBox_DoubleClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Try
Dim p As PictureBox = CType(sender, PictureBox)
Dim filetemp As String
filetemp = Replace(p.Tag, "Thumb", "jpg")
If Me.UseEditor = True Then
System.Diagnostics.Process.Start(filetemp)
Else

End If

_Image = Image.FromFile(filetemp)

Catch ex As Exception
Throw New Exception("PictureBox_DoubleClick", ex)
End Try

End Sub

Is there a way to emulate this functionality by single clicking the
image to select and and then click a button on the form to perform the
same action as the above code? If so, please could you provide sample
code.

Thanks.
 
P

Phill W.

wkaibigan said:
Private Sub PictureBox_DoubleClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
Try
Dim p As PictureBox = CType(sender, PictureBox)
Dim filetemp As String
filetemp = Replace(p.Tag, "Thumb", "jpg")
If Me.UseEditor = True Then
System.Diagnostics.Process.Start(filetemp)
Else

End If

_Image = Image.FromFile(filetemp)

Catch ex As Exception
Throw New Exception("PictureBox_DoubleClick", ex)
End Try

End Sub

Is there a way to emulate this functionality by single clicking the
image to select and and then click a button on the form to perform the
same action as the above code? If so, please could you provide sample
code.

Extract the code into a separate routine and call it wherever you want
to call it from.

Private Sub EditPicture( ByVal sender as PictureBox )
'Try
Dim p As PictureBox = CType(sender, PictureBox)
Dim filetemp As String _
= Replace(p.Tag, "Thumb", "jpg")
If Me.UseEditor = True Then
System.Diagnostics.Process.Start(filetemp)
Else
???
End If

_Image = Image.FromFile(filetemp)

'Catch ex As Exception
' Throw New Exception("PictureBox_DoubleClick", ex)
'End Try
End Sub

.... then ...

Private m_CurrentPicture as PictureBox = Nothing

Private Sub PictureBox_Click(ByVal sender As System.Object _
, ByVal e As System.EventArgs)

m_CurrentPictureBox = DirectCast( sender, PictureBox )

End Sub

Private Sub Button7_Click(ByVal sender As System.Object _
, ByVal e As System.EventArgs)

EditPicture( m_CurrentPictureBox )

End Sub

You'll note I've commented out your Exception Handling in EditPicture -
there's no point catching an Exception unless you're going to do
something /useful/ with it (and, IMHO, wrapping a nice, specific
Exception in a System.Exception isn't).

HTH,
Phill W.
 
K

kimiraikkonen

I have the following code

Private Sub PictureBox_DoubleClick(ByVal sender As System.Object,
ByVal e As System.EventArgs)
        Try
            Dim p As PictureBox = CType(sender, PictureBox)
            Dim filetemp As String
            filetemp = Replace(p.Tag, "Thumb", "jpg")
            If Me.UseEditor = True Then
                System.Diagnostics.Process.Start(filetemp)
            Else

            End If

            _Image = Image.FromFile(filetemp)

        Catch ex As Exception
            Throw New Exception("PictureBox_DoubleClick", ex)
        End Try

    End Sub

Is there a way to emulate this functionality by single clicking the
image to select and and then click a button on the form to perform the
same action as the above code? If so, please could you provide sample
code.

Thanks.

Hi,
As Picturebox is a control, and control class has OnDoubleClick method
to raise DoubleClick event. So you can directly call it from anywhere
and you'd better look at that method:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.ondoubleclick.aspx

Hope this helps,

Onur Güzel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top