AddHandler for events with Optional parameters syntax error - vb.net

In our VB.NET code, we are using many event handlers.
For below line of code we are getting the syntax error "BC30469 Reference to a non-shared member requires an object reference" where we use an optional parameter in the function signature:
AddHandler TabControl.Click, AddressOf FrmMW.ProcessTabClickEvent
where the function signature is
Public Sub ProcessTabClickEvent(sender As System.Object, e As System.EventArgs, Optional OpenNewTab As Boolean = False)
But for the below case, i.e. without optional parameters, we are not getting the syntax error:
AddHandler menuItem.Click, AddressOf SelectAllAction
where the function signature is
Private Sub SelectAllAction(ByVal sender As Object, ByVal e As EventArgs)
Please assist in solution. Our code is getting built successfully in VB Express 2010, but we are getting this error in VB Express 2017.

Related

AddHandler event on dynamic button, incorrect instance properties in VB.NET

I'm new to custom classes. I have a class called 'game'. In the class, I have a method called 'addGame()' that creates a dynamic picture box called 'pBox'. After creating the control, I'm doing the following to register a click event:
AddHandler pBox.Click, AddressOf Me.launchGame
And here is launchGame:
Public Sub launchGame()
MsgBox(Me.name)
End Sub
The problem is, "Me.name" is always the most recently added instances name, not the one I clicked on.
Based on a suggestion, I also tried this:
Public Sub launchGame(ByVal sender As Object)
MsgBox(sender.name)
End Sub
But now "AddHandler pBox.Click, AddressOf Me.launchGame" says
Method 'Public Sub launchGame(sender As Object)' does not have a signature compatible with delegate 'Delegate Sub EventHandler(sender As Object, e As System.EventArgs)'
And "AddHandler pBox.Click, AddressOf Me.launchGame(Me)" says
AddressOf operand must be the name of a method without parentheses
Public Sub launchGame(ByVal sender As Object, ByVal sender as EventArgs)
MsgBox(sender.name)
End Sub
Now no errors, but the msgBox is blank.
I think the problem was that pBox was always the most recent pictureBox control. I created a control Array based on Creating Control Arrays in Visual Basic .NET and Visual C# .NET (MSDN).
Now I do the AddHandler in the AddNewpBox() method of my pBoxArray class.
I also created a list to handle the "game" class, as suggested by David Brunow. Then I'm setting the pictureBox "Tag" property to the index of the game in the "games" array.
So now my click handler looks like the following, and it seems to work great.
Public Sub pBoxClick(ByVal sender As Object, e As EventArgs)
MsgBox(games(sender.tag).name)
End Sub

Does an AddHandler require parameters?

I'm trying to add a handler, but as soon as I'm targetting a method that has parameters, the handler fails. This is the simple code:
AddHandler App.Current.RootVisual.MouseLeftButtonUp, RootVisual_MouseLeftButtonUp
Private Sub RootVisual_MouseLeftButtonUp(ByVal sender As Object, ByVal e As MouseButtonEventArgs)
End Sub
This error won't let me build. When looking at the examples, I'm doing it right. This is the error I get:
Error 3 Argument not specified for parameter 'e' of 'Private Sub
RootVisual_MouseLeftButtonUp(sender As Object, e As
System.Windows.Input.MouseButtonEventArgs)'. C:\TFS\ProjectCollection\ItemManagementTool\ItemManagementTool.ClientApplication\Views\MainMenu.xaml.vb 82 70 ItemManagementTool.ClientApplication
I get a similar error for the "sender" parameter.
Any ideas?
You are missing the AddressOf keyword
AddHandler App.Current.RootVisual.MouseLeftButtonUp, AddressOf RootVisual_MouseLeftButtonUp

calling webservice asynchronously in vb.net

