Laden...

VirtualListBox (virtuelle ListBox)

Erstellt von dr4g0n76 vor 14 Jahren Letzter Beitrag vor 14 Jahren 5.887 Views
dr4g0n76 Themenstarter:in
2.921 Beiträge seit 2005
vor 14 Jahren
VirtualListBox (virtuelle ListBox)

Beschreibung:

Virtuelle ListBox, die Texte schneller anzeigt als eine Standard-ListBox


	public class VirtualListBox : ListBox
	{
		/*
		* Listbox Styles
		*/
		private const int LBS_NOTIFY = 0x0001;
		private const int LBS_SORT = 0x0002;
		private const int LBS_NOREDRAW = 0x0004;
		private const int LBS_MULTIPLESEL = 0x0008;
		private const int LBS_OWNERDRAWFIXED = 0x0010;
		private const int LBS_OWNERDRAWVARIABLE = 0x0020;
		private const int LBS_HASSTRINGS = 0x0040;
		private const int LBS_USETABSTOPS = 0x0080;
		private const int LBS_NOINTEGRALHEIGHT = 0x0100;
		private const int LBS_MULTICOLUMN = 0x0200;
		private const int LBS_WANTKEYBOARDINPUT = 0x0400;
		private const int LBS_EXTENDEDSEL = 0x0800;
		private const int LBS_DISABLENOSCROLL = 0x1000;
		private const int LBS_NODATA = 0x2000;

		private const int LB_GETCOUNT = 0x018B;
		private const int LB_SETCOUNT = 0x01A7;

		private const int LB_GETCURSEL = 0x188;
		private const int LB_SETCURSEL = 0x186;
		private const int CB_SETCURSEL = 0x14E;
		private const int LB_SETSEL = 0x185;
		private const int LB_GETSEL = 0x187;
		private const int LB_GETSELCOUNT = 0x190;
		private const int LB_GETSELITEMS = 0x191;
		private const int LB_ERR = -1;


		public VirtualListBox()
		{
			this.DrawMode = DrawMode.OwnerDrawFixed;
			this.Sorted = false;
			this.ItemHeight = 15;
		}

		[DllImport("user32", CharSet = CharSet.Auto)]
		private extern static int SendMessage(
		   IntPtr hWnd, int msg, int wParam, IntPtr lParam);

		/// <summary>
		/// Gets or sets the number of virtual items in the ListBox.
		/// </summary>
		public int Count
		{
			get
			{
				return SendMessage(this.Handle,
				   LB_GETCOUNT, 0, IntPtr.Zero);
			}
			set
			{
				SendMessage(this.Handle,
				   LB_SETCOUNT, value, IntPtr.Zero);
			}
		}

		/// <summary>
		/// Sets up the <see cref="CreateParams" /> object to tell 
		/// Windows how the ListBox control should be created.  In 
		/// this instance the default configuration is modified to 
		/// remove <c>LBS_HASSTRINGS</c> and <c>LBS_SORT</c> 
		/// styles and to add <c>LBS_NODATA</c>and LBS_OWNERDRAWFIXED 
		/// styles. This converts the ListBox into a Virtual ListBox.
		/// </summary>
		protected override System.Windows.Forms.CreateParams CreateParams
		{
			get
			{
				CreateParams defParams = base.CreateParams;
				defParams.Style = defParams.Style & ~LBS_HASSTRINGS;
				defParams.Style = defParams.Style & ~LBS_SORT;
				defParams.Style = defParams.Style |
					LBS_OWNERDRAWFIXED | LBS_NODATA;
				return defParams;
			}
		}

		protected override void OnDrawItem(DrawItemEventArgs e)
		{
			//has to be empty here.
		}

		/// <summary>
		/// Throws an exception.  All the items for a Virtual ListBox 
		/// are externally managed.
		/// </summary>
		/// <remarks>The selected index can be obtained using 
		/// the <see cref="SelectedIndex"/> and
		/// <see cref="SelectedIndices"/> properties.
		/// </remarks>
		/*[BrowsableAttribute(false)]
		public new SelectedObjectCollection SelectedItems
		{
			get
			{
				throw new InvalidOperationException(
				   "A Virtual ListBox does not have a " +
				   "SelectedObject collection");
			}
		}  */

		/// <summary>
		/// Gets/sets the selected index in the control.  If the 
		/// control has the multi-select style, then the first 
		/// selected item is returned.
		/// </summary>
		public override int SelectedIndex
		{
			get
			{
				int selIndex = -1;
				if (SelectionMode == SelectionMode.One)
				{
					selIndex = SendMessage(this.Handle,
					   LB_GETCURSEL, 0, IntPtr.Zero);
				}
				else if ((SelectionMode == SelectionMode.MultiExtended) ||
				   (SelectionMode == SelectionMode.MultiSimple))
				{
					int selCount = SendMessage(this.Handle,
					   LB_GETSELCOUNT, 0, IntPtr.Zero);
					if (selCount > 0)
					{
						IntPtr buf = Marshal.AllocCoTaskMem(4);
						SendMessage(this.Handle, LB_GETSELITEMS, 1, buf);
						selIndex = Marshal.ReadInt32(buf);
						Marshal.FreeCoTaskMem(buf);
					}
				}
				return selIndex;
			}
			set
			{
				if (SelectionMode == SelectionMode.One)
				{
					SendMessage(this.Handle,
					   LB_SETCURSEL, value, IntPtr.Zero);
				}
				else if ((SelectionMode == SelectionMode.MultiExtended) ||
				   (SelectionMode == SelectionMode.MultiSimple))
				{
					// clear any existing selection:
					SendMessage(this.Handle, LB_SETSEL, 0, new IntPtr(-1));
					// set the requested selection
					SendMessage(this.Handle, LB_SETSEL, 1, (IntPtr)value);
				}
			}
		}      

Schlagwörter: Virtuelle ListBox

Seit der Erkenntnis, dass der Mensch eine Nachricht ist, erweist sich seine körperliche Existenzform als überflüssig.

3.971 Beiträge seit 2006
vor 14 Jahren
public new int SelectedIndex  

Warum verwendest du hier new und nicht override?

Es gibt 3 Arten von Menschen, die die bis 3 zählen können und die, die es nicht können...

dr4g0n76 Themenstarter:in
2.921 Beiträge seit 2005
vor 14 Jahren

Danke für den Hinweis. wird abgeändert.

Die Methode wurde mit DevExpress erstellt. Ich hab aber nicht nachgeprüft, ob sich SelectedIndex überschreiben lässt.

Edit: Abgeändert.

Seit der Erkenntnis, dass der Mensch eine Nachricht ist, erweist sich seine körperliche Existenzform als überflüssig.