Solved

VB.net Errors after converting from C#

Posted on 2014-09-09
7
303 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:Murray Brown
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 40311777
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:Murray Brown
ID: 40311791
Hi. Thanks. I get 'e is not declared'
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 40311843
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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40311848
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:Murray Brown
ID: 40311887
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 70

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40311900
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:Murray Brown
ID: 40311977
Thanks very much Eric
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

624 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