私が問題を再現できないため、現在私はその点であなたを助けることはできません。しかし、私はあなたが合うと思うところであなたを助けることができます。
現在のDropメソッドには、リストボックスに追加するアイテムの数を倍にする余分なループがあります。
あなたの現在の方法:(のForEachを使用して)
private void listbox4_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
//if you keep this loop, you will all the dropped files for each dropped file
//therefore, if I dropped 3 files, I'd get 9 entries in the listbox
//if I dropped 4 files, I'd get 16 entries and so on...
for (int i = 0; i < droppedFilePaths.Length; i++)//this has to go
{//this has to go
var filePath = droppedFilePaths[i];//this needs to be a different variable since "i" will no longer exist
var fileName = System.IO.Path.GetFileName(filePath);
//fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}//this has to go
}
}
}
リファクタリング
private void blaze_125_listbox4_Drop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
var filePath = droppedFilePath;
var fileName = System.IO.Path.GetFileName(filePath);
//fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}
これはまた、(ForLoopを使用して)うまくいく
private void blaze_125_listbox4_Drop_anotherSpin(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
for (int i = 0; i < droppedFilePaths.Length; i++)
{
var filePath = droppedFilePaths[i];
var fileName = System.IO.Path.GetFileName(filePath);
//fileDictionary.Add(fileName, filePath);
listbox4.Items.Add(fileName);
listbox4.SelectedItem = fileName;
}
}
}
スリム
private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));
}
}
}
選択を維持するか、または最後を選択する
private void UpdateTheListboxMaintainExistingSelection(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
{
var preSelectedItem = listboxToModify.SelectedItem;//store the current selection
listboxToModify.Items.Clear();//clear all the items in the list
foreach (KeyValuePair<string, string> item in incomingDictionary)
{
listboxToModify.Items.Add(item.Key);
}
//this method should probably be optimized because if your listBox already contains a large number of items
//it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
//Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
//Maintain the selected item if there was one
if (preSelectedItem != null)
{
listboxToModify.SelectedItem = preSelectedItem;
}
}
選択された項目がある場合、選択された項目を維持するために、リスト
Dictionary<string, string> fileDictionary = new Dictionary<string, string>();
private void blaze_125_listbox4_Drop_Slimmer(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
{
string[] droppedFilePaths =
e.Data.GetData(DataFormats.FileDrop, true) as string[];
foreach (string droppedFilePath in droppedFilePaths)
{
//listbox4.Items.Add(System.IO.Path.GetFileName(droppedFilePath));//don't need this anymore
//Check if the file is already in the dictionary.
//Check by looking up the key, and by looking up the value too.
if (fileDictionary.ContainsKey(System.IO.Path.GetFileName(droppedFilePath)) || fileDictionary.ContainsValue(droppedFilePath))
{
//no need to add this file, it's already in the dictionary
//if you try to add a file with a KEY identical to a KEY that already exists in the dictionary,
//it will throw an exception
//A dictionary can contain the same value multiple times, but it can not contain the same key more than once.
}
else
{
//the file is not listed in the dictionary, so lets add it
fileDictionary.Add(System.IO.Path.GetFileName(droppedFilePath), droppedFilePath);
}
}
}
//Now lets call the method in charge of updating the listbox
UpdateTheListbox(fileDictionary, listbox4);
}
private void UpdateTheListbox(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
{
listboxToModify.Items.Clear();//clear all the items in the list
foreach (KeyValuePair<string, string> item in incomingDictionary)
{
listboxToModify.Items.Add(item.Key);
}
//this method should probably be optimized because if your listBox already contains a large number of items
//it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
//Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
}
更新方法アイテムを格納するために辞書を使用し、そして更新します項目が選択されていない場合
private void UpdateTheListboxMaintainExistingOrSelectLastAdded(Dictionary<string, string> incomingDictionary, ListBox listboxToModify)
{
var preSelectedItem = listboxToModify.SelectedItem;//store the current selection
listboxToModify.Items.Clear();//clear all the items in the list
foreach (KeyValuePair<string, string> item in incomingDictionary)
{
listboxToModify.Items.Add(item.Key);
}
//this method should probably be optimized because if your listBox already contains a large number of items
//it may be quicker to only add the missing items, instead of reverting back to an empty list, and adding all the items to it again.
//Though I'll leave this up to you to implement. We'll be here to answer questions if you have a hard time doing it.
if (preSelectedItem != null)
{
//Maintain the selected item if there was one
listboxToModify.SelectedItem = preSelectedItem;
}
else
{
//select the last item in the listbox if nothing was pre-selected
listboxToModify.SelectedItem = listboxToModify.Items[listboxToModify.Items.Count - 1];
}
}
新しいWPFプロジェクトにコードを貼り付け、複数のファイルをドロップすると、「うまくいった」と言われました。あなたのコードは微調整が必要ですが、全体的には "うまくいっています"。だからあなたはどんな問題に直面しているのか分かりません。 –