From: Roidy on 25 May 2010 14:08 OK bear with me while I try to explain :) I'm writing a program where I need to do a lot of different background worker tasks and display the progress in a dialog box. So rather than create a separate dialog for each task I created a class that displays a dialog and fires off a background worker, what I can't seem to do is pass a dowork handler sub into the class. This is basically what I've got so far:- MainForm code:- ........ Dim pBox as New ProgressBox("Test caption") pBox.ShowDialog() Sub worksub() ......... Do background work here ......... End Sub ........ Class code:- Public Class ProgressBox Public Sub New(ByVal txt as String) InitProgBox() Me.Label1.text = txt Me.backgroundworker1.runWorkerAsync() End Sub Public Sub InitProgBox() .......... .......... Code to set up the dialog and background worker .......... .......... End Sub End Class I can happily pass string and other variables in to the class but when I create a new instance of ProgressBox I need to be able to pass worksub into it and have it added as the handler of the backgroundworkers DoWork function. I just can't figure out how to pass worksub into the class along the lines of:- Dim pBox as New ProgressBox("Test caption", worksub) Help please Robert
From: Tom Shelton on 25 May 2010 14:17 Roidy formulated on Tuesday : > OK bear with me while I try to explain :) > > I'm writing a program where I need to do a lot of different background worker > tasks and display the progress in a dialog box. So rather than create a > separate dialog for each task I created a class that displays a dialog and > fires off a background worker, what I can't seem to do is pass a dowork > handler sub into the class. This is basically what I've got so far:- > > MainForm code:- > > ....... > Dim pBox as New ProgressBox("Test caption") > pBox.ShowDialog() > > Sub worksub() > ......... > Do background work here > ......... > End Sub > ....... > > Class code:- > > Public Class ProgressBox > Public Sub New(ByVal txt as String) > InitProgBox() > Me.Label1.text = txt > Me.backgroundworker1.runWorkerAsync() > End Sub > > Public Sub InitProgBox() > .......... > .......... > Code to set up the dialog and background worker > .......... > .......... > End Sub > End Class > > I can happily pass string and other variables in to the class but when I > create a new instance of ProgressBox I need to be able to pass worksub into > it and have it added as the handler of the backgroundworkers DoWork function. > I just can't figure out how to pass worksub into the class along the lines > of:- > > Dim pBox as New ProgressBox("Test caption", worksub) > Dim pBox As New Progressbox ("Test caption", AddressOf workSub) HTH -- Tom Shelton
From: Roidy on 25 May 2010 14:36 Thanks Tom, but how do I then get the sub in the class and add it as a handler? What type will it be? Public Class ProgressBox Public Sub New(ByVal txt as String, ???????????????<- How to get it) InitProgBox() AddHandler backgroundWorker1.DoWork, ?????????????????????? Me.Label1.text = txt Me.backgroundworker1.runWorkerAsync() End Sub End Sub End Class Thanks rob > > Dim pBox As New Progressbox ("Test caption", AddressOf workSub) > > HTH > > -- > Tom Shelton > >
From: Tom Shelton on 25 May 2010 14:44 Roidy formulated on Tuesday : > Thanks Tom, but how do I then get the sub in the class and add it as a > handler? What type will it be? > > Public Class ProgressBox > Public Sub New(ByVal txt as String, ???????????????<- How to get it) > InitProgBox() > > AddHandler backgroundWorker1.DoWork, ?????????????????????? > > Me.Label1.text = txt > Me.backgroundworker1.runWorkerAsync() > End Sub > End Sub > End Class > > Thanks > rob > >> >> Dim pBox As New Progressbox ("Test caption", AddressOf workSub) >> >> HTH >> >> -- Tom Shelton >> >> In the class that is creating going to acutally host the event... Private Sub DoWork (ByVal sender As Object, ByVal e As DoWorkEventArgs) ' do cool stuff End Sub ''' declaring an instance in the class with the sub Dim pBox As New ProgressBox("Test Caption", AddressOf DoWork) '' in the progressBox Class Constructor Public Sub New(ByVal caption As String, ByVal workSub As DoWorkEventHandler) ' do init ' add the handler AddHandler _backgroundWorker.DoWork, workSub End Sub HTH -- Tom Shelton
From: Roidy on 25 May 2010 15:28 Thanks Tom that did it. Works perfectly. Robert "Tom Shelton" <tom_shelton(a)comcast.invalid> wrote in message news:hth5r3$jo3$1(a)news.eternal-september.org... > > In the class that is creating going to acutally host the event... > > > Private Sub DoWork (ByVal sender As Object, ByVal e As DoWorkEventArgs) > ' do cool stuff > End Sub > > > ''' declaring an instance in the class with the sub > Dim pBox As New ProgressBox("Test Caption", AddressOf DoWork) > > '' in the progressBox Class Constructor > Public Sub New(ByVal caption As String, ByVal workSub As > DoWorkEventHandler) > ' do init > ' add the handler > AddHandler _backgroundWorker.DoWork, workSub > End Sub > > HTH > > -- > Tom Shelton > >
|
Pages: 1 Prev: How do I get back deleted Tab Page Next: Maximum index on a list is less than the list size |