-
Control array in VB.NET?
Hi all!
Yes, I know VB.NET doesn't support CA's as such, and I realize thera are
probably 10 different ways to implement them. Anyone done it? Like to share
it? Mainly, I'm a bit foggy about how to direct all events from a collection
of dynamically created controls to one event-handler
Any suggestions appreciated!
/Martin
-
Re: Control array in VB.NET?
In article <3b8f33cc$1@news.devx.com> (from martin rydman
<martin@aprire.se>),
>
> Hi all!
>
> Yes, I know VB.NET doesn't support CA's as such, and I realize thera are
> probably 10 different ways to implement them. Anyone done it? Like to share
> it? Mainly, I'm a bit foggy about how to direct all events from a collection
> of dynamically created controls to one event-handler
As you create the controls, use the AddHandler statement to hook up
their events:
dim oButton as Button
oButton = New Button()
oButton.Text = "Button 1"
AddHandler oButton.OnClick, AddressOf ClickIt
....
oButton = New Button()
oButton.Text = "Button 2"
AddHandler oButton.OnClick, AddressOf ClickIt
....
oButton = New Button()
oButton.Text = "Button 3"
AddHandler oButton.OnClick, AddressOf ClickIt
--
Patrick Steele
-
Re: Control array in VB.NET?
Hi Patrick!
Thanks. Cool. Just to get it stright in my mind...
ClickIt would get the sender and eventargs, just as usual, right? That would
mean that the sender references the individual object of the array?
It would also mean that I should duplicate the signature from the event that
I like to implement, right?
TIA
/Martin
Patrick Steele <psteele@ipdsolution.com_> wrote:
>In article <3b8f33cc$1@news.devx.com> (from martin rydman
><martin@aprire.se>),
>>
>> Hi all!
>>
>> Yes, I know VB.NET doesn't support CA's as such, and I realize thera are
>> probably 10 different ways to implement them. Anyone done it? Like to
share
>> it? Mainly, I'm a bit foggy about how to direct all events from a collection
>> of dynamically created controls to one event-handler
>
>As you create the controls, use the AddHandler statement to hook up
>their events:
>
>dim oButton as Button
>
>oButton = New Button()
>oButton.Text = "Button 1"
>AddHandler oButton.OnClick, AddressOf ClickIt
>....
>oButton = New Button()
>oButton.Text = "Button 2"
>AddHandler oButton.OnClick, AddressOf ClickIt
>....
>oButton = New Button()
>oButton.Text = "Button 3"
>AddHandler oButton.OnClick, AddressOf ClickIt
>
>--
>Patrick Steele
-
Re: Control array in VB.NET?
In article <3b8f7f9d$1@news.devx.com> (from martin rydman
<martin@aprire.se>),
>
> Hi Patrick!
>
> Thanks. Cool. Just to get it stright in my mind...
> ClickIt would get the sender and eventargs, just as usual, right? That would
> mean that the sender references the individual object of the array?
> It would also mean that I should duplicate the signature from the event that
> I like to implement, right?
Yes to all. The signatures of the event handler (Clickit) needs to
match the signature of the event (OnClick).
Also, if you do have all of your "buttons" in an ArrayList, you can
simulate the classic VB control array and get an integer index by using
the "IndexOf" method on the ArrayList:
dim my_buttons as New ArrayList()
..
..
..
Sub Clickit(Byval sender as Object, Byval e as EventArgs)
dim idx As Integer = my_buttons.IndexOf(sender)
...
end sub
--
Patrick Steele
-
Re: Control array in VB.NET?
Thanks Patrick, I tried it and it works just fine
/Martin
Patrick Steele <psteele@ipdsolution.com_> wrote:
>In article <3b8f7f9d$1@news.devx.com> (from martin rydman
><martin@aprire.se>),
>>
>> Hi Patrick!
>>
>> Thanks. Cool. Just to get it stright in my mind...
>> ClickIt would get the sender and eventargs, just as usual, right? That
would
>> mean that the sender references the individual object of the array?
>> It would also mean that I should duplicate the signature from the event
that
>> I like to implement, right?
>
>Yes to all. The signatures of the event handler (Clickit) needs to
>match the signature of the event (OnClick).
>
>Also, if you do have all of your "buttons" in an ArrayList, you can
>simulate the classic VB control array and get an integer index by using
>the "IndexOf" method on the ArrayList:
>
>dim my_buttons as New ArrayList()
>..
>..
>..
>Sub Clickit(Byval sender as Object, Byval e as EventArgs)
> dim idx As Integer = my_buttons.IndexOf(sender)
> ...
>end sub
>
>--
>Patrick Steele
-
Re: Control array in VB.NET?
Martin,
In addition to Patrick's suggestion. Which may be easier...
You can also manually add the events on the end of the 'Handles' clause of
the event handler Sub statement...
Something like:
Private Sub Button1_Click(ByVal sender as System.Object, ByVal e as
System.EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click
End Sub
I used it to route Form.Load & a Menu.Click event to the same handler!
Hope this helps
Jay
"martin rydman" <martin@aprire.se> wrote in message
news:3b8f33cc$1@news.devx.com...
>
> Hi all!
>
> Yes, I know VB.NET doesn't support CA's as such, and I realize thera are
> probably 10 different ways to implement them. Anyone done it? Like to
share
> it? Mainly, I'm a bit foggy about how to direct all events from a
collection
> of dynamically created controls to one event-handler
>
> Any suggestions appreciated!
>
> /Martin
-
Re: Control array in VB.NET?
Hi Jay!
Thanks for the tip!
/Martin
-
Re: Control array in VB.NET?
In article <3b943746@news.devx.com> (from Jay B. Harlow
<Jay_Harlow@email.msn.com>),
> You can also manually add the events on the end of the 'Handles' clause of
> the event handler Sub statement...
Yes, for controls created at design time. Martin's original question
mentioned "dynamically created controls" -- which I interpreted to mean
created at run-time.
--
Patrick Steele
-
Re: Control array in VB.NET?
Patrick,
I knew I missed part of the conversation ;-)
Question: If I call AddHandler, do I have to call RemoveHandler? Assuming
the form itself is going to be around for a while (GC considers it
reachable). but the control has been discarded (GC considers it
unreachable)? Does the GC consider the control reachable if I still have a
reference to an event handler?
Thanks
Jay
"Patrick Steele" <psteele@ipdsolution.com_> wrote in message
news:MPG.15fea6f55e0605e198983f@news.devx.com...
> In article <3b943746@news.devx.com> (from Jay B. Harlow
> <Jay_Harlow@email.msn.com>),
> > You can also manually add the events on the end of the 'Handles' clause
of
> > the event handler Sub statement...
>
> Yes, for controls created at design time. Martin's original question
> mentioned "dynamically created controls" -- which I interpreted to mean
> created at run-time.
>
> --
> Patrick Steele
-
Re: Control array in VB.NET?
In article <3b951dbb@news.devx.com> (from Jay B. Harlow
<Jay_Harlow@email.msn.com>),
> Question: If I call AddHandler, do I have to call RemoveHandler?
It can't hurt. 
> Assuming
> the form itself is going to be around for a while (GC considers it
> reachable). but the control has been discarded (GC considers it
> unreachable)? Does the GC consider the control reachable if I still have a
> reference to an event handler?
I'm not sure on that one. It sounds like something I've read before
(assigning event handlers and the GC), but I couldn't find any info on
it when searching around.
If I find anything, I'll post it.
--
Patrick Steele
-
Re: Control array in VB.NET?
Jay,
> Question: If I call AddHandler, do I have to call RemoveHandler? Assuming
> the form itself is going to be around for a while (GC considers it
> reachable). but the control has been discarded (GC considers it
> unreachable)? Does the GC consider the control reachable if I still have a
> reference to an event handler?
No, the reference is the other way around so you don't need to call
RemoveHandler. The control has a reference to the form (indirectly via the
delegate that AddHandler creates). But the form does not maintain a
reference to the control (after it's removed from the form's Controls
collection).
What you should do however, after removing a control from a form, is Dispose
it.
--
David.
-
Re: Control array in VB.NET?
In article <3b951dbb@news.devx.com> (from Jay B. Harlow
<Jay_Harlow@email.msn.com>),
> If I call AddHandler, do I have to call RemoveHandler?
Yes, you should. In this case (a WinForms app) it's not quite as
critical, but I now remember this from Applemans "Moving To VB.NET".
His example was suppose you create a bunch of client objects that attach
to an event on the server. At some point in time, you're done with
those clients and simply set them all to Nothing (skipping the
RemoveHandler). Are they available for GC? No -- the server is still
holding on to the event delegate from the clients since you didn't use
RemoveHandler.
--
Patrick Steele
-
Re: Control array in VB.NET?
David,
Does Disposing a control, cause the Delegate to be also Disposed?
How is this handled in a class of my own design? Not a Control or
Component...
See my notes to Patrick.
Thanks for the info!
Jay
"David Bayley" <dbayley@spamless.aebacus.com> wrote in message
news:3b9614b4@news.devx.com...
> Jay,
>
> > Question: If I call AddHandler, do I have to call RemoveHandler?
Assuming
> > the form itself is going to be around for a while (GC considers it
> > reachable). but the control has been discarded (GC considers it
> > unreachable)? Does the GC consider the control reachable if I still have
a
> > reference to an event handler?
>
> No, the reference is the other way around so you don't need to call
> RemoveHandler. The control has a reference to the form (indirectly via
the
> delegate that AddHandler creates). But the form does not maintain a
> reference to the control (after it's removed from the form's Controls
> collection).
>
> What you should do however, after removing a control from a form, is
Dispose
> it.
>
> --
> David.
>
>
>
-
Re: Control array in VB.NET?
Patrick,
Interesting that you and David stated opposite statements! ;-)
I guess it really depends: Does a Delegate hold a regular reference to the
object, or a WeakReference to the object?
I tend to believe you are correct, either way I am leaning toward calling
RemoveHandler as it seems to be the more resource friendly ('correct') way
of doing it.
The place I am considering using it is basically as you stated, I create/add
objects to a custom Collection class. Each item added, I plan on doing a
AddHandler. In the Remove method, I was planning on doing a RemoveHandler.
The Collection class would then raise the events to its owner/parent.
Something like Workbook.SheetActivate & Worksheet.Activate events in Excel.
Thanks for the info!
Jay
"Patrick Steele" <psteele@ipdsolution.com_> wrote in message
news:MPG.15ffe9908d417fdd989843@news.devx.com...
> In article <3b951dbb@news.devx.com> (from Jay B. Harlow
> <Jay_Harlow@email.msn.com>),
> > If I call AddHandler, do I have to call RemoveHandler?
>
> Yes, you should. In this case (a WinForms app) it's not quite as
> critical, but I now remember this from Applemans "Moving To VB.NET".
>
> His example was suppose you create a bunch of client objects that attach
> to an event on the server. At some point in time, you're done with
> those clients and simply set them all to Nothing (skipping the
> RemoveHandler). Are they available for GC? No -- the server is still
> holding on to the event delegate from the clients since you didn't use
> RemoveHandler.
>
> --
> Patrick Steele
-
Re: Control array in VB.NET?
Hmm...
Funny how reading something after sending it, changes one's thought on the
matter...
Now I am wondering if David is correct, as the Delegate has the reference,
and the Delegate is part of the object that no longer has a reference to
it...
I think I will have to contrive a test later just to prove it to myself! ;-)
Thanks
Jay
"Jay B. Harlow" <Jay_Harlow@email.msn.com> wrote in message
news:3b96b8f0@news.devx.com...
> Patrick,
> Interesting that you and David stated opposite statements! ;-)
>
> I guess it really depends: Does a Delegate hold a regular reference to the
> object, or a WeakReference to the object?
>
> I tend to believe you are correct, either way I am leaning toward calling
> RemoveHandler as it seems to be the more resource friendly ('correct') way
> of doing it.
>
> The place I am considering using it is basically as you stated, I
create/add
> objects to a custom Collection class. Each item added, I plan on doing a
> AddHandler. In the Remove method, I was planning on doing a RemoveHandler.
> The Collection class would then raise the events to its owner/parent.
> Something like Workbook.SheetActivate & Worksheet.Activate events in
Excel.
>
> Thanks for the info!
> Jay
>
> "Patrick Steele" <psteele@ipdsolution.com_> wrote in message
> news:MPG.15ffe9908d417fdd989843@news.devx.com...
> > In article <3b951dbb@news.devx.com> (from Jay B. Harlow
> > <Jay_Harlow@email.msn.com>),
> > > If I call AddHandler, do I have to call RemoveHandler?
> >
> > Yes, you should. In this case (a WinForms app) it's not quite as
> > critical, but I now remember this from Applemans "Moving To VB.NET".
> >
> > His example was suppose you create a bunch of client objects that attach
> > to an event on the server. At some point in time, you're done with
> > those clients and simply set them all to Nothing (skipping the
> > RemoveHandler). Are they available for GC? No -- the server is still
> > holding on to the event delegate from the clients since you didn't use
> > RemoveHandler.
> >
> > --
> > Patrick Steele
>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks