VB.NET form.show()错误 [英] VB.NET form.show() error

查看:193
本文介绍了VB.NET form.show()错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次当我使用时:

  form1.show()

我得到引用非共享成员需要一个对象引用。



它一直工作到现在。我不知道有什么问题。



另外,它甚至没有显示在Startup form下拉菜单。



编辑:包含整个代码。

 私人_cpuid As String 


///这里是生成的构造函数

Sub New()
'TODO:完成成员初始化
End Sub



公共ReadOnly属性cpuid作为字符串
获取
返回_cpuid
结束获取
完成属性

Private _pc As PerformanceCounter
Private _currentvalue As Integer = 0
Public Sub New(ByVal cpuid As String)
InitializeComponent()
_cpuid = cpuid
_pc =新的PerformanceCounter(过程es,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& %


结束Sub


解决方案<假设在proj中有一个名为form1的表单,你需要创建一个实例: New Form1'创建msg正在谈论的实例

frm.Show

编辑新信息...



您已经重写了构造函数,然后不使用它。我不这样做,在表单加载事件中执行CPU设置的东西(只是移动代码)。修正你的Sub New:

  Sub New(cpuID As String)
'TODO:完成成员初始化

InitializeComponent()'TODO告诉你这是需要的

_cpuID = cpuID
End Sub

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

  _pc =新的PerformanceCounter(进程,CPU使用率(%),cpuid)
Me.ProgressBar1.Maximum = 100
Me.ProgressBar1.Minimum = 0

Me。 Label1.Text =CPU& cpuid

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



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

  Dim frm as Form1'声明frm是

frm =新Form1(cpuname)'这个'NEW'触发'Sub New'

frm.Show


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

解决方案

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

这篇关于VB.NET form.show()错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