How to get the form to resize two listviews using anchor without overlapping.

awardle

Newcomer
Joined
Apr 21, 2003
Messages
9
Hi

I've been trying to find an answer to this for ages now, every time I try and set the anchors to my two list views and then start the project and resize the form the listviews overlap each other.

I tried using splitter control but got in a muddle with that any help would be appreciated.

The two list views are horizontal on the form

Thanks
Aaron
 
You could do the resizing/moving math on the fly. I think that might be the only way you can get the affect you are looking for.

To do this I would probably ancor each to the side of the form that are on (left and right) so that they stay on their respective sides when you resize the form. They I'd come up with a growth ratio for the sizeOfForm:sizeOfListView (horizontal and verticle) and resize accourdingly on the form.resize event. Give it a try at least.

If you have troubles getting things to work just right with the resizing, there is an API you can use to capture the resizing event. Ask around, I'm sure someone out there will know it.
 
mskeel is correct that you'll need to calculate the ratio of change from you're default form's size, and apply that ratio to the listview's size, column size and location. The far left hand listview can rely on the anchor position, but the right hand listview and maybe the splitter locations will need to be programmably changed.

Here's a short example that resizes a listview, its columns and other controls on the form. Since the only height change occurs with the listview the only ratio variable used is Ratio_X for the horizontal size change.

Visual Basic:
Private Sub Form1_SizeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.SizeChanged

  If Me.Width < 500 Or Me.Height < 400 Then  Exit Sub 'set these values a minimums

  Dim Ratio_X As Double = Me.Width / 800
  Dim i, k as Integer

  Me.SuspendLayout() 'this stops flickering, but does load the new form image with a flash
  lvwData.BeginUpdate()
  lvwData.Scrollable = True
  Me.lvwData.Size = New Size(Me.Width - 8, Me.Height - 600 + 218) 'applying ratios
  Me.lvwData.Columns(0).Width = 115 * Ratio_X 'Description
  Me.lvwData.Columns(1).Width = 30 * Ratio_X 'Quantity
  Me.lvwData.Columns(2).Width = 25 * Ratio_X 'Phase
  For i = 3 To 10
      If lvwData.Columns(i).Text = "Eff" Then 'special sizing rules
           For k = 3 To 12
                If lvwData.Columns(k).Text = "Eff" Then
                   Me.lvwData.Columns(k).Width = 37 * Ratio_X 'Eff
                ElseIf lvwData.Columns(k).Text = "PF" Then
                   Me.lvwData.Columns(k).Width = 37 * Ratio_X 'PF
                ElseIf lvwData.Columns(k).Text = "Cdtr ga." Then
                   Me.lvwData.Columns(k).Width = 50 * Ratio_X 'conductor gauge
                ElseIf lvwData.Columns(k).Text = "Gnd ga." Then
                   Me.lvwData.Columns(k).Width = 50 * Ratio_X 'ground gauge
                'normal sizing rules
                Else : Me.lvwData.Columns(k).Width = 74 * Ratio_X 'Optional Columns
                End If
            Next
       Else 'normal sizing rules
            For k = 3 To 11
                If lvwData.Columns(k).Text = "Cdtr ga." Then
                    Me.lvwData.Columns(k).Width = 50 * Ratio_X 'conductor gauge
                ElseIf lvwData.Columns(k).Text = "Gnd ga." Then
                    Me.lvwData.Columns(k).Width = 50 * Ratio_X 'ground gauge
                Else : Me.lvwData.Columns(k).Width = 74 * Ratio_X 'Optional Columns
                End If
            Next
        End If
    Next
    lvwData.Scrollable = False
    lvwData.EndUpdate()

    'move and resize other controls
    Me.pnlMain.Width = Me.Width - 8

    Me.StatusBar1.Width = Me.pnlMain.Width
    Me.StatusProgram.Width = Me.Width - 800 + 558

    lblFileName.Location = New Point(191 + (Me.Width - 800) / 2, 41)
    txtFileName.Location = New Point(251 + (Me.Width - 800) / 2, 34)
    grpDeviceInfo.Location = New Point(5 + (Me.Width - 800) / 2, 54)
    grpConductorInfo.Location = New Point(grpDeviceInfo.Location.X + 405, 25)
    grpDistance.Location = New Point(grpDeviceInfo.Location.X + 588, 25)
    grpNecWire.Location = New Point(grpDistance.Location.X, 92)

    'refresh the container controls
    grpDeviceInfo.Invalidate()
    grpConductorInfo.Invalidate()
    grpDistance.Invalidate()
    grpNecWire.Invalidate()
    pnlMain.Invalidate()
    lvwData.Invalidate()

    Me.ResumeLayout()
    CheckGridLength() 'this is a special sub that checks the listview length and resizes the columns if it requires scroll bars.
    Me.Refresh()
End Sub

Although this is a complete example, including all other controls, I hope that it'll give you an idea how to resize yours using managed code.
 
Last edited:
Back
Top