none
What's wrong with my calling signature? RRS feed

  • Question

  • I have a subroutine defined as follows:

    <System.Runtime.CompilerServices.Extension>
    Public Sub MoveItself(Of T As Control)(ByVal ctrl As T)
       <code>
    End Sub

    but when I call it like this:

    MoveItself(MyControl)

    I get this error message:

       BC36582    Too many arguments to extension method 'Public Sub MoveItself()

    Why?

    Wednesday, October 30, 2019 6:22 PM

All replies

  • Hello,

    This

    <Runtime.CompilerServices.Extension>
    Public Sub MoveItself(Of T As Control)(ByVal ctrl As T)
    
    End Sub

    Called

    Button1.MoveItself()


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 30, 2019 7:06 PM
    Moderator
  • I tried that.  It is allowed for, for example, a Button.  But it is not allowed for an Ellipse or for a Polygon, which is is my case.  Do I have to add something to Graphics controls to make it work.

    Also, I now have a second problem.  I used a Button just too make sure my MoveItself was working:

    Button1.MoveItself

    The compiler is ok with that but the Button does not respond.  Here is the code"

    Private MouseDown As Boolean
    	Private LastLocation As Point
    
    	<System.Runtime.CompilerServices.Extension>
    	Public Sub MoveItself(Of T As Control)(ByVal ctrl As T)
    		Debug.WriteLine("Runtime")
    		AddHandler ctrl.MouseDown, Sub(o, e)
    									   MouseDown = True
    									   LastLocation = e.GetPosition(DirectCast(o, Control))
    								   End Sub
    		AddHandler ctrl.MouseMove, Sub(o, e)
    									   If MouseDown Then
    										   Dim c As Control = DirectCast(o, Control)
    										   c.Margin = New Thickness(c.Margin.Left - LastLocation.X + e.GetPosition(c).X, c.Margin.Top - LastLocation.Y + e.GetPosition(c).Y, 0, 0)
    									   End If
    								   End Sub
    		AddHandler ctrl.MouseUp, Sub(o, e)
    									 MouseDown = False
    								 End Sub
    	End Sub

    Can you see where the problem lies?

    Wednesday, October 30, 2019 8:54 PM
  • In regards to "But it is not allowed for an Ellipse or for a Polygon, which is is my case." That is because they are not controls.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 30, 2019 9:19 PM
    Moderator
  • Also not sure where you are getting this from LastLocation = e.GetPosition(DirectCast(o, Control)) 

    MouseEventArgs does not have GetPosition

    This is what I used to do a simple test

    Option Strict  On
    Option Infer On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Controls.OfType(of Button).
                Where(Function(button) button.Name <> "Button1").
                ToList().
                ForEach(Sub(button)
                    AddHandler button.MouseDown, sub(s as Object,ea as MouseEventArgs)
                       Console.WriteLine($"{CType(s,Button).Name}")                              
                    End sub
                End Sub)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        End Sub
    End Class
    

     

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, October 30, 2019 9:46 PM
    Moderator
  • In regards to "But it is not allowed for an Ellipse or for a Polygon, which is is my case." That is because they are not controls.

    D'oh!  Now I feel stupid. Of course.

    MouseEventArgs does not have GetPosition

    It's not a mouse arg, it's a control arg.

    Friday, November 1, 2019 5:21 PM
  • In regards to "But it is not allowed for an Ellipse or for a Polygon, which is is my case." That is because they are not controls.

    D'oh!  Now I feel stupid. Of course.

    MouseEventArgs does not have GetPosition

    It's not a mouse arg, it's a control arg.

    Unless you compile with Option Strict Off I can't get what you got in regards to GetPosition. 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, November 1, 2019 7:24 PM
    Moderator