Solved

VB.net Errors after converting from C#

Posted on 2014-09-09
7
280 Views
Last Modified: 2014-09-09
Hi
I am trying to convert the code at the following link to VB.net but I get the error 'ERROR Expression does not produce a value
This is shown at the three lines marked with ERROR
http://www.codeproject.com/Tips/709121/Move-and-Resize-Controls-on-a-Form-at-Runtime-With

The code I get follows. The
Imports System.Collections.Generic
Imports System.Drawing
Imports System.Globalization
Imports System.Linq
Imports System.Windows.Forms

Public Class ControlMoverOrResizer

    Private Shared _moving As Boolean
    Private Shared _cursorStartPoint As Point
    Private Shared _moveIsInterNal As Boolean
    Private Shared _resizing As Boolean
    Private Shared _currentControlStartSize As Size
    Friend Shared Property MouseIsInLeftEdge() As Boolean
        Get
            Return m_MouseIsInLeftEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInLeftEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInLeftEdge As Boolean
    Friend Shared Property MouseIsInRightEdge() As Boolean
        Get
            Return m_MouseIsInRightEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInRightEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInRightEdge As Boolean
    Friend Shared Property MouseIsInTopEdge() As Boolean
        Get
            Return m_MouseIsInTopEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInTopEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInTopEdge As Boolean
    Friend Shared Property MouseIsInBottomEdge() As Boolean
        Get
            Return m_MouseIsInBottomEdge
        End Get
        Set(value As Boolean)
            m_MouseIsInBottomEdge = value
        End Set
    End Property
    Private Shared m_MouseIsInBottomEdge As Boolean

    Friend Enum MoveOrResize
        Move
        Resize
        MoveAndResize
    End Enum

    Friend Shared Property WorkType() As MoveOrResize
        Get
            Return m_WorkType
        End Get
        Set(value As MoveOrResize)
            m_WorkType = value
        End Set
    End Property
    Private Shared m_WorkType As MoveOrResize

    Friend Shared Sub Init(control As Control)
        Init(control, control)
    End Sub

    Friend Shared Sub Init(control As Control, container As Control)
        _moving = False
        _resizing = False
        _moveIsInterNal = False
        _cursorStartPoint = Point.Empty
        MouseIsInLeftEdge = False
        MouseIsInLeftEdge = False
        MouseIsInRightEdge = False
        MouseIsInTopEdge = False
        MouseIsInBottomEdge = False
        WorkType = MoveOrResize.MoveAndResize
        AddHandler control.MouseDown, Function(sender, e) StartMovingOrResizing(control, e) 'ERROR Expression does not produce a value
        AddHandler control.MouseUp, Function(sender, e) StopDragOrResizing(control) 'ERROR Expression does not produce a value
        AddHandler control.MouseMove, Function(sender, e) MoveControl(container, e) 'ERROR Expression does not produce a value
    End Sub

    Private Shared Sub UpdateMouseEdgeProperties(control As Control, mouseLocationInControl As Point)
        If WorkType = MoveOrResize.Move Then
            Return
        End If
        MouseIsInLeftEdge = Math.Abs(mouseLocationInControl.X) <= 2
        MouseIsInRightEdge = Math.Abs(mouseLocationInControl.X - control.Width) <= 2
        MouseIsInTopEdge = Math.Abs(mouseLocationInControl.Y) <= 2
        MouseIsInBottomEdge = Math.Abs(mouseLocationInControl.Y - control.Height) <= 2
    End Sub

    Private Shared Sub UpdateMouseCursor(control As Control)
        If WorkType = MoveOrResize.Move Then
            Return
        End If
        If MouseIsInLeftEdge Then
            If MouseIsInTopEdge Then
                control.Cursor = Cursors.SizeNWSE
            ElseIf MouseIsInBottomEdge Then
                control.Cursor = Cursors.SizeNESW
            Else
                control.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInRightEdge Then
            If MouseIsInTopEdge Then
                control.Cursor = Cursors.SizeNESW
            ElseIf MouseIsInBottomEdge Then
                control.Cursor = Cursors.SizeNWSE
            Else
                control.Cursor = Cursors.SizeWE
            End If
        ElseIf MouseIsInTopEdge OrElse MouseIsInBottomEdge Then
            control.Cursor = Cursors.SizeNS
        Else
            control.Cursor = Cursors.[Default]
        End If
    End Sub

    Private Shared Sub StartMovingOrResizing(control As Control, e As MouseEventArgs)
        If _moving OrElse _resizing Then
            Return
        End If
        If WorkType <> MoveOrResize.Move AndAlso (MouseIsInRightEdge OrElse MouseIsInLeftEdge OrElse MouseIsInTopEdge OrElse MouseIsInBottomEdge) Then
            _resizing = True
            _currentControlStartSize = control.Size
        ElseIf WorkType <> MoveOrResize.Resize Then
            _moving = True
            control.Cursor = Cursors.Hand
        End If
        _cursorStartPoint = New Point(e.X, e.Y)
        control.Capture = True
    End Sub

    Private Shared Sub MoveControl(control As Control, e As MouseEventArgs)
        If Not _resizing AndAlso Not _moving Then
            UpdateMouseEdgeProperties(control, New Point(e.X, e.Y))
            UpdateMouseCursor(control)
        End If
        If _resizing Then
            If MouseIsInLeftEdge Then
                If MouseIsInTopEdge Then
                    control.Width -= (e.X - _cursorStartPoint.X)
                    control.Left += (e.X - _cursorStartPoint.X)
                    control.Height -= (e.Y - _cursorStartPoint.Y)
                    control.Top += (e.Y - _cursorStartPoint.Y)
                ElseIf MouseIsInBottomEdge Then
                    control.Width -= (e.X - _cursorStartPoint.X)
                    control.Left += (e.X - _cursorStartPoint.X)
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height
                Else
                    control.Width -= (e.X - _cursorStartPoint.X)
                    control.Left += (e.X - _cursorStartPoint.X)
                End If
            ElseIf MouseIsInRightEdge Then
                If MouseIsInTopEdge Then
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width
                    control.Height -= (e.Y - _cursorStartPoint.Y)

                    control.Top += (e.Y - _cursorStartPoint.Y)
                ElseIf MouseIsInBottomEdge Then
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width
                    control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height
                Else
                    control.Width = (e.X - _cursorStartPoint.X) + _currentControlStartSize.Width
                End If
            ElseIf MouseIsInTopEdge Then
                control.Height -= (e.Y - _cursorStartPoint.Y)
                control.Top += (e.Y - _cursorStartPoint.Y)
            ElseIf MouseIsInBottomEdge Then
                control.Height = (e.Y - _cursorStartPoint.Y) + _currentControlStartSize.Height
            Else
                StopDragOrResizing(control)
            End If
        ElseIf _moving Then
            _moveIsInterNal = Not _moveIsInterNal
            If Not _moveIsInterNal Then
                Dim x As Integer = (e.X - _cursorStartPoint.X) + control.Left
                Dim y As Integer = (e.Y - _cursorStartPoint.Y) + control.Top
                control.Location = New Point(x, y)
            End If
        End If
    End Sub

    Private Shared Sub StopDragOrResizing(control As Control)
        _resizing = False
        _moving = False
        control.Capture = False
        UpdateMouseCursor(control)
    End Sub

