none
vb.net 关于PictureBox和Panel滚动条问题 未将对象引用设置到对象的实例。 RRS feed

  • 问题

  • 代码:

    本来利用的是PictureBox的AutoSize属性 再放到一个Panel里面得以增加滚动条

        Private Sub Panel1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint
            PictureBox.Size = New Point(PictureBox.Image.Width, PictureBox.Image.Height)
        End Sub

    有时可以读取 而有时不行

    报错时Panel周围ScrollBar的区域出现红线

    System.NullReferenceException: 未将对象引用设置到对象的实例。
       在 Personal_Tools.Form_ClipPic.Panel1_Paint(Object sender, PaintEventArgs e) 位置 *(本人已删除) :行号 23
       在 System.Windows.Forms.Control.OnPaint(PaintEventArgs e)
       在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer)
       在 System.Windows.Forms.Control.WmPaint(Message& m)
       在 System.Windows.Forms.Control.WndProc(Message& m)
       在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

    2015年1月16日 14:50

答案

  • 1. 可能你贴错代码了,下面应该是Size而不是Point, Point 只是一个坐标

     PictureBox.Size = New Point(PictureBox.Image.Width, PictureBox.Image.Height)

    2. 其实panel的大小应该已经固定,我们需要在 picturebox的size变化的时候处理下picturebox的位置, 比如我们想要它一直居中,则可以尝试以下方式。

        Private Sub PictureBox1_Resize(sender As Object, e As EventArgs) Handles PictureBox1.Resize
            Me.Panel1.VerticalScroll.Value = 0
            Me.Panel1.HorizontalScroll.Value = 0
            If Me.PictureBox1.Image IsNot Nothing Then
    
    
                Dim picSize As Size = Me.PictureBox1.Image.Size
                Dim panSize As Size = Me.Panel1.Size
                If picSize.Height < panSize.Height Then
                    Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X, (panSize.Height - picSize.Height) / 2)
                Else
                    Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X, 0)
                End If
                If picSize.Width < panSize.Width Then
                    Me.PictureBox1.Location = New Point((panSize.Width - picSize.Width) / 2, Me.PictureBox1.Location.Y)
                Else
                    Me.PictureBox1.Location = New Point(0, Me.PictureBox1.Location.Y)
                End If
    
            End If
        End Sub


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    2015年1月19日 4:51
    版主