VB.NET form.show()错误

时间:2013-09-25 14:04:26

标签: vb.net forms show

每次使用时:

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

1 个答案:

答案 0 :(得分: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