public string[] files, paths;
public void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
files = openFileDialog1.SafeFileNames; // Save only the names
paths = openFileDialog1.FileNames; // Save the full paths
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(files[i]); // Add songs to the listbox
}
}
}
这里listbox1(播放列表)填充打开对话框,listbox1显示并与我的播放器配合良好。我需要一个函数移动并复制到listbox2(收藏夹),我的字符串值来自listbox1(播放列表)
public string[] files, paths;
我还需要使用我的字符串文件和路径从新值listbox2(收藏夹)移动和复制到播放列表的功能
这是一个使用winddows 7媒体播放器WMPLib库的mp3播放器。我需要功能来修改这两个播放列表的意思与收藏夹和播放列表...... 非常感谢你
public void MoveListBoxItems(ListBox files, ListBox files2)
{
ListBox.SelectedObjectCollection sourceItems = files.SelectedItems;
foreach (var Items in sourceItems)
{
files2.Items.Add(Items);
}
while (files.SelectedItems.Count > 0)
{
files.Items.Remove(files.SelectedItems[0]);
}
}
此处按钮单击
MoveListBoxItems(listBox1, listBox2);
这个功能我使用但不工作当我一起交换列表时listbox1工作的第一个值意味着在播放器上播放
这里的球员
private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = paths[listBox2.SelectedIndex];
}
答案 0 :(得分:0)
老实说,从您的描述中很难理解您的问题,但听起来好像您的列表框包含对其数据存储在两个单独数组(文件名和文件路径)中的项的引用;您将两个数组之一的条目放入列表框(文件名),并对所选项与第二个数组中相应索引之间的关联感到困惑。
如果您只需要在listBox1
中创建与所选索引相对应的数组,则可以执行以下操作:
var selectedFiles = listBox1.SelectedIndices.Cast<int>().Select(index => files[index]).ToArray();
var selectedPaths = listBox1.SelectedIndices.Cast<int>().Select(index => paths[index]).ToArray();
然而,一个更好的解决方案是用表示所有必要数据的类数组替换这对数组:
public class FileAndPath : IEquatable<FileAndPath>
{
public FileAndPath(string file, string path)
{
this.File = file;
this.Path = path;
}
public FileAndPath(string path)
{
File = System.IO.Path.GetFileName(path);
Path = path;
}
public string File { get; set; }
public string Path { get; set; }
public override string ToString()
{
return File == null ? string.Empty : File;
}
public override bool Equals(object obj)
{
if (object.ReferenceEquals(this, obj))
return true;
if (obj == null)
return false;
if (obj.GetType() != GetType())
return false;
var other = (FileAndPath)obj;
return File == other.File && Path == other.Path;
}
public override int GetHashCode()
{
int code = 0;
if (File != null)
code ^= File.GetHashCode();
if (Path != null)
code ^= Path.GetHashCode();
return code;
}
#region IEquatable<FileAndPath> Members
public bool Equals(FileAndPath other)
{
return Equals((object)other);
}
#endregion
public static bool operator ==(FileAndPath first, FileAndPath second)
{
if (object.ReferenceEquals(first, second))
return true;
else if (object.ReferenceEquals(first, null))
return false;
return first.Equals(second);
}
public static bool operator !=(FileAndPath first, FileAndPath second)
{
return !(first == second);
}
}
通过覆盖ToString()
,您可以确保文件名显示在列表框中。通过在类中保持名称和路径,您可以确保在列表之间移动项目时永远不会松开关联:
您的MoveListBoxItems
变为:
public static void MoveListBoxItems(ListBox files1, ListBox files2)
{
var selections = files1.SelectedItems.Cast<FileAndPath>().ToList();
foreach (var selection in selections)
{
files2.Items.Add(selection);
files1.Items.Remove(selection);
}
}
listBox2_MouseDoubleClick
变为:
private void listBox2_MouseDoubleClick(object sender, MouseEventArgs e)
{
axWindowsMediaPlayer1.URL = ((FileAndPath)listBox2.SelectedItem).Path;
}
button1_Click
:
public void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
var files = openFileDialog1.SafeFileNames; // local variable only
var paths = openFileDialog1.FileNames; // local variable only
for (int i = 0; i < files.Length; i++)
{
listBox1.Items.Add(new FileAndPath(files[i], paths[i])); // Add songs to the listbox
}
}
}