【问题标题】:Error when using auto resize picturebox/groupbox vb.net使用自动调整图片框/组框 vb.net 时出错
【发布时间】:2014-05-27 17:02:57
【问题描述】:

您好,当我从 exe 文件加载程序时出现错误,但从 Visual Studio 启动时却没有。

我收到的错误是

>System.NullReferenceException: Object reference not set to an instance of an object.

在 C:\Users\AJ\Documents\Movable_Re217400212010\MoveResizeControl\controlHandler.vb:line 211 中的 MoveResizeControl.controlHandler.mControl_Resize(Object sender, EventArgs e) 处

在 System.Windows.Forms.Control.OnSizeChanged(EventArgs e) 在 System.Windows.Forms.Control.UpdateBounds(Int32 x, Int32 y, Int32 宽度, Int32 高度, Int32 clientWidth, Int32 clientHeight)

在 System.Windows.Forms.Control.UpdateBounds() 在 System.Windows.Forms.Control.WmWindowPosChanged(消息和 m) 在 System.Windows.Forms.Control.WndProc(消息和 m) 在 System.Windows.Forms.GroupBox.WndProc(消息和 m) 在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam

我知道我的问题在于这段代码

    Private Sub Timer7_Tick(sender As Object, e As EventArgs) Handles Timer7.Tick
    Timer2.Start()
    Timer5.Start()
    Dim i As Integer
    i = 0
    Dim bl1 As Integer
    Dim br1 As Integer
    Dim tl1 As Integer
    Dim tr1 As Integer
    Dim bl14 As Integer
    Dim br14 As Integer
    Dim tl14 As Integer
    Dim tr14 As Integer

    bl1 = bl.Text
    br1 = br.Text
    tl1 = tl.Text
    tr1 = tr.Text

    bl14 = bl4.Text
    br14 = br4.Text
    tl14 = tl4.Text
    tr14 = tr4.Text

    Me.PictureBox1.Height = (i + bl1) - (i + tl1)
    Me.PictureBox1.Width = (i + br1) - (i + tr1)
    Me.GroupBox1.Height = (i + bl14) - (i + tl14)
    Me.GroupBox1.Width = (i + br14) - (i + tr14)

    Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X + (0 + tr.Text), Me.PictureBox1.Location.Y + (0 + tl.Text))
    Me.GroupBox1.Location = New Point(Me.GroupBox1.Location.X + (0 + tr4.Text), Me.GroupBox1.Location.Y + (0 + tl4.Text))
    Timer7.Stop()
End Sub

我需要在开始之前检查 null 并且遇到了问题。

【问题讨论】:

  • 您正在将文本分配给整数!编码不好。我知道 vb 可以处理它,但如果文本无效怎么办。不要滥用语言
  • 开启 Option Strict,修复所有这些错误,这个错误就会消失
  • 好的,谢谢大家,这里已经很晚了,明天下班后我会继续写这篇文章,我会得到帮助。文本字段将仅包含保存到 txt 文件中的数字,并且在打开程序时会读取此文件以填充文本框。

标签: vb.net error-handling nullreferenceexception autoresize


【解决方案1】:

可能在初始化完成之前执行了计时器滴答事件。尝试禁用计时器,然后以“显示”事件的形式启用它们。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-09-23
    • 1970-01-01
    • 2016-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-05-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多