Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 972539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:08:26+00:00 2026-05-16T03:08:26+00:00

I support a .Net 2.0 Windows application that has a custom listview control to

  • 0

I support a .Net 2.0 Windows application that has a custom listview control to allow for greater flexibility. I am not allowed to bring this to a newer version of the gramework until next year. We are moving from XP to Windows 7, and we have identified an issue with how the column sort image (up/down arrow) is drawn to the listview. Currently in XP, the image is drawn to the right of the column’s text. However, in Windows 7, I am getting Access Violation Exceptions. Now, I know that the error is with the improper handling of memory using the unmanaged code as shown below. I am looking for a method to mimic what we have below in a safe manner using managed code.

Public Shared Sub ColumnImageToRight(ByVal view As ListView, ByVal index As Integer)
        Dim LVM_GETCOLUMNW As Integer = &H1000 + 95
        Dim LVM_SETCOLUMNW As Integer = &H1000 + 96
        If Not view.IsHandleCreated Then
            Throw New InvalidOperationException("ListView not yet created, wait...")
        End If
        If index >= view.Columns.Count Then
            Throw New ArgumentOutOfRangeException("Column index out of range")
        End If
        Dim buf As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(LVCOLUMN)))
        Dim lvc As New LVCOLUMN()
        lvc.mask = &HFFFF
        Marshal.StructureToPtr(lvc, buf, False)
        Dim retval As IntPtr = SendMessageW(view.Handle, LVM_GETCOLUMNW, CType(index, IntPtr), buf)
        lvc = CType(Marshal.PtrToStructure(buf, GetType(LVCOLUMN)), LVCOLUMN)
        lvc.fmt = lvc.fmt Or &H1000
        Marshal.StructureToPtr(lvc, buf, False)
        retval = SendMessageW(view.Handle, LVM_SETCOLUMNW, CType(index, IntPtr), buf)
        Marshal.FreeHGlobal(lvc.pszText)
        Marshal.FreeHGlobal(buf)
    End Sub
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:08:26+00:00Added an answer on May 16, 2026 at 3:08 am

    I was able to migrate from the unmanaged code to managed code with the methods below:

    Private Sub ListViewEx_DrawColumnHeader(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawListViewColumnHeaderEventArgs) Handles Me.DrawColumnHeader
        Dim lcIcon As Drawing.Image = Nothing
        If miSelectedColumn = e.ColumnIndex Then
            Select Case Me.Sorting
                Case SortOrder.Ascending
                    lcIcon = Me.SmallImageList.Images(EnumSortImageKeys.Ascending)
                Case SortOrder.Descending
                    lcIcon = Me.SmallImageList.Images(EnumSortImageKeys.Descending)
                Case SortOrder.None
                    'Don't Do Anything
            End Select
        End If
        If lcIcon IsNot Nothing Then
            Dim liMargin As Integer = 4
            Dim x, y As Integer
            e.DrawBackground()
    
            Dim lcFontSize As Drawing.SizeF = e.Graphics.MeasureString(e.Header.Text, e.Font)
            x = CInt(lcFontSize.Width + e.Bounds.Left + (liMargin * 2))
            y = CInt((e.Bounds.Height / 2) - (lcIcon.Height / 2))
    
            Dim lsText As String = e.Header.Text
            If lcFontSize.Width + lcIcon.Width + liMargin > e.Bounds.Width Then
                Dim liCharWidth As Integer = CInt(lcFontSize.Width / lsText.Length)
                Dim liLength As Integer = CInt((e.Bounds.Width - (liMargin + lcIcon.Width)) / liCharWidth)
    
                lsText = lsText.Substring(0, liLength)
    
                lcFontSize = e.Graphics.MeasureString(lsText, e.Font)
                x = CInt(lcFontSize.Width + e.Bounds.Left + liMargin)
                y = CInt((e.Bounds.Height / 2) - (lcIcon.Height / 2))
                liMargin = CInt(liMargin / 2)
            End If
    
            e.Graphics.DrawString(lsText, e.Font, Drawing.Brushes.Black, e.Bounds.Left + liMargin, CSng((e.Bounds.Height / 2) - (lcFontSize.Height / 2)))
            e.Graphics.DrawImageUnscaled(lcIcon, x, y)
        Else
            e.DrawDefault = True
        End If
    End Sub
    
    Private Sub lv_DrawItem(ByVal view As Object, ByVal e As System.Windows.Forms.DrawListViewItemEventArgs) Handles Me.DrawItem
        e.DrawDefault = True
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 495k
  • Answers 495k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can check Environment.UserInteractive property which will return false if… May 16, 2026 at 11:27 am
  • Editorial Team
    Editorial Team added an answer Your server is too slow or it doesn't responds (networking… May 16, 2026 at 11:27 am
  • Editorial Team
    Editorial Team added an answer If your object is always related to a single form,… May 16, 2026 at 11:27 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I want to write an application in .Net that has to be shipped with
If I develop a .Net application that's going to run on WinXP and Vista,
I have a .NET WinForms application that I've converted into a COM dll using
We have an ASP.NET web application that we offer as a Service (it's hosted
The company for which I work for has built a large ASP.NET MVC application
First, a little background. I have written a custom HTTP compression module for ASP.NET.
.NET has become a popular technology among software developers and comsumers. I was thinking
We have a web application that passes parameters in the url along the lines
We have built a Windows Service that is running on client's machines, which occasionally
I would like to know the version of ASP.NET supports using Windows AppFabric caching

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.