title: auto resize column width to fit content in a dialog
body: <p><img src="https://i.stack.imgur.com/jIhD4.png" alt="Screenshot of wizard dialog"></p>
  
  <p>I want to resize column width automatically to match the longest content width. And I have made a function (<code>ResizeColumnWidth</code>) to do this job. However, it seems that the function did't work after adding contents longer than header. Once I click Back or Next button and return this dialog again, it will resize the column width automatically. Did I miss any step after calling resizing function when adding the contents in the field? Or did I called that function in a wrong place?</p>
  
  <p>Here is my code:</p>
  
  <pre><code>void CWzdEventDependencies::ResizeColumnWidths()
  {
      SetRedraw(FALSE);
  
      m_lstDependencies.SetColumnWidth(0, 100);
      int nHeaderWidth = m_lstDependencies.GetColumnWidth(0);
      m_lstDependencies.SetColumnWidth(0, LVSCW_AUTOSIZE);
      int nColumnWidth = m_lstDependencies.GetColumnWidth(0);
      m_lstDependencies.SetColumnWidth(0, max(nColumnWidth, nHeaderWidth));
  
      m_lstDependencies.SetColumnWidth(1, 95);
  
      m_lstDependencies.SetColumnWidth(2, LVSCW_AUTOSIZE_USEHEADER);
      nHeaderWidth = m_lstDependencies.GetColumnWidth(2);
      m_lstDependencies.SetColumnWidth(2, LVSCW_AUTOSIZE);
      nColumnWidth = m_lstDependencies.GetColumnWidth(2);
      m_lstDependencies.SetColumnWidth(2, max(nColumnWidth, nHeaderWidth));
      m_lstDependencies.SortColumn(0);
      SetRedraw(TRUE);
  
  //  m_lstDependencies.SetColumnWidth(0, 50);
  
  }
  
      enter code here
  
  void CWzdEventDependencies::InsertDependencies()
  {
      m_lstDependencies.DeleteAllItems();
      CorrelationData::FieldsDependenciesType::const_iterator It = m_pFieldsDependencies-&gt;begin();
      int nDependency = 0;
      for(; It != m_pFieldsDependencies-&gt;end(); ++It, ++nDependency)
      {
          InsertDependency(*It);
      }
  }
  
  int CWzdEventDependencies::InsertDependency(const CorrelationEventFieldDependencies* pDependency)
  {
      AString strField1    = pDependency-&gt;GetField1();
      AString strOperation = pDependency-&gt;GetOperation();
      AString strField2    = pDependency-&gt;GetField2();
  
      LVITEM lvItem = {0};
  
      lvItem.mask         = LVIF_TEXT|LVIF_PARAM;
      lvItem.iItem        = m_lstDependencies.GetItemCount();
      lvItem.pszText      = strField1.LockBuffer();
      lvItem.lParam       = (LPARAM) pDependency;
  
      lvItem.iItem = m_lstDependencies.InsertItem(&amp;lvItem, m_pCmbFields);
      strField1.UnlockBuffer();
      ASSERT(lvItem.iItem != -1);
  
      lvItem.pszText      = strOperation.LockBuffer();
      lvItem.iSubItem = 1;
      m_lstDependencies.SetItem(&amp;lvItem, m_pCmbSign);
      strOperation.UnlockBuffer();
  
      lvItem.pszText      = strField2.LockBuffer();
      lvItem.iSubItem = 2;
      m_lstDependencies.SetItem(&amp;lvItem, m_pCmbFields);
      strField2.UnlockBuffer();
  
      ResizeColumnWidths();
      UpdateWindow();
  
      return lvItem.iItem;
  }
  </code></pre>