0

我对这件事很陌生。其实这是我的第一部作品。我想要一个从文本框中读取随机文件计数的程序。它有一个按钮可以从选定的路径随机化文件。我需要在列表框中打开文件。

我的问题是当我双击列表框时,无论我点击什么文件,它都会打开列表中的最后一个文件。我试图在下面添加两个斜杠的行。但它也没有奏效。我能做些什么?

public Form1()
    {
        InitializeComponent();
    }
    Random r = new Random();
    string path1;           
    DirectoryInfo dif;
    FileInfo[] files;             
   int randomchoose;
      //FileInfo[] files2; 
     //int hoho; 
    int[] randomcount;

    private void button1_Click(object sender, EventArgs e)
    {

        FolderBrowserDialog hoho = new FolderBrowserDialog();  
        hoho.ShowNewFolderButton = true;

        if (hoho.ShowDialog() == DialogResult.OK) 
        {
            path1 = hoho.SelectedPath; 
            textBox1.Text = path1;
            dif = new DirectoryInfo(path1);  
            files = dif.GetFiles();              
        }

    }

    private void btnrasgele_Click(object sender, EventArgs e)
    {           
        randomcount = new int[Convert.ToInt32(textBox3.Text)];
      // int hoho=0;
        foreach (int k in randomcount)
        {               
            int pd = files.Length;

            randomchoose = r.Next(0, Convert.ToInt32(pd + 1));
            listBox1.Items.Add(files[randomchoose]); 
       //files2[hoho] = files[randomchoose].FullName;           
            }
        }

    private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
    {

         //listBox1.SelectedIndex = hoho;
         //Process.Start(files2[hoho].FullName);
          Process.Start(files[randomchoose].FullName);

    }
4

1 回答 1

0

你传入了randomchoose在那个时候是固定的,试试这个:

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
   if(listBox1.SelectedItem != null)
      Process.Start(((FileInfo)listBox1.SelectedItem).FullName);
}
于 2013-09-22T19:15:19.687 回答