Hi, in vb 6.0 the listview had a view property, if you set that property to Icons you could move the ítems wherever you wanted but now i don't see anything like that, the behaviour i want to have is like the Windows desktop where you can move the ícons and put them where you want by drag and drop by the way is for menú, i wnat the user can have the hability to move the menú icons wich in ths case are listview ítems.. thanks..

Recommended Answers

All 5 Replies

Perhaps something like this will work for you.

Public Class Form1

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      With ListView1
         .Alignment = ListViewAlignment.SnapToGrid
         .AllowDrop = True
         .AutoArrange = False
         .Scrollable = False
         .View = View.LargeIcon
      End With
   End Sub

   Private Sub ListView1_ItemDrag(ByVal sender As Object, ByVal e As System.Windows.Forms.ItemDragEventArgs) Handles ListView1.ItemDrag
      Dim lvi As ListViewItem = CType(e.Item, ListViewItem)
      ListView1.DoDragDrop(New DataObject("System.Windows.Forms.ListViewItem", lvi), DragDropEffects.Move)
   End Sub

   Private Sub ListView1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragEnter
      If e.Data.GetDataPresent("System.Windows.Forms.ListViewItem") Then
         e.Effect = DragDropEffects.Move
      End If
   End Sub

   Private Sub ListView1_DragOver(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles ListView1.DragOver
      Dim lvi As ListViewItem = CType(e.Data.GetData("System.Windows.Forms.ListViewItem"), ListViewItem)
      Dim Offset As Size = Size.Subtract(Cursor.Size, New Size(Cursor.HotSpot.X, Cursor.HotSpot.Y))
      lvi.Position = Point.Subtract(ListView1.PointToClient(New Point(e.X, e.Y)), Offset)
      e.Effect = DragDropEffects.Move
   End Sub

End Class

TnTinNM Thank u very Much, that's exactly what i needed it seems like you have experience with listviews let me ask you another question, in vb 6 you could set the BackgroundImageAligment in .net thats not allowed, Most Backgroundimages have the BackgroundImageLayout property but the listview one doesn't, so how can i set the Layout of the BgImage.. thanks again..

it seems like you have experience with listviews

Not really, I do not particularly like this control.

n vb 6 you could set the BackgroundImageAligment in .net thats not allowed

Yeh, if you do a search you will find that in the original .Net they had some issues with it. I did some snooping of the source code and for some reason they did not give you the option to set the x,y positioning eventhough there does not appear to be any reason for it. They just locked it into (0,0), the top-left corner. So as usual when the control does not do what you want, you need to create your own version. I had some issues with graphical artifacts when something dragged over the image, so I added some hacks. Its not perfect, but close. It does give you a bit more control on positioning. You set the (x,y) point as an integer percentage value for x and y. You will see the new properties in the property grid. Just add this to your project, build the project and it should show up at the top of your toolbox.

Public Class LV
   Inherits ListView

   <Runtime.InteropServices.DllImport("user32.dll", SetLastError:=True, CharSet:=Runtime.InteropServices.CharSet.Auto)> _
   Private Shared Function SendMessage(ByVal hWnd As IntPtr, _
                                       ByVal Msg As Int32, _
                                       ByVal wParam As IntPtr, _
                                       ByRef lParam As LVBKIMAGE) As IntPtr
   End Function

   Private Structure LVBKIMAGE
      Public ulFlags As LVFlags
      Public hbm As IntPtr
      Public pszImage As String
      Public cchImageMax As Int32
      Public xOffsetPercent As Int32
      Public yOffsetPercent As Int32
   End Structure

   <Flags()> _
   Private Enum LVFlags As UInt32
      LVBKIF_FLAG_TILEOFFSET = &H100
      LVBKIF_SOURCE_HBITMAP = &H1
      LVBKIF_SOURCE_NONE = 0
      LVBKIF_STYLE_NORMAL = 0
      LVBKIF_SOURCE_URL = &H2
      LVBKIF_STYLE_TILE = &H10
   End Enum


   Private _BackgroundImage As Image
   Public Overrides Property BackgroundImage() As Image
      Get
         Return _BackgroundImage
      End Get
      Set(ByVal value As Image)
         _BackgroundImage = value
         BackgroundImgChanged = True
         SetBackGroundImage()

      End Set
   End Property 'BackgroundImage

   Private _BKImage_X_Position As Int32
   <System.ComponentModel.Description("Range 0 to 100 (left to right)" & vbCrLf & "Percent of width BK image position")> _
   Public Property BKImage_X_Position() As Int32
      Get
         Return _BKImage_X_Position
      End Get
      Set(ByVal value As Int32)
         If value < 0 OrElse value > 100 Then
            Throw New ArgumentException("Invalid x position percentage")
         End If
         _BKImage_X_Position = value
         SetBackGroundImage()
      End Set
   End Property 'BKImage_X_Position

   Private _BKImage_Y_Position As Int32
   <System.ComponentModel.Description("Range 0 to 100 (top to bottom)" & vbCrLf & "Percent of height BK image position")> _
   Public Property BKImage_Y_Position() As Int32
      Get
         Return _BKImage_Y_Position
      End Get
      Set(ByVal value As Int32)
         If value < 0 OrElse value > 100 Then
            Throw New ArgumentException("Invalid y position percentage")
         End If
         _BKImage_Y_Position = value
         SetBackGroundImage()
      End Set
   End Property 'BKImage_y_Position

   Private Const LVM_FIRST As Int32 = &H1000
   Private Const LVM_SETBKIMAGE As Int32 = (LVM_FIRST + 68)
   Private tmpFile As String

   Private BackgroundImgChanged As Boolean

   Private Sub SetBackGroundImage()
      Application.OleRequired()
      If BackgroundImage IsNot Nothing AndAlso Me.IsHandleCreated Then
         If BackgroundImgChanged Then
            DeleteTempFile()
            tmpFile = IO.Path.GetTempFileName
            BackgroundImage.Save(tmpFile)
            BackgroundImgChanged = False
         End If

         Dim lvbkimg As LVBKIMAGE
         With lvbkimg
            .xOffsetPercent = _BKImage_X_Position
            .yOffsetPercent = _BKImage_Y_Position
            .ulFlags = LVFlags.LVBKIF_SOURCE_URL Or _
                       If(BackgroundImageTiled, LVFlags.LVBKIF_STYLE_TILE, LVFlags.LVBKIF_STYLE_NORMAL)
            .pszImage = tmpFile & ControlChars.NullChar
            .cchImageMax = Len(tmpFile) + 1
         End With
         SendMessage(Me.Handle, LVM_SETBKIMAGE, IntPtr.Zero, lvbkimg)
         Refresh()
      End If
   End Sub

   Private Sub DeleteTempFile()
      If Not String.IsNullOrEmpty(tmpFile) AndAlso System.IO.File.Exists(tmpFile) Then
         System.IO.File.Delete(tmpFile)
      End If
   End Sub

   Protected Overrides Sub Dispose(ByVal disposing As Boolean)
      'If BackgroundImage IsNot Nothing Then BackgroundImage.Dispose()
      DeleteTempFile()
      MyBase.Dispose(disposing)
   End Sub

   Protected Overrides Sub OnCreateControl()
      MyBase.OnCreateControl()
      SetBackGroundImage()
   End Sub
   Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
      MyBase.OnHandleCreated(e)
      SetBackGroundImage()
   End Sub

   Protected Overrides Sub OnPaint(ByVal pevent As System.Windows.Forms.PaintEventArgs)
      MyBase.OnPaint(pevent)
      SetBackGroundImage()
   End Sub

   Private parentform As Form
   ''' <summary>
   ''' Trap events on parent form to deal with paint artifacts
   ''' </summary>
   Protected Overrides Sub OnParentChanged(ByVal e As System.EventArgs)
      MyBase.OnParentChanged(e)
      If Me.Parent IsNot Nothing Then
         parentform = Me.FindForm
         If parentform IsNot Nothing Then
            AddHandler parentform.Paint, AddressOf Form_Paint
            AddHandler parentform.SizeChanged, AddressOf Form_SizeChanged
         End If
      End If
   End Sub

   Private Sub Form_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs)
      SetBackGroundImage()
   End Sub
   Private Sub Form_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs)
      SetBackGroundImage()
   End Sub
End Class

Please close out this thread as your orginal question seems answered. Thanks.

Thanks for your help TnTinMN i will test this in my project..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.