2013-02-09 87 views
1

我有ListBox名爲lstFiles顯示圖像的文件名,然後從列表框中選擇時,從鼠標或鍵盤。滾動到列表框上的頂部和底部

的圖像將然後PictureBoxpictureBox1內被顯示,但我有麻煩試圖使ListBox回到頂部最後一個條目已經上市後,如果您選擇了向下箭頭的鍵盤上最後一個條目,並選擇最上面的條目,我希望當您按下第一個條目上的向上箭頭鍵時,同樣會進入底部條目。

我都試過,不能讓它列表框內工作

我有三個聯合列表框來顯示系統驅動器,文件夾及其內容

private void lstDrive_SelectedIndexChanged_1(object sender, EventArgs e) 
     { 
      lstFolders.Items.Clear(); 
     try 
     { 
      DriveInfo drive = (DriveInfo)lstDrive.SelectedItem; 

      foreach (DirectoryInfo dirInfo in drive.RootDirectory.GetDirectories()) 
       lstFolders.Items.Add(dirInfo); 
     } 
     catch (Exception ex) 
     { 
      MessageBox.Show(ex.Message); 
     } 
    } 

    private void lstFolders_SelectedIndexChanged_1(object sender, EventArgs e) 
    { 
     lstFiles.Items.Clear(); 

     DirectoryInfo dir = (DirectoryInfo)lstFolders.SelectedItem; 

     foreach (FileInfo fi in dir.GetFiles()) 
      lstFiles.Items.Add(fi); 
    } 

    private void lstFiles_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     pictureBox1.Image = Image.FromFile(((FileInfo)lstFiles.SelectedItem).FullName); 

     //I have tried this, but it makes the selected cursor go straight to the bottom file// 
     lstFiles.SelectedIndex = lstFiles.Items.Count - 1; 

     } 
     } 
    } 
+0

這有幫助嗎? http://stackoverflow.com/questions/8796747/how-to-scroll-to-bottom-of-listbox – Haedrian 2013-02-09 16:49:47

回答

2

你可以做到這一點通過處理ListBox KeyUp事件。試試這個:

private int lastIndex = 0; 

    private void listBox1_KeyUp(object sender, KeyEventArgs e) 
    { 

     if (listBox1.SelectedIndex == lastIndex) 
     { 
      if (e.KeyCode == Keys.Up) 
      { 
       listBox1.SelectedIndex = listBox1.Items.Count - 1; 
      } 

      if (e.KeyCode == Keys.Down) 
      { 
       listBox1.SelectedIndex = 0; 
      }     

     } 

     lastIndex = listBox1.SelectedIndex; 
    } 
+0

非常感謝你這完美的工作^ _ ^ – magi4000 2013-02-09 22:11:36