.NET Delegate [modified]
-
I have a few questions about the .NET Delegate. It seems like the delegate is consumed to change a value on the delegate's source form. My problem is, is that I need to change about 50 values (set them to blank). This (setting up a delegate) doesn't seem like the way to go about consuming an event that just merely changes textboxes to be blank. I don't need to return any value from form 1, but just fire the event. Christian Graus: i've seen you reply to a few people's questions with the simple line 'use a delegate' or something to that effect. Just wondering what your take is on this situation. Nathan Lindley .NET Aficionado -- modified at 11:16 Wednesday 4th October, 2006
-
I have a few questions about the .NET Delegate. It seems like the delegate is consumed to change a value on the delegate's source form. My problem is, is that I need to change about 50 values (set them to blank). This (setting up a delegate) doesn't seem like the way to go about consuming an event that just merely changes textboxes to be blank. I don't need to return any value from form 1, but just fire the event. Christian Graus: i've seen you reply to a few people's questions with the simple line 'use a delegate' or something to that effect. Just wondering what your take is on this situation. Nathan Lindley .NET Aficionado -- modified at 11:16 Wednesday 4th October, 2006
OK. What are you trying to do with this? Are you just trying to clear a form from a seperate class?? If so, then it's bad practice to do it from your class. If your class knew anything about the form it's trying to modify, it would then be permanently tied to that form and not be able to be reused somewhere else. What you should be doing is sending back data to the form that it knows should signal that some processing is done. This signal can then be used as a trigger so the form clears its own textbox's. This is either done with a return value from your class' method, or from an event or callback delegate that your form subscribes to. With this model, your class can be reused with any UI, be it a Compact Framework app, ASP.NET app, Web Service, ...
Dave Kreskowiak Microsoft MVP - Visual Basic
-
OK. What are you trying to do with this? Are you just trying to clear a form from a seperate class?? If so, then it's bad practice to do it from your class. If your class knew anything about the form it's trying to modify, it would then be permanently tied to that form and not be able to be reused somewhere else. What you should be doing is sending back data to the form that it knows should signal that some processing is done. This signal can then be used as a trigger so the form clears its own textbox's. This is either done with a return value from your class' method, or from an event or callback delegate that your form subscribes to. With this model, your class can be reused with any UI, be it a Compact Framework app, ASP.NET app, Web Service, ...
Dave Kreskowiak Microsoft MVP - Visual Basic
Thanks for the response Dave :-D
Dave Kreskowiak wrote:
an event or callback delegate that your form subscribes to
I've been trying to work with the delegate again all morning, and i'm just missing the connection (in my brain) somewhere. Heres what I have... Form1(Modify2.vb) Create the delegate: Public Delegate Sub ClearDel(ByVal clear As Boolean) Create the event: Private evtClear As ClearDel Public Sub Register(ByVal objClear As ClearDel) 'raise event here? End Sub Public Sub fireClear() 'clearing code End Sub Form2(frmPayableInfo) Private WithEvents mobjModify2 As Modify2 In the sub where I process, at the end i have this: mobjModify2 = New Modify2 mobjModify2.Register(New Modify2.ClearDel(AddressOf eventClear) and then i have the sub: Public Sub eventClear(ByVal clear As Boolean) clear = True End Sub So now the question is, how do I hook up the fireClear() event on Form1(modify2.vb) with the delegate. And where would I set the condition for the delegate to fire? Thanks again for the response. Nathan Lindley .NET Aficionado
-
Thanks for the response Dave :-D
Dave Kreskowiak wrote:
an event or callback delegate that your form subscribes to
I've been trying to work with the delegate again all morning, and i'm just missing the connection (in my brain) somewhere. Heres what I have... Form1(Modify2.vb) Create the delegate: Public Delegate Sub ClearDel(ByVal clear As Boolean) Create the event: Private evtClear As ClearDel Public Sub Register(ByVal objClear As ClearDel) 'raise event here? End Sub Public Sub fireClear() 'clearing code End Sub Form2(frmPayableInfo) Private WithEvents mobjModify2 As Modify2 In the sub where I process, at the end i have this: mobjModify2 = New Modify2 mobjModify2.Register(New Modify2.ClearDel(AddressOf eventClear) and then i have the sub: Public Sub eventClear(ByVal clear As Boolean) clear = True End Sub So now the question is, how do I hook up the fireClear() event on Form1(modify2.vb) with the delegate. And where would I set the condition for the delegate to fire? Thanks again for the response. Nathan Lindley .NET Aficionado
Wow! You've got everything backwards and in the wrong classes. First of all, get off the delegate kick and start explaining what you're trying to do. Since you're using two forms, the circumstances have now changed. Does Form1 show Form2 as a dialog and wait for the user to close Form2? If so, then all you have to do in Form2 is expose the data as a few properties and resume execution from after the ShowDialog call:
' In Form1 Dim myForm2 As New Form2() ' ShowDialog is a blocking call, so nothing after it will execute ' until the dialog is dismissed by the user. If myForm2.ShowDialog() = DialogResult.OK Then ' Get the data off of Form2 and do something it... Dim x As Integer = myForm2.someProperty ' Clear the textbox controls... ClearMyControls() End If ' When using ShowDialog(), you MUST call Dispose on the object ' when you're done with it! myForm2.Dispose()
Now, if you're running both forms simultaneously, you can expose some events in Form2 that Form1 can subscribe to:
' In Form2 Public Event SomethingHappened(ByVal someValue As Integer)
Public Sub SomeMethod()
' Do something then raise an event to say it was done.
RaiseEvent SomethingHappened(data)
End Sub
' In Form1
Dim myForm2 As New Form2()
AddHandler myForm2.SomethingHappened, AddressOf MyHandler
.
.
' Must match the signature of the SomethingHappened event!
Private Sub MyHandler(ByVal value As Integer)
' We were told something happened. Clear our controls!
ClearMyControls()
End SubDave Kreskowiak Microsoft MVP - Visual Basic
-
Wow! You've got everything backwards and in the wrong classes. First of all, get off the delegate kick and start explaining what you're trying to do. Since you're using two forms, the circumstances have now changed. Does Form1 show Form2 as a dialog and wait for the user to close Form2? If so, then all you have to do in Form2 is expose the data as a few properties and resume execution from after the ShowDialog call:
' In Form1 Dim myForm2 As New Form2() ' ShowDialog is a blocking call, so nothing after it will execute ' until the dialog is dismissed by the user. If myForm2.ShowDialog() = DialogResult.OK Then ' Get the data off of Form2 and do something it... Dim x As Integer = myForm2.someProperty ' Clear the textbox controls... ClearMyControls() End If ' When using ShowDialog(), you MUST call Dispose on the object ' when you're done with it! myForm2.Dispose()
Now, if you're running both forms simultaneously, you can expose some events in Form2 that Form1 can subscribe to:
' In Form2 Public Event SomethingHappened(ByVal someValue As Integer)
Public Sub SomeMethod()
' Do something then raise an event to say it was done.
RaiseEvent SomethingHappened(data)
End Sub
' In Form1
Dim myForm2 As New Form2()
AddHandler myForm2.SomethingHappened, AddressOf MyHandler
.
.
' Must match the signature of the SomethingHappened event!
Private Sub MyHandler(ByVal value As Integer)
' We were told something happened. Clear our controls!
ClearMyControls()
End SubDave Kreskowiak Microsoft MVP - Visual Basic
Thanks for the response Dave K :-D Like I said I am brand new to the whole delegate concept so I have no idea how they work. First let me just explain my situation. I have a form that displays a datagrid with customer info. when you double click the start of the row, it opens up a form (form2) that has a bunch of information based on the record that was clicked. When I submit this information (form2) I need to clear the datagrid (on form1) so that the user has to look up the ID of the person they updated (i probably should just reset the find, but thats another issue i can handle when i get this cross form communicating going), so there isn't old data. Okay. Now in your example:
Dave Kreskowiak wrote:
' In Form2 Public Event SomethingHappened(ByVal someValue As Integer) Public Sub SomeMethod() ' Do something then raise an event to say it was done. RaiseEvent SomethingHappened(data) End Sub
What significance does the Integer in the argument have? Nathan Lindley
-
Thanks for the response Dave K :-D Like I said I am brand new to the whole delegate concept so I have no idea how they work. First let me just explain my situation. I have a form that displays a datagrid with customer info. when you double click the start of the row, it opens up a form (form2) that has a bunch of information based on the record that was clicked. When I submit this information (form2) I need to clear the datagrid (on form1) so that the user has to look up the ID of the person they updated (i probably should just reset the find, but thats another issue i can handle when i get this cross form communicating going), so there isn't old data. Okay. Now in your example:
Dave Kreskowiak wrote:
' In Form2 Public Event SomethingHappened(ByVal someValue As Integer) Public Sub SomeMethod() ' Do something then raise an event to say it was done. RaiseEvent SomethingHappened(data) End Sub
What significance does the Integer in the argument have? Nathan Lindley
nlindley7 wrote:
What significance does the Integer in the argument have?
Nothing. It's just an example of how to pass data in an event. If what you're doing in your datagrid is editing the details of that record and sending them back to the database, then you would probably show this detail form with ShowDialog(). After that statement returns (the user has finsihed editing this record and it was written back to the database), you can execute the code to refresh your datagrid's datasource.
Dave Kreskowiak Microsoft MVP - Visual Basic
-
nlindley7 wrote:
What significance does the Integer in the argument have?
Nothing. It's just an example of how to pass data in an event. If what you're doing in your datagrid is editing the details of that record and sending them back to the database, then you would probably show this detail form with ShowDialog(). After that statement returns (the user has finsihed editing this record and it was written back to the database), you can execute the code to refresh your datagrid's datasource.
Dave Kreskowiak Microsoft MVP - Visual Basic
Hey Dave TA :-D, The datagrid displays a list of claims for each client. When an individual claim is clicked, a new form pops up that displays the details of that particular claim. When you edit/submit the details of this form (form 2), the values are submittied to a claimsDetails table. One of the details you can edit, happens to be the primary key of the claims/claimDetails tables. So the form submits data to the database that the datagrid reads from; hence needing to refresh that table. So I would assume the second example you showed is the one to follow? Nathan Lindley
-
Hey Dave TA :-D, The datagrid displays a list of claims for each client. When an individual claim is clicked, a new form pops up that displays the details of that particular claim. When you edit/submit the details of this form (form 2), the values are submittied to a claimsDetails table. One of the details you can edit, happens to be the primary key of the claims/claimDetails tables. So the form submits data to the database that the datagrid reads from; hence needing to refresh that table. So I would assume the second example you showed is the one to follow? Nathan Lindley
Actually, the best example to follow in this case would be the first one. After the details are edited in the second form, the original form has to refresh its dataset so it can display the new data. The second form doesn't need to stay up if it's not editing anything. What I meant by "running two forms simulataneously" is that the user can switch back and forth between the forms at any time, or they are modeless. A modal form is a dialog box. You can't continue with the rest of the application unless that dialog box is dismissed (OK or Cancel).
Dave Kreskowiak Microsoft MVP - Visual Basic