简体   繁体   English

C#列表框-播放第一个选择的项目

[英]c# listbox - first selected item is played

Here is my problem: I have a TextBox, button, and a ListBox. 这是我的问题:我有一个TextBox,按钮和一个ListBox。 The functions works right but whenever I search flash videos, the moment I click the search button, the first video on the list is played. 该功能正常运行,但是每当我搜索Flash视频时,单击搜索按钮时,就会播放列表中的第一个视频。 I don't want it to be played on that time - I want to choose from the list without playing the first highlighted item on the ListBox. 我不想在那段时间播放它-我想从列表中选择而不播放ListBox上的第一个突出显示的项目。

Here is my code: 这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    var path = "C:\\Users\\John\\Desktop\\Video\\FLASH";
    listBox1.DataSource = Directory.GetFiles(path, "*" + txtbox1.Text + "*")
                       .Select(f => Path.GetFileName(f))
                       .ToList();
}

This is the search button. 这是搜索按钮。 It will search the text on textbox1 from the specified path: 它将从指定路径搜索textbox1上的文本:

private void listBox1_DoubleClick(object sender, EventArgs e)
{
    var fileName = listBox1.SelectedItem as string;
    if (fileName != null)
    {
        var path = Path.Combine("C:\\Users\\John\\Desktop\\Video\\FLASH", fileName);
        Process.Start(path);
    }    
}

This is the ListBox, the searched items will be here, but there is always one item selected and that item plays whenever the search finished. 这是ListBox,搜索到的项目将在此处,但是始终选择一个项目,并且只要搜索完成,该项目就会播放。

Fill the list, then add the selectedindexchanged event handler. 填写列表,然后添加selectedindexchanged事件处理程序。 (Make sure that the event isn't added for you by the designer). (确保设计师没有为您添加活动)。

So, listBox1.DataSource = ... 因此,listBox1.DataSource = ...

listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged); listBox1.SelectedIndexChanged + =新的System.EventHandler(this.listBox1_SelectedIndexChanged);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM