Short question: can VB.NET operator AddressOf return Nothing in any case?
Not really, you always have to give the reference of a valid method in an AddressOf statement, or you'll have a compiler error.
Related
I am converting a C# program to VB,Net. The conversion created the following line:
Me.axWebBrowser1.DocumentCompleted += New Windows.Forms.WebBrowserDocumentCompletedEventHandler(AddressOf Me.axWebBrowser1_DocumentComplete)
This line creates the following error message:
Error 17 'Public Event DocumentCompleted(sender As Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.
I can not find whether there is a RaiseEvent statement in the Systems class which I can use to raise the event. I can not do it in my derived class.
You're not trying to raise the event, you're trying to subscribe to it.
In VB.NET this is done using an AddHandler statement:
AddHandler Me.axWebBrowser1.DocumentCompleted, AddressOf Me.axWebBrowser1_DocumentComplete
Also a statement isn't located inside a class, namespace or library. A statement is a line, or multiple lines, of code that goes together. So, not related to your problem, but you can read about the RaiseEvent statement.
I need to be able to pass the thread name into a subroutine in order to abort it when I need to.
So I have this so far:
Dim BrowserSpawn As New System.Threading.Thread(AddressOf BrowserSub)
BrowserSpawn.Start()
Private Async Sub BrowserSub(BrowserSpawn)
...
End Sub
Because the subroutine creates a browser within Form1 groups I needed to invoke access to these controls within the sub.
Note: This works fine when I'm not passing in the thread name.
If Me.GroupNamehere.InvokeRequired Then
Me.GroupNamehere.Invoke(New MethodInvoker(AddressOf BrowserSub))
Else
'Do nothing
End If
When I'm passing in the thread name these become a problem when trying to compile:
Method does not have a signature compatible with delegate 'Delegate Sub MethodInvoker()'.
I'm hoping this is just a syntax thing but I can't seem to get it to work. Is there any way I'm able to pass in this thread name without breaking my invokerequired check?
If I try and change it to the obvious:
Me.GroupNamehere.Invoke(New MethodInvoker(AddressOf BrowserSub(BrowserSpawn)))
It tells me Addressof operand must be the name of a method (without parentheses). Although without the parentheses it's not happy either so I don't know where to go from here.
/edit:
Stumbled across How can I create a new thread AddressOf a function with parameters in VB?
Which seems to confirm what I was trying passing something like:
Private Sub test2(Threadname As Thread)
' Do something
End Sub
And the sub seems happy with that. But I'm not sure how to do that without breaking the invoker part.
Me.GroupNameHere.Invoke(New MethodInvoker(AddressOf SubNameHere))
Works normally. If SubNameHere() becomes SubNameHere(threadname as thread) then that seems happy enough but the invoke line breaks and doesn't want more than the address of.
Two slight syntax changes sorted it:
Private Async Sub SubName(ThreadNameAs Thread)
and
GroupName.Invoke(New MethodInvoker(Sub() Me.SubName(ThreadName)))
We're creating objects at runtime so before running the code doesn't know what object it is working with. We want to add an event handler to every TextBox which is created at runtime. But when we try AddHandler obj.Leave, AddressOf leaveControl the compiler won't run the program because "object doesn't have an event like Leave".
Is there a way to add an event handler to a object of unknown type?
Thanks :)
VB.NET supports late binding to write dynamic code. That works well for properties and methods but not for events. Odd restriction, I don't know the technical reason for it. Short from it never having to be necessary in earlier versions of Basic where event binding was dynamic based on the method name, I suspect it has something to do with the WithEvents keyword.
The workaround is simple enough, you need to use Reflection. Like this:
Dim obj As Object = New TextBox
Dim evt = obj.GetType().GetEvent("Leave")
evt.AddEventHandler(obj, New EventHandler(AddressOf leaveControl))
You do know it's a textbox, so cast it
AddHandler Ctype(obj,textbox).Leave, AddressOf leaveControl
Without seeing your code for creating the controls, as #Dom suggests, you can check the type of control you are creating using the following (as an example for looking at textboxes only);
Dim tb As TextBox = TryCast(obj, TextBox)
If tb IsNot Nothing
AddHandler tb.Leave, AddressOf leaveControl
End If
Again, this is just an illustration without knowing the full extent of what you are doing in the first place
What you want is something along these lines:
If obj.GetType() Is GetType(TextBox) then
AddHandler obj.Leave, Address myNewRoutine
End If
Note that you can't just have is TextBox you need to use GetType again.
I used the following code:
Sub AppStartup()
AddHandler AppDomain.CurrentDomain.UnhandledException,
Sub(sender As Object, args As UnhandledExceptionEventArgs)
Dim e = CType(args.ExceptionObject, Exception)
ShowMessage("Oops...", HandleErr(e), MessageBoxImage.Error)
End Sub
End Sub
and I don't get the property handled Although it appears this link is supposed to be his
Your link points at XAML article, your question is probably about WinForms.
Are you starting this inside VS? Please note that exception will first be handled by VS, and then by this handler. When you run as a standalone executable, it should work as expected. You don't need to set any e.Handled to anything.
While your question doesn't really make sense, I'd say its safe to say you are trying to implement an unhandled exception handler.
This article should have all the information you need.
http://msdn.microsoft.com/en-gb/library/system.appdomain.unhandledexception.aspx
In VB.net, I have to use AddHandler (to add handler function dynamically) without the brackets.
However if I use brackets like:
AddHandler(MyBtn.Click, AddressOf MyBtn_Click)
It does not work. The call must be without brackets.
Is there any equivalent syntax for calling AddHandler using brackets?
Is AddHandler different from subroutine or a function (method) of some class?
It's a statement, not a function call. Just like Return.
You wouldn't do
Return(x)