locked
Invalid operation exception was unhandeled due to empty a listview1 in other form (form32) or any code with Form32 after adding the code indicated below RRS feed

  • Question

  • Hi,

    Invalid operation exception was unhandeled due to empty a listview1 in other form (form32) or any code with Form32 after adding the code indicated below. 

    as show in the two images below.

    Private Sub PictureBox2_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox2.Paint
    
    With e.Graphics
                    .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                    .ScaleTransform(Sx, Sx)
                    .DrawImage(bm, 0, 0)
                    If resizing Then
                        .DrawRectangle(RSpen, resizerect)
                    End If
                End With
            End If
        End Sub
        Dim sx, zoomfactor As Single
        Dim resizing As Boolean = False
        Dim resizerect As New Rectangle
        Dim RSpen As New Pen(Color.Black)
        Dim bm As New Bitmap(PictureBox2.Width, PictureBox2.Height)
        Dim currMP As Point
        Private Sub Form32Irregular_byCoordinates_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox2.Refresh()
            Sx = 1 : zoomfactor = 2.0
            With RSpen
                .Width = 4.0F
                .DashCap = Drawing2D.DashCap.Round
                .DashPattern = New Single() {4.0F, 2.0F}
            End With
        End Sub
        Private Sub ZoomToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZoomToolStripMenuItem.Click
            resizerect = Rectangle.Round(bm.GetBounds(GraphicsUnit.Pixel))
            resizing = True
            currMP = PointToClient(MousePosition)
            PictureBox2.Invalidate()
        End Sub
        Private Sub ResetToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetToolStripMenuItem.Click
            sx = 1
            resizing = False
            PictureBox2.Invalidate()
        End Sub
        Private Sub picturebox2_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseClick
            resizing = False
            PictureBox2.Invalidate()
        End Sub
        Private Sub picturebox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            If resizing Then
                If e.Location.X > currMP.X Then
                    sx += zoomfactor / 20
                End If
                If e.Location.X < currMP.X Then
                    sx -= zoomfactor / 20
                End If
                currMP = e.Location
                PictureBox2.Invalidate()
            End If
        End Sub

    Can any one assist.

    Kind Regards,


    Hany Metry







    • Edited by Morgan Mosa Sunday, November 10, 2019 12:11 PM add
    Sunday, November 10, 2019 9:15 AM