#Region "Save And Load"

    Private Shared Function GetAllChildControls(control As Control, list As List(Of Control)) As List(Of Control)
        Dim controls As List(Of Control) = control.Controls.Cast(Of Control)().ToList()
        list.AddRange(controls)
        Return controls.SelectMany(Function(ctrl) GetAllChildControls(ctrl, list)).ToList()
    End Function

    Friend Shared Function GetSizeAndPositionOfControlsToString(container As Control) As String
        Dim controls As New List(Of Control)()
        GetAllChildControls(container, controls)
        Dim cultureInfo As New CultureInfo("en")
        Dim info As String = String.Empty
        For Each control As Control In controls
            info += control.Name & ":" & control.Left.ToString(cultureInfo) & "," & control.Top.ToString(cultureInfo) & "," & control.Width.ToString(cultureInfo) & "," & control.Height.ToString(cultureInfo) & "*"
        Next
        Return info
    End Function
    Friend Shared Sub SetSizeAndPositionOfControlsFromString(container As Control, controlsInfoStr As String)
        Dim controls As New List(Of Control)()
        GetAllChildControls(container, controls)
			Dim controlsInfo As String() = controlsInfoStr.Split(New () {"*"}, StringSplitOptions.RemoveEmptyEntries)
        Dim controlsInfoDictionary As New Dictionary(Of String, String)()
        For Each controlInfo As String In controlsInfo
				Dim info As String() = controlInfo.Split(New () {":"}, StringSplitOptions.RemoveEmptyEntries)
            controlsInfoDictionary.Add(info(0), info(1))
        Next
        For Each control As Control In controls
            Dim propertiesStr As String
            controlsInfoDictionary.TryGetValue(control.Name, propertiesStr)
				Dim properties As String() = propertiesStr.Split(New () {","}, StringSplitOptions.RemoveEmptyEntries)
            If properties.Length = 4 Then
                control.Left = Integer.Parse(properties(0))
                control.Top = Integer.Parse(properties(1))
                control.Width = Integer.Parse(properties(2))
                control.Height = Integer.Parse(properties(3))
            End If
        Next
    End Sub

#End Region

Open in new window

0
Comment
Question by:murbro
7 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
I think it should be simply like this:
        AddHandler control.MouseDown, StartMovingOrResizing(control, e) 
        AddHandler control.MouseUp, StopDragOrResizing(control) 
        AddHandler control.MouseMove, MoveControl(container, e) 

Open in new window

0
 

Author Comment

by:murbro
Comment Utility
Hi. Thanks. I get 'e is not declared'
0
 
LVL 83

Expert Comment

by:CodeCruiser
Comment Utility
See example of anonymous function with addhandler at the following page

http://10rem.net/blog/2010/04/16/tip-anonymous-event-handlers-in-vbnet


Public Sub LoadPolicyDetail()
    Dim client As New Services.Service1Client()
 
    AddHandler client.GetPolicyDetailCompleted,
        Sub(s As Object, e As GetPolicyDetailCompletedEventArgs)
            If e.Result <> Nothing Then
                MessageBox.Show("Awesome!")
            End If
        End Sub
 
    client.GetPolicyDetailAsync()
End Sub

Open in new window

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
can you try this:

AddHandler control.MouseDown, AddressOf  StartMovingOrResizing
AddHandler control.MouseUp, AddressOf  StopDragOrResizing
AddHandler control.MouseMove, AddressOf  MoveControl

Open in new window

0
 

Author Comment

by:murbro
Comment Utility
Thanks Eric. The top and bottom lines work but I get the following error for the middle line
Error      4      Method 'Private Shared Sub StopDragOrResizing(control As System.Windows.Forms.Control)' does not have a signature compatible with delegate 'Delegate Sub MouseEventHandler(sender As Object, e As System.Windows.Forms.MouseEventArgs)'.
0
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
Comment Utility
change:
Private Shared Sub StopDragOrResizing(control As Control)

Open in new window


for:
Private Shared Sub StopDragOrResizing(control As Control, e As System.Windows.Forms.MouseEventArgs)

Open in new window

0
 

Author Closing Comment

by:murbro
Comment Utility
Thanks very much Eric
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now