I am trying to call this webservice asynchrounously in vb.net. So on the aspx side i added this property async="true". Now on the vb.net code side i have this function inside my webservice that i am calling.
So -
dim as as webservice.webstring
as.functionasync(param1, param2)
Now when i run the page, i can see that it wont call the webservice after a timegap.
Should i add .thread.sleep()?
Do i require the beginAsyn function and the EndAsyn function.
I'm using asp.net 3.5 with IIS7
First, please read this MSDN article about how the asynchronous pages work in ASP.NET.
Second, you need to have an asynchronous method in your web-service. Please read this HOWTO article about how to create such methods.
This is how your implementation of the async page could look like:
private _as as WebService.WebString = Nothing
Public Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
AddOnPreRenderCompleteAsync(New BeginEventHandler(BeginCallingWebService),
New EndEventHandler(EndCallingWebService));
End Sub
Private Function BeginCallingWebService(Byval sender As Object, ByVal e As EventArgs, ByVal cb As AsyncCallback, ByVal state As Object)
_as = New WebService.WebString()
Return _as.BeginMyMethod(cb, state)
End Function
Private Sub EndCallingWebService(ByVal ar as IAsyncResult)
Dim result As MyWebServiceResult = _as.EndMyMethod(ar)
' Process the result of the web-service method
End Sub
Hope this will help you.

Create a dynamic control and AddHandle WITH Values/Brackets

it seems that adding for example a button Dim myButton as New Button and then addHandler to mySub("lol", 255) is not possible.
Where mySub is Shared Sub MySub(byRef myString as string, myInteger as Integer)
So: addHandler myButton.click, addressOf mySub("lol", 255) - returns an error saying it does not work with parentheses or whatever.
I somehow see why this might not be possible, so I'm looking for a work-around on this problem.
Please help _jakeCake
First of all the syntax for AddHandler would be:
AddHandler myButton.click, AddressOf mySub
Secondly the signature of the eventhandler procedure must match the signature of the event like so:
Private Sub myButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
[...]
End Sub
Maybe you could look into using a lambda expression when you add the event. When using lambda's in VB.NET the function must return a value and does not support multi-line statements.
Dim myButton As New Button
AddHandler myButton.Click, Function(senderObj, args) myFunc("lol", 255)

C# to VB.Net Syntax conversion

Can any one translate the following syntax to vb.net.
m_TextBox.Loaded += TextBoxLoaded
m_TextBox.Loaded -= TextBoxLoaded;
private void TextBoxLoaded(object sender, RoutedEventArgs e)
{
Init();
}
..
containsTextProp.AddValueChanged(m_TextBox, (sender, args) => UpdateAdorner());
...
private void UpdateAdorner()
{...}
Despite the 25% acceptance rate, here it is:
AddHandler m_TextBox.Loaded, AddressOf TextBoxLoaded
RemoveHandler m_TextBox.Loaded, AddressOf TextBoxLoaded
Private Sub TextBoxLoaded(ByVal sender as Object, ByVal e as RoutedEventArgs)
Init()
End Sub
Your call to AddValueChanged can't be directly translated, as VB.NET's lambda expression support is not as robust as C#'s. In particular, VB.NET lambdas must be an expression, so you must either return a value or call a Function. In your case, you would be calling a Sub, which isn't allowed in VB.NET. You should consider changing the signature of UpdateAdorner to be a standard event handler (like the TextBoxLoaded method) and pass AddressOf UpdateAdoerner to AddValueChanged.
Like this:
containsTextProp.AddValueChanged(m_TextBox, AddressOf UpdateAdorner);
...
Private Sub UpdateAdorner(ByVal sender as Object, ByVal e as EventArgs)
...
End Sub
There are plenty of online converters, you shuold probably try that first next time and post here if it doesn't work or you have a problem.
AddHandler m_TextBox.Loaded, AddressOf TextBoxLoaded ' per #Adam Robinson'
RemoveHandler m_TextBox.Loaded, AddressOf TextBoxLoaded ' per #Adam Robinson'
Private Sub TextBoxLoaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
Init()
End Sub
Private Sub UpdateAdorner()
End Sub
You might find the "C# and VB.NET Comparison Cheat Sheet" useful.
http://aspalliance.com/625
You can toss it in an app, build it, then open the app in .NET reflector. .NET Reflector can take the IL and "turn it into" C#/VB.NET, etc.