All replies

  • "Object ref not set to instance "

    That is almost always because you did not include the NEW part somewhere for something...

    ie


      Dim ListView1 as NEW ListView


    Sunday, November 10, 2019 9:54 AM
  • Hi Tommy,

    Dim LV32 As New ListView
            LV32 = Form32Irregular_byCoordinates.ListView1
            LV32.Clear()

    The above code does not solve the problem.


    Hany Metry

    Sunday, November 10, 2019 10:09 AM
  • "Object ref not set to instance "

    That is almost always because you did not include the NEW part somewhere for something...

    ie


      Dim ListView1 as NEW ListView


    Hi,

    I guess the problem due to undefined form32

    But the code in the image not accepted.


    Hany Metry

    Sunday, November 10, 2019 10:28 AM
  • "Object ref not set to instance "

    That is almost always because you did not include the NEW part somewhere for something...

    ie


      Dim ListView1 as NEW ListView


    Hi,

    I guess the problem due to undefined form32

    But the code in the image not accepted.


    Hany Metry

    Hi,

    The below code is not accepted also

    Dim F32 As New Form
            F32 = Form32Irregular_byCoordinates
            Dim LV32 As New ListView
            LV32 = F32.listview1
            LV32.Clear()


    Hany Metry

    Sunday, November 10, 2019 10:52 AM

  • Hi,

    The below code is not accepted also

    Dim F32 As New Form
            F32 = Form32Irregular_byCoordinates
            Dim LV32 As New ListView
            LV32 = F32.listview1
            LV32.Clear()


    Hany Metry

    What is this?

    F32 = Form32Irregular_byCoordinates

    That is not how you make a copy of a form (instance). Stop all other and learn that only. One thing at a time. Go make a practice project and make a new form as you want.

    Yeah, see you need to get that issue alone by itself. You cant learn to integrate if you cant sin theta and if you have unrelated divide by zero beeping at you all the time.

    I am not sure exactly what you want to do. Make a second instance of a form with no values? Search for that. Make a one form example...

    PS I guess what you have might work for the form but I am not sure. That is what I mean. We cant debug your project we don't have it.

    Sunday, November 10, 2019 11:24 AM

  • Hi,

    The below code is not accepted also

    Dim F32 As New Form
            F32 = Form32Irregular_byCoordinates
            Dim LV32 As New ListView
            LV32 = F32.listview1
            LV32.Clear()


    Hany Metry

    What is this?

    F32 = Form32Irregular_byCoordinates

    That is not how you make a copy of a form (instance). Stop all other and learn that only. One thing at a time. Go make a practice project and make a new form as you want.

    Yeah, see you need to get that issue alone by itself. You cant learn to integrate if you cant sin theta and if you have unrelated divide by zero beeping at you all the time.

    I am not sure exactly what you want to do. Make a second instance of a form with no values? Search for that. Make a one form example...

    PS I guess what you have might work for the form but I am not sure. That is what I mean. We cant debug your project we don't have it.

    Hi Tommy,

    First I want to high light that I am not understand your English.

    The problem because I referred to  Form32 in Form31.

    Listview1 is in Form32 and I dont know how I can referred to Listview1 (which in form32) while I rename form32 by F32 as new form.

    I need to learn and please learn me my teacher.

    Kind Regards,

                             


    Hany Metry

    Sunday, November 10, 2019 11:42 AM

  • Hi,

    The below code is not accepted also

    Dim F32 As New Form
            F32 = Form32Irregular_byCoordinates
            Dim LV32 As New ListView
            LV32 = F32.listview1
            LV32.Clear()


    Hany Metry

    What is this?

    F32 = Form32Irregular_byCoordinates

    That is not how you make a copy of a form (instance). Stop all other and learn that only. One thing at a time. Go make a practice project and make a new form as you want.

    Yeah, see you need to get that issue alone by itself. You cant learn to integrate if you cant sin theta and if you have unrelated divide by zero beeping at you all the time.

    I am not sure exactly what you want to do. Make a second instance of a form with no values? Search for that. Make a one form example...

    PS I guess what you have might work for the form but I am not sure. That is what I mean. We cant debug your project we don't have it.

    Hi,

    I solved that problem by deleting the code which transfer the picture box 2 (which in Form32) to be bitmap.

    Delete all the code below.

    Private Sub PictureBox2_Paint(sender As Object, e As PaintEventArgs) Handles PictureBox2.Paint
    
    With e.Graphics
                    .SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
                    .ScaleTransform(Sx, Sx)
                    .DrawImage(bm, 0, 0)
                    If resizing Then
                        .DrawRectangle(RSpen, resizerect)
                    End If
                End With
            End If
        End Sub
        Dim sx, zoomfactor As Single
        Dim resizing As Boolean = False
        Dim resizerect As New Rectangle
        Dim RSpen As New Pen(Color.Black)
        Dim bm As New Bitmap(PictureBox2.Width, PictureBox2.Height)
        Dim currMP As Point
        Private Sub Form32Irregular_byCoordinates_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            PictureBox2.Refresh()
            Sx = 1 : zoomfactor = 2.0
            With RSpen
                .Width = 4.0F
                .DashCap = Drawing2D.DashCap.Round
                .DashPattern = New Single() {4.0F, 2.0F}
            End With
        End Sub
        Private Sub ZoomToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ZoomToolStripMenuItem.Click
            resizerect = Rectangle.Round(bm.GetBounds(GraphicsUnit.Pixel))
            resizing = True
            currMP = PointToClient(MousePosition)
            PictureBox2.Invalidate()
        End Sub
        Private Sub ResetToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ResetToolStripMenuItem.Click
            sx = 1
            resizing = False
            PictureBox2.Invalidate()
        End Sub
        Private Sub picturebox2_MouseClick(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseClick
            resizing = False
            PictureBox2.Invalidate()
        End Sub
        Private Sub picturebox2_MouseMove(sender As Object, e As MouseEventArgs) Handles PictureBox2.MouseMove
            If resizing Then
                If e.Location.X > currMP.X Then
                    sx += zoomfactor / 20
                End If
                If e.Location.X < currMP.X Then
                    sx -= zoomfactor / 20
                End If
                currMP = e.Location
                PictureBox2.Invalidate()
            End If
        End Sub

    Kind Regards,  


    Hany Metry


    • Edited by Morgan Mosa Sunday, November 10, 2019 12:13 PM add
    • Marked as answer by Morgan Mosa Sunday, November 10, 2019 12:13 PM
    • Unmarked as answer by Morgan Mosa Sunday, November 10, 2019 12:14 PM
    Sunday, November 10, 2019 12:07 PM
  • "First I want to high light that I am not understand your English."

    Yes I understand. That is one reason I show you code examples and images. But we cant teach you every detail.

    Some things I am not the best at either so others can explain better.

    Here is a simple example of a dialog. That is form which opens on the screen and has total focus nothing else in the application can be used until the form is closed. If you dont want dialog then its done slightly different.

    Click the button and Form4 uses showdialog for NEW form5:

    Click button on form5 to put value in form5 variable and close form5.

    Finally form4 shows the form5 variable in the form4 heading text 0.67. Form5 is disposed by the end using statement in form4.

    Public Class Form4
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    
            Using thisForm5 As New Form5
                thisForm5.ShowDialog()
    
                Text = thisForm5.Form5Value.ToString
            End Using
    
        End Sub
    End Class
    
    Public Class Form5
    
        Public Form5Value As Double
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Form5Value = 2 / 3
    
            Close()
        End Sub
    End Class

    .

    See, in the time I can explain it the problem is something else...

    :)




    Sunday, November 10, 2019 12:32 PM