2

每次我使用时:

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
4

1 回答 1

3

假设项目中名为 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
于 2013-09-25T14:06:53.937 回答