vb.net在用户控件上调用子例程

时间:2021-09-07 00:01:54

I have a panel2 inside a split container that has several user controls loaded into it. Panel 1 has an exit button and I want to call one of the sub routines that is in one of the user controls loaded into Panel2.

我在一个拆分容器中有一个panel2,它有几个用户控件加载到它里面。面板1有一个退出按钮,我想调用其中一个加载到Panel2的用户控件中的子例程。

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Dim dialogMessage As DialogResult
        Dim a As New ucTimeTracker

    dialogMessage = MessageBox.Show("Are you sure you want to exit?", "Exit Ready Office Assistant?", _
    MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question)

    If dialogMessage = Windows.Forms.DialogResult.Yes Then

        ucTimeTracker.autoWriteFileOnExit()
        Me.Close()

    Else
        Return
    End If

End Sub

This line is giving me trouble.

这条线给了我麻烦。

ucTimeTracker.autoWriteFileOnExit()

I am getting (reference to non-shared member requires an object reference).

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

I want the exit button on frmMain.SplitContainer.Panel1 to call autoWriteFileOnExit() on the user control named ucTimeTracker that is loaded into splitContainer.Panel2

我希望frmMain.SplitContainer.Panel1上的退出按钮在名为ucTimeTracker的用户控件上调用autoWriteFileOnExit(),该控件被加载到splitContainer.Panel2中

2 个解决方案

#1


It seems that you are using the user control class name ucTimeTracker instead of the instance name. Click on the user control in design view and in the properties view, there's a "Name" property. use the value in the name property (probably ucTimeTracker1) instead:

您似乎使用的是用户控件类名ucTimeTracker而不是实例名。在设计视图中单击用户控件,在属性视图中,有一个“名称”属性。使用name属性中的值(可能是ucTimeTracker1):

  ucTimeTracker1.autoWriteFileOnExit()

#2


You are using ucTimeTracker to reference the method, which is the name of the class. Earlier in the method you create an instance of that class (Dim a As New ucTimeTracker), so you should call a. autoWriteFileOnExit() instead, if this is the instance that you want to use. If ucTimeTracker is a control on the form, you should instead use the name of that control.

您正在使用ucTimeTracker来引用该方法,该方法是类的名称。在该方法的早期,您创建了该类的实例(Dim a As New ucTimeTracker),因此您应该调用a。 autoWriteFileOnExit(),如果这是您要使用的实例。如果ucTimeTracker是表单上的控件,则应该使用该控件的名称。

To understand this you need to understand the difference between static members and instance members. A static member can be accessed directly through the class, without the need to create an instance of the class. In order to use an instance member you will need an instance of the class first. You can look at the Int32 class as an example:

要理解这一点,您需要了解静态成员和实例成员之间的区别。可以通过类直接访问静态成员,而无需创建类的实例。要使用实例成员,首先需要该类的实例。您可以查看Int32类作为示例:

' call a static method in the Int32 class, that returns an Int32 instance'
Dim asInt As Int32 = Int32.Parse("14") 
' call an instance method on the Int32 instance, that will act on the data in '
' that instance, returning a string representation of its value '
Dim asString As String = asInt.ToString()

Typically static methods does not act on data that is held inside the class (though this is not always true), but rather acts on data passed to the methods through parameters. Instance methods have access to the internal data of that specific instance, and can act on that data (as in the example above).

通常,静态方法不会对类中保存的数据起作用(虽然这并非总是如此),而是作用于通过参数传递给方法的数据。实例方法可以访问该特定实例的内部数据,并可以对该数据进行操作(如上例所示)。

#1


It seems that you are using the user control class name ucTimeTracker instead of the instance name. Click on the user control in design view and in the properties view, there's a "Name" property. use the value in the name property (probably ucTimeTracker1) instead:

您似乎使用的是用户控件类名ucTimeTracker而不是实例名。在设计视图中单击用户控件,在属性视图中,有一个“名称”属性。使用name属性中的值(可能是ucTimeTracker1):

  ucTimeTracker1.autoWriteFileOnExit()

#2


You are using ucTimeTracker to reference the method, which is the name of the class. Earlier in the method you create an instance of that class (Dim a As New ucTimeTracker), so you should call a. autoWriteFileOnExit() instead, if this is the instance that you want to use. If ucTimeTracker is a control on the form, you should instead use the name of that control.

您正在使用ucTimeTracker来引用该方法,该方法是类的名称。在该方法的早期,您创建了该类的实例(Dim a As New ucTimeTracker),因此您应该调用a。 autoWriteFileOnExit(),如果这是您要使用的实例。如果ucTimeTracker是表单上的控件,则应该使用该控件的名称。

To understand this you need to understand the difference between static members and instance members. A static member can be accessed directly through the class, without the need to create an instance of the class. In order to use an instance member you will need an instance of the class first. You can look at the Int32 class as an example:

要理解这一点,您需要了解静态成员和实例成员之间的区别。可以通过类直接访问静态成员,而无需创建类的实例。要使用实例成员,首先需要该类的实例。您可以查看Int32类作为示例:

' call a static method in the Int32 class, that returns an Int32 instance'
Dim asInt As Int32 = Int32.Parse("14") 
' call an instance method on the Int32 instance, that will act on the data in '
' that instance, returning a string representation of its value '
Dim asString As String = asInt.ToString()

Typically static methods does not act on data that is held inside the class (though this is not always true), but rather acts on data passed to the methods through parameters. Instance methods have access to the internal data of that specific instance, and can act on that data (as in the example above).

通常,静态方法不会对类中保存的数据起作用(虽然这并非总是如此),而是作用于通过参数传递给方法的数据。实例方法可以访问该特定实例的内部数据,并可以对该数据进行操作(如上例所示)。