Hi,

I have the listbox filled with data like : /abc/xyz/inst1
I need to view the complete path in the listbox.but there is no autosize property for a listbox.
How can i view the complete path ?

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace daniweb
{
  public partial class frmListBox : Form
  {
    public frmListBox()
    {
      InitializeComponent();
    }

    private void frmListBox_Load(object sender, EventArgs e)
    {
      PopulateDefaults();
    }

    private void PopulateDefaults()
    {
      listBox1.BeginUpdate();
      listBox1.Items.Clear();
      for (int i1 = 0; i1 < 10; i1++)
      {
        string s = i1.ToString() + "|" + Guid.NewGuid().ToString();
        if ((i1 % 5) == 0)
          s += Guid.NewGuid().ToString() + Guid.NewGuid().ToString();
        listBox1.Items.Add(s);
      }
      listBox1.EndUpdate();
    }

    private void AutoFit()
    {
      if (listBox1.Items.Count == 0)
        return;
      int width = listBox1.Width;
      using (Graphics g = listBox1.CreateGraphics())
      {
        for (int i1 = 0; i1 < listBox1.Items.Count; i1++)
        {
          int itemWidth = Convert.ToInt32(g.MeasureString(Convert.ToString(listBox1.Items[i1]), listBox1.Font).Width);
          width = Math.Max(width, itemWidth);
        }
      }
      listBox1.Width = width;
    }

    private void button1_Click(object sender, EventArgs e)
    {
      AutoFit();
    }


  }
}
This article has been dead for over six months. Start a new discussion instead.