1

画像のファイル名を表示するlstFilesという名前をListBox付けました。リスト ボックスから選択すると、マウスまたはキーボードから選択できます。

画像はPictureBox pictureBox1内に表示されますが、最後のエントリがリストされた後、トップに戻ろうとして問題が発生しListBoxています。最後のエントリでキーボードの下矢印を選択し、トップを持っている場合エントリが選択されている場合、最初のエントリで上矢印キーを押すと、同じように一番下のエントリに移動します。

試してみましたが、リストボックス内で動作させることができません

システムドライブ、フォルダー、およびそのコンテンツを表示するための3つの共同リストボックスがあります

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;

        }
      }
   }
4

1 に答える 1

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;
    }
于 2013-02-09T19:37:26.143 に答える