Hi @karam bultaif , Welcome to Microsoft Q&A.
Your code has several problems.
You could not use arrays like this in c#.
C# arrays are not mutable.
Use List<string> instead of array.
And when you use Name_File.Items.RemoveAt(Name_File.SelectedIndex); it fires Name_File.Items.RemoveAt(Name_File.SelectedIndex);.
Reckless judgment has to be added to it.
The following is my simplified code, which works after my tests. for reference only:
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace _8_xx_xx
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<string> paths, files;
private void SafeButton_Click(object sender, System.EventArgs e)
{
int index = Name_File.SelectedIndex;
if (index == -1)
return;
Name_File.Items.RemoveAt(index);
files.RemoveAt(index);
paths.RemoveAt(index);
}
private void Open_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Multiselect = true;
if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//First time
if (files == null || paths == null)
{
files = ofd.SafeFileNames.ToList();
paths = ofd.FileNames.ToList();
}
//Not first time
else
{
files.AddRange(ofd.SafeFileNames.ToList());
paths.AddRange(ofd.FileNames.ToList());
}
}
Name_File.Items.Clear();
for (int lc = 0; lc < files.Count; lc++)
{
Name_File.Items.Add(files[lc]);
}
}
private void Name_File_SelectedIndexChanged(object sender, System.EventArgs e)
{
//Check if there is a selected item
if (Name_File.SelectedIndex == -1)
return;
Player.URL = paths[Name_File.SelectedIndex];
Player.Ctlcontrols.play();
}
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.