VB.NET form.show() error

谁说我不能喝 提交于 2019-12-24 03:29:42

问题


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


来源:https://stackoverflow.com/questions/19007163/vb-net-form-show-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!