gpt4 book ai didi

VB.NET form.show() 错误

转载 作者:行者123 更新时间:2023-12-04 04:40:07 24 4
gpt4 key购买 nike

每次我使用:

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

最佳答案

假设在 proj 中有一个名为 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 或设置 Property,则不需要将 cpuid 传递给过程(到目前为止,您实际上并不需要这两种方法)。

现在,您要显示表单的方式是:
  Dim frm as Form1                   ' declare what frm is

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

frm.Show

关于VB.NET form.show() 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19007163/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com