Set the TopIndex property of the ListBox to ensure the most recently added items are visible : ListBox « GUI Windows Forms « C# / CSharp Tutorial

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » GUI Windows Forms » ListBox 
23.27.1.Set the TopIndex property of the ListBox to ensure the most recently added items are visible
Set the TopIndex property of the ListBox to ensure the most recently added items are visible
using System;
using System.Windows.Forms;

public class ListBoxItemVisibleFormDemo{
    [STAThread]
    public static void Main(string[] args)
    {
        Application.Run(new ListBoxItemVisibleForm());
    }
}

public partial class ListBoxItemVisibleForm : Form
{
    private int counter = 0;
    
    public ListBoxItemVisibleForm()
    {
        InitializeComponent();
    }

    private void cmdTest_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < 200; i++)
        {
            counter++;
            listBox1.Items.Add("Item " + counter.ToString());
        }


        listBox1.TopIndex = listBox1.Items.Count - 1;
        listBox1.SelectedIndex = listBox1.Items.Count - 1;

    }

}
partial class ListBoxItemVisibleForm
{
    private System.Windows.Forms.ListBox listBox1;
    private System.Windows.Forms.Button cmdTest;
    private System.ComponentModel.IContainer components = null;

    protected override void Dispose(bool disposing)
    {
        if (disposing && (components != null))
        {
            components.Dispose();
        }
        base.Dispose(disposing);
    }

    private void InitializeComponent()
    {
        this.listBox1 = new System.Windows.Forms.ListBox();
        this.cmdTest = new System.Windows.Forms.Button();
        this.SuspendLayout();

        this.listBox1.FormattingEnabled = true;
        this.listBox1.Location = new System.Drawing.Point(88);
        this.listBox1.Name = "listBox1";
        this.listBox1.Size = new System.Drawing.Size(248160);
        this.listBox1.TabIndex = 0;

        this.cmdTest.Location = new System.Drawing.Point(12184);
        this.cmdTest.Name = "cmdTest";
        this.cmdTest.Size = new System.Drawing.Size(13228);
        this.cmdTest.TabIndex = 1;
        this.cmdTest.Text = "Test Scroll";
        this.cmdTest.Click += new System.EventHandler(this.cmdTest_Click);

        this.AutoScaleDimensions = new System.Drawing.SizeF(6F13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(292246);
        this.Controls.Add(this.cmdTest);
        this.Controls.Add(this.listBox1);
        this.Name = "ListBoxItemVisibleForm";
        this.Text = "ListBoxItemVisibleForm";
        this.ResumeLayout(false);

    }
}
23.27.ListBox
23.27.1.Set the TopIndex property of the ListBox to ensure the most recently added items are visibleSet the TopIndex property of the ListBox to ensure the most recently added items are visible
23.27.2.Add Items to ListBoxAdd Items to ListBox
23.27.3.Add ContextMenu to ListBoxAdd ContextMenu to ListBox
23.27.4.ListBox Items AddListBox Items Add
23.27.5.ListBox Items Add a RangeListBox Items Add a Range
23.27.6.ListBox Selection ModeListBox Selection Mode
23.27.7.Use RadioButton to control ListBox selection modeUse RadioButton to control ListBox selection mode
23.27.8.ListBox Events: SelectedIndexChanged, SelectedValueChanged, DataSourceChanged, DisplayMemberChanged, ValueMemberChanged
23.27.9.Add text in the TextBox to the ListBox
23.27.10.Fills a ListBox with data from Database
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.