WithEvents/Handles Better That Remove/AddHandler

Feb 5, 2010

Is WithEvents & Handles usage preferable to RemoveHandler & AddHandler?

From Memory point of view (remove an added handler after utilization, etc.)

View 3 Replies


ADVERTISEMENT

VS 2008 - Remove Handler - Use "WithEvents" And "Handles"

Aug 30, 2009

I am having some trouble understanding the different aspects of Event Handling. Of course, I know how to use "WithEvents" and "Handles". However, I have created some interdependent Custom Cotrols. I will try to describe this, and then post some code.

Assume a Custom Control 'GroupedListHeader", an instance of which is created by, and added to the controls collection of another Custom Control, "GroupedListNode".

GroupedListHeader adds an Event handler "OnHeaderClick" During initialization which hanndles several different events. As part of the handling in "OnHeaderClick", GroupedListHeader raises one of several Events which GroupedListNode in turn handles, again by using "AddHandler" during initialization. And so on. The events can propegate up through a chain of sub-components of my final control. I understand that I am supposed to use "RemoveHandler" somewhere, at some point, when I use "AddHandler". This is where my questions arise.

A. In the cases where I have used "AddHandler" to handle a custom event sourced by a sub-component, I get the following Waring:

Quote:

Warning2The 'AddressOf' expression has no effect in this context because the method argument to 'AddressOf' requires a relaxed conversion to the delegate type of the event. Assign the 'AddressOf' expression to a variable, and use the variable to add or remove the method as the handler.C:Documents and SettingsSuper God-like UserMy DocumentsVisual Studio 2008ProjectsGroupedListControlTestAttributesNode ComponentsGroupedListNodeBaseII.vb27446GroupedListControl

I am not quite sure what this is about, other than it seems to relate to trying to Remove a handler which is referencing another Custom Event.

An Example (The two in red are the source of the Warning): 'Added during the Constructor Method, after "InitializeComponent and some other Drawing-related Stuff:

AddHandler moTGHeader.Expanded, AddressOf Me.OnExpand
AddHandler moTGHeader.Collapsed, AddressOf Me.OnCollapse

AddHandler moTGHeader.Selected, AddressOf Me.OnSelect

[CODE]...

I should note futher that the component indicated above further propegates these handled Events upward to the parent of this component, where again, the same issue exists. What is it that makes the two items in red different from the others that they would cause this warning? How critical is it that I use "RemoveHandler" in this context? These events should persist until the control goes out of scope. Also, this is just an example; at several other levels in the hierarchy, I am doing the same thing. My understanding is this can result in objects retaining references to the Handlers(?) or something like that.

View 4 Replies

Handles Clause Requires A WithEvents Variable

Mar 14, 2012

i'm a .net programmer, using .net framework 4, and i have a question about WithEvents clause. [code] Private Sub mylist_AddingNew(ByVal sender As Object, ByVal e As AddingNewEventArgs) Handles mylist.AddingNew..i get this error: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.I think the problem is my custom bindinglist class....but how can i solve this?

View 8 Replies

VS 2008 Handles Clause Requires A WithEvents Variable?

Dec 5, 2011

I get the error on this line on [ListBox1]

[code] Private Sub ListBox1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListBox1.SelectedIndexChanged

error

Quote:

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

View 8 Replies

Handles Clause Requires A WithEvents Variable Defined In The Containing Type?

Feb 23, 2010

I've just opened a project I was working on yesterday perfectly well only to find it's gone nuts. I'm getting this error ..."Handles clause requires a WithEvents variable defined in the containing type or one of its base types."... on each one of my Sub declarations!

View 2 Replies

Asp.net - Handles Clause Requires A WithEvents Variable Defined In The Containing Type Or One Of Its Base Types?

Oct 6, 2011

I get the error

Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

in the following code..

Public Sub selCurrentManuf_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles selCurrentManuf.SelectedIndexChanged
End Sub

The drop list to go with it is the following...

