使进度栏从公共班级运行 [英] Make Progress Bar Running From Public Class

查看:74
本文介绍了使进度栏从公共班级运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个带有进度栏和按钮之类的对象的表单。
我还已经在表单外部创建了公共库代码

I have created a Form with Objects like Progressbar and Button. I have also created Public library code outside my Form

我想从编写的 Sub 中修改进度条控件或按钮的文本一个库(我尝试将表单作为参数传递):

I want to modify the Progressbar Control or the Button's Text from a Sub that is written in a library (I try to pass my Form as a parameter):

Public Shared Sub ModifyItems(ByRef _WhichForm As Form)
   _WhichForm.MyProgressBar1.visible = True
End sub

不幸的是,代码无法识别进度栏名称 MyProgressBar1

Unfortunately, the code Is not recognizing the Progressbar name MyProgressBar1

是否可以直接在 Sub Function 是在类库中编写的,而不是直接在表单代码中编写的?

Is there a way to modify the Progressbar Control on the Form directly in a Sub or Function that is written in a class library, not directly in the Form Code ?

推荐答案

这种方法通常会落空在坏习惯的保护下让对象互相修改成员会直接引入紧密的耦合,从而使代码难以扩展,调试和维护。

This type of approach would generally fall under the umbrella of "bad practice". Having objects modifying each others members directly introduces tight coupling that makes for difficult code to extend, debug, and maintain.

与其尝试从库中修改某些内容,不如从通知库中的某些内容发生更改方面进行思考。例如,您可以在库中引发一个事件。然后, Form1 可以侦听该事件并对其适当的组件进行任何更改。这样, Form1 只负责修改其组件,而库只负责宣布对其内部状态的更改。

Rather than trying to modify something from within the library, better to think in terms of notifying that something within the library has changed. You could, for example, raise an event within the library. Form1 could then listen for that event and make any changes to its components that are appropriate. This way Form1 is solely responsible for modifying its components and the library is solely responsible for announcing changes to its internal state.

举一个很好的例子-例如,如果您要更改表单的进度条组件(也许您找到了更好的组件),您会突然引入一项重大更改进入您的图书馆。

To give a good example - if, say, you were to change your Form's progress bar component (maybe you find a better one out there) you suddenly introduce a breaking change into your library.

要使用某个事件,您可以执行以下操作:

To use an event you might do something like :

Public Class MyLibrary
    Event OnSomethingHappened()

    Private Sub SomethingHappened()
       RaiseEvent OnSomethingHappened()
    End Sub
End Class

然后以您的形式:

Public Class Form1
    Private WithEvents _myLibrary as New MyLibrary

    Private Sub LibraryDidSomething() Handles _myLibrary.OnSomethingHappened
        MyProgressBar1.Visible = True
    End Sub
End Class

这很好地消除了对 Form1 -该库作为一个独立的实体存在,并且不关心谁听其事件。

This nicely decouples any dependence on Form1 - the library exists as a self-contained entity and doesn't care who listens to its events.

如果要使用 shared 共享类来执行此操作,还可以使用共享事件-这些必须以编程方式连接as:

If you want to do this with a shared class you can also use shared events - these must be connected programmatically as :

Public Class MyLibrary
    Shared Event OnSomething()

    Public Shared Sub DoSomething()
        RaiseEvent OnSomething()
    End Sub
End Class

形式:

Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) _
                                                        Handles MyBase.Load
       AddHandler MyLibrary.OnSomethingHappened, AddressOf LibDidSomething
    End Sub

    Private Sub Form1_FormClosed(sender As System.Object, e As _
        System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
       RemoveHandler MyLibrary.OnSomethingHappened, AddressOf LibDidSomething
    End Sub

    Private Sub LibDidSomething() 
        MyProgressBar1.Visible = True
    End Sub
End Class

以编程方式添加事件时,在处置已订阅事件的对象之前,必须小心删除它们。在这种情况下,我在窗体加载时添加了处理程序,并在窗体关闭时将其删除。如果无法删除添加的处理程序,则将不会垃圾回收该表单,这将导致内存泄漏。

When programmatically adding events you must take care to remove them before disposing of the objects that have subscribed to them. In this case I have added the handler when the form loads and have removed it when the form closes. If you fail to remove an added handler then the form would not be garbage collected and this would cause a memory leak.

请参阅此处以获取更多信息:引发事件并响应事件(MSDN)

See here for more reading : Raising Events and Responding to Events (MSDN)

这篇关于使进度栏从公共班级运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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