Welcome to WindowsClient.net | Sign in | Join

Here are some frequently asked questions about Windows Forms and their answers.

Windows Forms FAQs

Browse by Tags

All Tags » ListBox (RSS)


  • How do I implement an owner-drawn ListBox?

    See Creating an Owner-Drawn Listbox on GotDotNet . It derives from Form and implements the owner drawing by handling the DrawItem event and MeasureItem event. You can also download a sample that implements an owner drawn listbox by deriving from ListBox...
  • How do I implement drag and drop between ListBoxes?

    The code below minimally handles D&D for a single selection list box by handling four events. The D&D is initiated in MouseDown if the user mouses down on the current selection. The DragEnter event is used to set the dragging effect to copy if...
  • How do I set the width of a ListBox to fit the text?

    You can iterate through the list to find the longest text extent using MeasureString, adding a fudge factor if the is present. using ( System.Drawing.Graphics g = listBox1.CreateGraphics() ) { float maxWidth = 0f; float height = 0f; for ( int i = 0; i...
  • How can I drag file names from Windows Explorer and drop them into a ListBox?

    Place a ListBox on your form, set its AllowDrop property and handle both DragEnter and DragDrop as below. private void listBox1_DragEnter( object sender, DragEventArgs e ) { if ( e.Data.GetDataPresent( DataFormats.FileDrop ) ) e.Effect = DragDropEffects...
  • How do I clear the contents of a ListBox?

    You have to access the Items of the ListBox using the Items property and clear (or otherwise operate on the items) these. listBox1.Items.Clear(); Contributed from George Shepherd's Windows Forms FAQ
  • How do I add and remove items from a ListBox?

    Check out Devinder Arora's article Using ListBox Control in C# on C# Corner . Contributed from George Shepherd's Windows Forms FAQ
  • How do I create an editable ListBox with an in-place TextBox and Button?

    The attached EditableList UserControl implements an editable ListBox with an in-place TextBox and Button allowing users to directly edit the contents of the list box. When the user clicks on a selected item, a textbox and a button is shown over the selected...