Home > Software engineering >  The Image of a PictureBox doesn't appear in a new Form, which should close when deactivated
The Image of a PictureBox doesn't appear in a new Form, which should close when deactivated

Time:03-11

The Image of a PictureEdit or PictureBox is not shown in a new form (Form2).
Also, this new Form should auto-close when clicking on a different Control in the parent form (form1).

With my code, only the third Image appears when I double-click on an Image in Form1.

Is there a solution so that I do not need to create 3 new forms according to the number of images in Form1?.

For the record, I use Visual Studio 2010

Public Class Form1
   Private Sub PictureEdit1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureEdit1.DoubleClick
        Dim yourForm As New Form2()
        yourForm.ShowDialog()
    End Sub

    Private Sub PictureEdit2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureEdit2.DoubleClick
        Dim yourForm As New Form2()
        yourForm.ShowDialog()
    End Sub
    Private Sub PictureEdit3_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureEdit3.DoubleClick
        Dim yourForm As New Form2()
        yourForm.ShowDialog()
    End Sub
End Class

Public Class Form2
    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim Form_One As Form1 = CType(Application.OpenForms("form1"), Form1)
        Me.PictureEdit1.Image = Form_One.PictureEdit1.Image
        Me.PictureEdit1.Image = Form_One.PictureEdit2.Image
        Me.PictureEdit1.Image = Form_One.PictureEdit3.Image
    End Sub
End Class

picturedit1,picturedit2,picturedit3 from form1 result picturedit1 from form2

CodePudding user response:

Small changes required:

  • Add a constructor to your Form2 that accepts an Image as argument.
  • Override OnDeactivate, to close the Form when you click outside of this Form.
  • Use Show(Me) instead of ShowDialog(): this will make the calling Form the Owner of Form2, so it stays on top of it, but it's not a Modal Window, which is not deactivated when you click outside of it.

Public Class Form1
   ' [...]
   Private Sub PictureEdit1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureEdit1.DoubleClick
        Dim f2 As New Form2(PictureEdit1.Image)
        f2.Show(Me)
    End Sub
    ' [...]
End Class

Public Class Form2
    Public Sub New()
        Me.New(Nothing)
    End Sub
    Public Sub New(img As Image)
        InitializeComponent()
        PictureEdit1.Image = img
    End Sub

    Protected Overrides Sub OnDeactivate(e As EventArgs)
        MyBase.OnDeactivate(e)
        BeginInvoke(New Action(Sub() Close()))
    End Sub
End Class 

CodePudding user response:

Once you've made the changes suggested by Jimi, you can also use ONE sub to handle all three PictureEdit controls by listing more than one control/event after the "Handles" keyword:

Private Sub PictureEditAll_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureEdit1.DoubleClick, PictureEdit2.DoubleClick, PictureEdit3.DoubleClick
    Dim picEdit As PictureEdit = DirectCast(sender, PictureEdit)
    Dim f2 As New Form2(picEdit.Image)
    f2.Show(Me)
End Sub
  • Related