VB.NET form.show() error

2019-07-20 07:34发布

问题:

Every time when i use :

form1.show()

I get Reference to a non-shared member requires an object reference.

It worked till now.I don't know what's the problem.

Also,it does not even show in "Startup form" dropdown menu.

Edit : Included whole code.

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:

assuming a form named form1 in the proj you need to create an instance of it:

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

frm.Show

EDIT for new info...

You have overridden the constructor, then not used it. I would not do it that way, do the CPU setup stuff in the Form Load event (just move the code). Fix your Sub New to this:

Sub New(cpuID As String)
    ' TODO: Complete member initialization 

     InitializeComponent()      ' the TODO is telling you this is needed

     _cpuID = cpuID
End Sub

the form load would be the rest of your code:

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

  Me.Label1.Text = "CPU" & cpuid

You dont need to pass the cpuid to the procedure if you pass it to New or set the Property (you dont really need both methods for what you have so far).

NOW, the way you want to show the form is:

  Dim frm as Form1                   ' declare what frm is

  frm = New Form1(cpuname)           ' this 'NEW' triggers 'Sub New'

  frm.Show