53 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			53 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Linq;
 | 
						|
using System.Text;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using System.Windows.Forms;
 | 
						|
 | 
						|
namespace Volian.Controls.Library
 | 
						|
{
 | 
						|
    //Class to handle bug in Windows WinForms ListBox
 | 
						|
    // that autoselects first item when no items are selected 
 | 
						|
    public partial class ListBoxMulti : ListBox
 | 
						|
    {
 | 
						|
        public ListBoxMulti()
 | 
						|
        {
 | 
						|
            Visible = true;
 | 
						|
            SelectionMode = SelectionMode.MultiSimple;
 | 
						|
 | 
						|
            SelectedIndexChanged += lb_SelectedIndexChanged;
 | 
						|
 | 
						|
            Disposed += ListBoxMulti_Disposed;
 | 
						|
        }
 | 
						|
 | 
						|
        //singleselectedindex
 | 
						|
        // will help to fix bug in Winforms ListBox
 | 
						|
        // that autoselects first item when no items are selected
 | 
						|
        // -1 = multi or set to this after 1st initialization
 | 
						|
        // if this = 0 and only 1 item selected,
 | 
						|
        // that means item was autoselected, so clear all items.
 | 
						|
        public int singleselectedindex { get; set; }
 | 
						|
        private void lb_SelectedIndexChanged(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            ListBoxMulti tmp = (ListBoxMulti)sender;
 | 
						|
 | 
						|
            if (tmp.SelectedItems.Count == 1 && tmp.singleselectedindex == 0)
 | 
						|
            {
 | 
						|
                tmp.ClearSelected();
 | 
						|
            }
 | 
						|
            else if (tmp.SelectedItems.Count == 1)
 | 
						|
                tmp.singleselectedindex = tmp.SelectedIndex;
 | 
						|
            else
 | 
						|
                tmp.singleselectedindex = -1;
 | 
						|
 | 
						|
        }
 | 
						|
 | 
						|
        //remove event when get rid of object
 | 
						|
        private void ListBoxMulti_Disposed(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            SelectedIndexChanged -= lb_SelectedIndexChanged;
 | 
						|
        }
 | 
						|
    }
 | 
						|
}
 |