<asp:DropDownList
OnSelectedIndexChanged="selCurrentManuf_SelectedIndexChanged"
selectedvalue='<%#Container.DataItem("c1_manufidcurrent")%>'

[code]....

View 1 Replies

Handles Clause Requires A WithEvents Variable Defined In The Containing Type Or One Of Its Base Types?

Aug 23, 2009

I created a lable to handle DateTime = Now, It works fine. but when I moved it inside a FormView I Get the following Message. Handles clause requires a WithEvents variable defined in the containing type or one of its base types?

View 5 Replies

Error : Handles Clause Requires A WithEvents Variable Defined In The Containing Type Of One Of Its Base Types

Mar 25, 2010

I am in the processing of moving an app from VB to C# but some of the classes I will just be moving to VB dlls to access from the main C# app. In doing this I am trying to get the VB dlls to utilize the main C# class lib. In this C# lib I have an abstract clase called base process, it sets up a background worker and handles all the progress tracking, errors, and even reports an ETA. To use this in a derived class in C# you would just do this...

public class SomeLongProcess : Common.Multithreading.BaseProcess
{
public void start()

[code]....

The problem is that if I do the equivelant in VB I get an error "Handles clause requires a WithEvents variable defined in the containing type of one of its base types". Well I can't do a WithEvents in the base class because it is C# so..

View 3 Replies

VS 2010 : Handles Clause Requires A WithEvents Variable Defined In The Containing Type Or One Of Its Base Types

Aug 26, 2011

I am trying to follow this thread

[URL]

and I have it pretty close but have one error.

Quote:Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

That error is on both of these, in blue

Private Sub SpellChecker1_DeletedWord(ByVal sender As Object, ByVal e As NetSpell.SpellChecker.SpellingEventArgs) Handles SpellChecker1.DeletedWord
'save existing selecting

[code]....

I have added the reference, I have also added them to the toolbox. My dictionary is all set. What did I miss?

View 1 Replies

Error - Handles Clause Requires A WithEvents Variable Defined In The Containing Type Or One Of Its Base Types.D:Shipping

Nov 22, 2011

i am getting errors on last two event handlers. what did i do wrong?

Error - Handles clause requires a WithEvents variable defined in the containing type or one of its base types.D:Shipping ApplicationShipping ApplicationForm1.vb75136Shipping Application
Error - 'PrintDocument1' is not declared. It may be inaccessible due to its protection level.D:Shipping ApplicationShipping ApplicationForm1.vb1119Shipping Application

[code]....

View 2 Replies

VS 2008 Button_Click Error:Handles Clause Requires A WithEvents Variable Defined In The Containing Type Or One Of Its Base Types

May 11, 2009

Just upgraded a VS 2005 ASP.NET 2.0 website to VS 2008 ASP.NET 3.5. There was an error on the Sub Button_Click. It seemed to be a minor error, the website and the button worked just fine, as usual. What does the error mean?

Protected Sub btnDEreports_Click(ByVal
sender As

[code]...

Error 20 Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

View 3 Replies

Error In My Coding - Error Handles Clause Requires A WithEvents Variable Defined In The Containing Type

Aug 9, 2011

My coding, I got error: Error Handles clause requires a WithEvents variable defined in the containing type or one of its base types.

For information i am using visual studio 2005. Here i highlight my probleam with in my coding

Public Class Admin
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub

[CODE]...

View 1 Replies

Runtime Error "1 Handles Clause Requires A WithEvents Variable Defined" When Attempt To Run A Program

Feb 2, 2010

When I attempt to run a program I just finished coding I get the following message: "Error 1 Handles clause requires a WithEvents variable defined in the containing type or one of its base types." Looked it up online and still does not make sense to me. If I need to paste code or anything else let me know and I will do so ASAP.

View 8 Replies

VB Silverlight For Windows Phone "Handles Clause Requires A WithEvents Variable Defined In The Containing Type Or One Of Its Base Types"

May 16, 2012

when i am selecting form the xaml file a button and then tap event from the right (in order to set the tap event) it auto generates this sub :

[Code]....

View 1 Replies

.net - Friend WithEvents In VB Vs Private In C#

Dec 17, 2009

Who knows, why in vb.net WinForm projects the designer by default use the Friend WithEvents attributes and in C# - private ones.

By ex, in a form.designer.

.cs

private Label Label1;

.vb

Friend WithEvents Label1 as Label;

For WithEvents is more or less clear(for using Handles, apparently). But why Friend in VB and private in C#...

View 4 Replies

Adding The Withevents Variable?

Jul 11, 2011

i have the folowing co

For x = 0 To 7 Step 1
For i = 0 To 7 Step 1
Dim rectangleShape1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape() 'create a new object to assign to the array location
board(x, i) = New Microsoft.VisualBasic.PowerPacks.RectangleShape() 'create the new object in the array

[code].....

View 2 Replies

Clarification For WithEvents Handlers

Feb 11, 2010

When you declare an object 'WithEvents' and then utilize the "Handles" clause at the end of your event methods, how does Visual Basic manage a reference change for the object? In other words, if the withevents var is set to ObjectA, then later I switch it to ObjectB (or nothing), does Visual Basic automatically both remove those handler methods from ObjectA, and attach them to ObjectB? Or do I still have a ObjectA's events being handled behind the scenes?

I ask this because if instead you use AddHandler for an object, and then switch your reference, unless you removed the handler before switching, your original object is still handled (at least I'm pretty sure that's how it works).

View 13 Replies

Events - .NET: WithEvents Not Working?

Apr 21, 2010

I have the following classes: Public Class Email

[Code]...

If a create a wrong email lets say "email" the exception is correctly cached and a message is showed however is i input a valid email the event is not raised, the object is being created but no message is shown and no error or exception is thrown i suspect it has something to do with using "myemail = new Email(email)" but i have seen examples of using new with withevents with no problem.

View 2 Replies

Local Variable / WithEvents

May 17, 2010

Does anyone know why a local variable cannot be declared WithEvents?T

View 3 Replies

Multiple ChildForms With WithEvents?

Jun 9, 2011

I'm trying to create multiple instances of a child form. This form has an event that feeds a string variable back to the parent so I'm declaring it with WithEvents which is declared outside of the Sub - meaning multiple clicks will not open multiple forms.

another method of doing this while keeping the event?:

Dim WithEvents FRestoreDB As New FormRestoreDB()
Private Sub BtnOpenDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnOpenDB.Click

[Code].....

View 2 Replies

Set To Nothing(in Dispose()) All WithEvents Fields?

Feb 24, 2010

Is it necessary to set to Nothing(in Dispose()) all WithEvents fields?

Apparently Handles keyword adds handlers to such fields, but does not remove it until this field is not Nothing, and this can generate memory leaks?!.

This should be specially actual on cases like

class Foo
{
Private WithEvents _bar as Bar

[Code].....

View 1 Replies

VS 2005 Experiencing WithEvents?

Feb 24, 2010

Protected WithEvents gel As New TabPage

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TabControl1.TabPages.Add(gel)
gel.Text = ("Gel")

Private Sub Gel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gel.Click
MsgBox("test") So my problem is that if I click on the tabpage gel, nothing happens.

View 15 Replies

VS 2010 Threading WithEvents?

Sep 7, 2011

I have an application that performs a lot of busy work. All of the work is performed using a BackgroundWorker and the point of execution is the DoWork Event of the BackgroundWorker.On the UI thread, before the workload begins, I display a PictureBox that contains an animated GIF that illustrates progress; for the most part this works fine and the UI thread is not paused or slowed while the BackgroundWorker does its job. There is however, one instance where the UI thread is impacted by the work being done and it defeats the whole purpose of putting this work in a separate thread. In this application, the user must wait until the work completes before they can move to the next step, so my sole reason for moving this all to a BackgroundWorker was so that I could display a smooth, uninterrupted progress indicator on the UI thread while this work was being done, so that the user could see that something was happening.

The case where the work being done is impacting the UI thread (pausing/delaying the PictureBox animation) deals with a referenced COM object.

[Code]...

All of the COM object event handlers and subsequent functions are appropriately using Delegates, but I am not sure why the ProcessFiles method would cause the UI thread to hang while the work completes; I thought that was the whole purpose of the BackgroundWorker.

View 6 Replies

WithEvents For Array Of Objects?

Dec 14, 2010

I'd like to create an array of objects with events. Where do I need to put the "WithEvents"? Should it be placed in the ReDim line or the next one?I'm trying to use a "global" event, like objects of people that recieve a time-event to get older.

[Code]...

View 18 Replies

.net - What Is The Optimal Approach To A WithEvents Collection

Mar 5, 2010

What is the optimal approach to a WithEvents Collection - VB.NET?Have you any remarks on the code bellow (skipping the Nothing verifications)?The problem is when I obtain the LinkedListNode(Of Foo) in a For Each block I can set myNode.Value = something, and here is a handlers leak...

-Could I override the FooCollection's GetEnumerator in this case?
-No. :( cause NotInheritable Class LinkedListNode(Of T)
Class Foo

[code]....

View 3 Replies

Clear All WithEvents Fields Via One Command?

Oct 15, 2010

The vb.net "WithEvents" syntax is very useful, but if a WithEvents field which references a long-lived object is not nulled out, it will often constitute a memory leak.Is there any easy way for a Dispose routine to have vb.net automatically clear out all WithEvents fields and unsubscribe them?

I've figured out a nice way to wrap the creation of disposable fields so that they will be automatically Disposed when the containing object is disposed, without having to individually list such objects. Is there any way to take care of WithEvents fields without having to manually null them out in the Dispose routine?

EDIT:Since there was no response indicating a built-in way to do it without reflection, is there any built-in means of using reflection to determine whether a particular property is a VB automatic implementation of a WithEvents "field"? I know that a WithEvents "field" called "foo" is implemented as a property called "foo" and a field called "_foo", but that's such a common naming pattern that I wouldn't count on it. Is there some attribute that could be used to determine which properties need to be auto-nulled?

View 1 Replies

Serialization For A Class Declared As WithEvents

May 9, 2009

Public WithEvents Tree as Tree. I am not able to Serialize the Tree class, but if i remove the "WithEvents" keyword, it works fine. Why? What should I do if I have to declare it with "WithEvents"?

View 2 Replies

Use WithEvents When Wav File Stops Playing?

Sep 27, 2009

I have done an extensive search before posting this question, using many different search parameters without success at finding an answer.Using Visual Studio 2008 Express VB:My program requires a notification when a wav file has finished playing, a media player song list will not suit my needs.I have used System.Media.SoundPlayer, Microsoft.DirectX.DirectSound and My.Computer.Audio.Play to play a wav file. All work well at playing and stopping the requested wav. So far, I have not found a methode to capture an event when the wav has ended. DirectSound stops playing when the form has lost focus, so I dont think that I can use it. Audio.Play AudioPlayMode.WaitToComplete only holds up other operations and I dont want to create multiple threads.

View 3 Replies

VS 2005 Using WithEvents With A Non-instantiated Class?

May 13, 2011

Can you raise events from a class that has only shared members and doesn't get instantiated? I originally had the class members non-shared and created instances of the class where I need them. I had the events being raised and everything worked as expected. But as the class members are actually application level and do not need instancing, I converted all members in the class to shared so they could be accessed from anywhere in the program without creating an instance of the class. But WithEvents requires the creation of a class instance. I've done that and it seems to work fine when using an instance. And since I need the events handled in only one class, I can create the instance in that class and let all other areas access the class directly, without creating an instance.

View 10 Replies

WithEvents And Changing Object Reference

May 3, 2011

So I wanted everyone's opinion on this and what you think of this. Using the WithEvents modifier makes life very easy in VB.Net (srsly, I love it), and I was exploring some of its limitations and noticed you can do something along the lines of this:

[Code]....

View 12 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved