System.Windows.Forms.ListBox.EnsureVisible C# (CSharp) Method

EnsureVisible() public method

public EnsureVisible ( int index ) : void
index int
return void
		void EnsureVisible (int index)
		{
			if (!IsHandleCreated || index == -1)
				return;

			if (index < top_index) {
				top_index = index;
				UpdateTopItem ();
				Invalidate ();
			} else if (!multicolumn) {
				int rows = items_area.Height / ItemHeight;
				rows = rows == 0 ? 1 : rows;
				if (index >= (top_index + rows))
					top_index = index - rows + 1;

				UpdateTopItem ();
			} else {
				int rows = Math.Max (1, items_area.Height / ItemHeight);
				int cols = Math.Max (1, items_area.Width / ColumnWidthInternal);
				
				if (index >= (top_index + (rows * cols))) {
					int incolumn = index / rows;
					top_index = (incolumn - (cols - 1)) * rows;

					UpdateTopItem ();
					Invalidate ();
				}
			}
		}

Usage Example

Example #1
0
        private void CreateListBox(string listFilter)
        {
            if (listBox != null)
            {
                listBox = null;
                return;
            }

            if (!listBoxOpened)
            {
                listBox                = new ListBox();
                listBox.Font           = Font;
                listBox.uwfContext     = true;
                listBox.Width          = Width;
                listBox.ItemHeight     = ItemHeight;
                listBox.Height         = listBox.ItemHeight * (Items.Count > MaxDropDownItems ? MaxDropDownItems : Items.Count);
                listBox.uwfWrapText    = false;
                listBox.uwfShadowBox   = true;
                listBox.uwfBorderColor = listBox.uwfBorderSelectColor;
                if (listBox.Height < listBox.ItemHeight)
                {
                    listBox.Height = listBox.ItemHeight;
                }

                bool selectedIndexChanged = false;
                for (int i = 0; i < Items.Count; i++)
                {
                    var item = Items[i];
                    if (DropDownStyle == ComboBoxStyle.DropDownList || String.IsNullOrEmpty(listFilter))
                    {
                        listBox.Items.Add(item);
                    }
                    else
                    {
                        var itemString = item.ToString();
                        if (!itemString.ToLower().Contains(listFilter.ToLower()))
                        {
                            continue;
                        }

                        listBox.Items.Add(item);
                        if (itemString != listFilter)
                        {
                            continue;
                        }

                        listBox.SelectedIndex = i;
                        selectedIndexChanged  = true;
                    }
                }
                for (int i = 0; i < Items.Count; i++)
                {
                    if (Items.IsDisabled(i))
                    {
                        listBox.Items.Disable(i);
                    }
                }

                if (selectedIndexChanged == false)
                {
                    listBox.SelectedIndex = SelectedIndex;
                    listBox.EnsureVisible();
                }

                var gpoint = PointToScreen(Point.Empty);
                listBox.Location  = new Point(gpoint.X, gpoint.Y + Height);
                listBox.MouseUp  += ListBoxOnMouseUp;
                listBox.KeyDown  += ListBoxOnKeyDown;
                listBox.Disposed += ListBoxOnDisposed;

                OnDropDown(EventArgs.Empty);
            }
            else
            {
                listBoxOpened = false;
            }
        }