简体   繁体   English

用句柄调用Public Sub

[英]Calling Public Sub with Handles

Trying to do something that is probably simple. 尝试做一些可能很简单的事情。 I have an event firing when you active an Item in a listview. 当您在列表视图中激活一个项目时,我会触发一个事件。

I want to call this event from another place (a timer in this case but this shouldn't change much). 我想从另一个地方调用此事件(在这种情况下为计时器,但这应该不会有太大变化)。

My Sub: 我的子:

Public Sub AccountChecker_AccountList_ItemActivate(sender As Object, e As EventArgs) Handles AccountChecker_AccountList.ItemActivate
    [...My Code]
End Sub

I want to have this execute even if I did not activated the item. 即使我没有激活该项目,我也要执行此操作。 However, I can find anything to "click" the listview item. 但是,我可以找到任何东西来“单击”列表视图项。

I tried with something like this: 我尝试过这样的事情:

AccountChecker_AccountList_ItemActivate(x,y)

I probably need x and y to do this. 我可能需要x和y来执行此操作。 What are the object and EventArgs in this function so i can call it? 这个函数中的对象和EventArgs是什么,所以我可以调用它?

regards, 问候,

If i understand you correctly you cannot trigger the sub that is called by the eventhandler for the itemactivate-event. 如果我正确理解您的信息,则无法触发事件处理程序为itemactivate-event调用的子项。 You could just write a function or a sub that does what you need. 您可以编写满足您需要的函数或子程序。 If you need the name of the item that is activated, you can just pass it as an argument into your sub / function. 如果您需要激活的项目的名称,则可以将其作为参数传递给您的子/函数。 You can call it when the event is fired or when the timer is where you want it to be. 您可以在事件触发时或计时器位于您希望的位置时调用它。 You can add parameters to your function to extend it. 您可以向函数添加参数以对其进行扩展。 That also makes it much easier to reuse your code :) 这也使重用代码变得更加容易:)

 Private Sub BTN_Run_Click(sender As Object, e As EventArgs) Handles BTN_Run.Click

    ListViewAction(ListViewItem)

End Sub

public sub ListViewAction(byval ListviewItem as string)
' do your stuff here 
end sub 

The sender is usually the object calling the method, so you can use Me . sender通常是调用该方法的对象,因此您可以使用Me You can use EventArgs.Empty for e , ie 您可以将EventArgs.Empty用于e ,即

AccountChecker_AccountList_ItemActivate(Me, EventArgs.Empty)

However, it may depend on what you actually do with sender in [...My Code] - some handlers may expect it to be a specific type and may not like being passed a random object! 但是,这可能取决于您在[...My Code]sender实际处理方式-一些处理程序可能期望它为特定类型,并且可能不喜欢传递给随机对象!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM