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


C# Examples » GUI Windows Forms » ListBox »

 

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(8,  8);
                this.listBox1.Name  =  "listBox1";
                this.listBox1.Size  =  new  System.Drawing.Size(248,  160);
                this.listBox1.TabIndex  =  0;

                this.cmdTest.Location  =  new  System.Drawing.Point(12,  184);
                this.cmdTest.Name  =  "cmdTest";
                this.cmdTest.Size  =  new  System.Drawing.Size(132,  28);
                this.cmdTest.TabIndex  =  1;
                this.cmdTest.Text  =  "Test  Scroll";
                this.cmdTest.Click  +=  new  System.EventHandler(this.cmdTest_Click);

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

        }
}
    
   
  
   




HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo GUI Windows Forms
» ListBox