【问题标题】:VB.NET form.show() errorVB.NET form.show() 错误
【发布时间】:2013-09-25 22:36:08
【问题描述】:

每次使用时:

form1.show()

我收到Reference to a non-shared member requires an object reference.

到现在还有效。不知道是什么问题。

此外,它甚至不显示在“启动表单”下拉菜单中。

编辑:包括整个代码。

Private _cpuid As String


///Here is the generated constructor

    Sub New()
        ' TODO: Complete member initialization 
    End Sub



    Public ReadOnly Property cpuid As String
        Get
            Return _cpuid
        End Get
    End Property

    Private _pc As PerformanceCounter
    Private _currentvalue As Integer = 0
    Public Sub New(ByVal cpuid As String)
        InitializeComponent()
        _cpuid = cpuid
        _pc = New PerformanceCounter("Processes", "CPU Usage (%)", cpuid)
        Me.ProgressBar1.Maximum = 100
        Me.ProgressBar1.Minimum = 0

        Me.Label1.Text = "CPU" & cpuid
    End Sub
    Public Sub callperformancecounter()
        _currentvalue = CInt(_pc.NextValue())
        Me.ProgressBar1.Value = _currentvalue
        Me.label2.text = _currentvalue & " %"


    End Sub

【问题讨论】:

    标签: vb.net forms show


    【解决方案1】:

    假设项目中有一个名为 form1 的表单,您需要创建它的一个实例:

    Dim frm as New Form1    ' creates the instance the msg is talking about
    
    frm.Show
    

    编辑新信息...

    你已经重写了构造函数,然后没有使用它。我不会那样做,在 Form Load 事件中进行 CPU 设置(只需移动代码)。修复你的 Sub New :

    Sub New(cpuID As String)
        ' TODO: Complete member initialization 
    
         InitializeComponent()      ' the TODO is telling you this is needed
    
         _cpuID = cpuID
    End Sub
    

    表单加载将是您的代码的其余部分:

      _pc = New PerformanceCounter("Processes", "CPU Usage (%)", cpuid)
      Me.ProgressBar1.Maximum = 100
      Me.ProgressBar1.Minimum = 0
    
      Me.Label1.Text = "CPU" & cpuid
    

    如果将 cpuid 传递给 New 或设置属性,则不需要将 cpuid 传递给过程(到目前为止,您实际上并不需要这两种方法)。

    现在,您要显示表单的方式是:

      Dim frm as Form1                   ' declare what frm is
    
      frm = New Form1(cpuname)           ' this 'NEW' triggers 'Sub New'
    
      frm.Show
    

    【讨论】:

    • 做到了,然后我得到了构造函数错误,这是通过在表单代码中创建一个修复的。但是现在,当表单加载时,没有对象(标签,文本框......)
    • 可能很明显,但是您是否在表单设计器中为 Form1 添加了标签和文本框?如果您清理并构建项目,然后打开设计器,它们还在吗?你在Form1.Sub New() 对他们做了什么吗?请您编辑您的帖子以显示 Sub New。
    • 这是我得到的Error 1 Argument not specified for parameter 'name' of 'Public Sub New(name As String)'。设计师通过在表单中​​插入构造函数代码来修复它,但在调试时没有显示控件。虽然它在我编辑表单时显示。
    • 明白没有代码我(我们)闭着眼睛在黑暗中狩猎。 name 是什么?它有什么作用?你为什么添加它?请更新您的问题以包含Sub New
    • 它插入这样的代码Sub New() ' TODO: Complete member initialization End Sub
    猜你喜欢
    • 2011-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-09
    • 2016-01-16
    • 2011-02-08
    相关资源
    最近更新 更多