ListBox1 Left Click List Duplicates - c#

I have the following issue: Once I click on the list it duplicates, unfortunately.
The list should not duplicate when it has been clicked.
There is a refresh button for refreshing the list - this is to be used only if there is something in a specific folder.
Code: C# .NET
https://gyazo.com/f7d026a956c648a1ecfd1a749a7c3b77
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\fakepath\Something");
FileInfo[] Files = dinfo.GetFiles("*.txt");
foreach (FileInfo file in Files)
{
listBox1.Items.Add(file.Name);
}
}

You could clear the element each time the an item is selected:
private void listBox1_SelectedIndexChanged(object sender, EventArgs e) {
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\Administrator\Desktop\Something\Something");
listBox1.Items.Clear();
// etc..

Related

File being used by another process when using File.CopyTo method

This is the code for my windows form. The goal of the form is to be able to preview a pdf on the application and then chose a folder with a drop down that you would like to copy the file to (this is what a "house" refers to in the code). This is a little project for work to make it simpler to organize files that get sent to the company. The listBox works populating with all the pdfs and the preview window works as well (this is the axAcroPDF component). But when hitting the send button to copy the file it says the file is being used by another process.
public partial class Form1 : Form { public Form1() { InitializeComponent();
}
string selectedPDF = "";
string selectedHouse = "";
DirectoryInfo currentPDF;
private void button1_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "PDF Files(*.pdf) |*.pdf;";
openFileDialog1.ShowDialog();
if(openFileDialog1.FileName != null)
{
axAcroPDF1.LoadFile(openFileDialog1.FileName);
}
}
private void refreshBTN_Click(object sender, EventArgs e)
{
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\bkamide\Desktop\ExamplePDFS");
FileInfo[] smFiles = dinfo.GetFiles("*.pdf");
foreach (FileInfo fi in smFiles)
{
pdfList.Items.Add(Path.GetFileName(fi.Name));
}
}
private void pdfList_SelectedIndexChanged(object sender, EventArgs e)
{
string firstSelectedItem = pdfList.SelectedItem.ToString();
selectedPDF = firstSelectedItem;
DirectoryInfo dinfo = new DirectoryInfo(#"C:\Users\bkamide\Desktop\ExamplePDFS\" + firstSelectedItem);
currentPDF = dinfo;
}
private void btnSend_Click(object sender, EventArgs e)
{
openFileDialog1.Reset();
axAcroPDF1.EndInit();
var sourceFile = new FileInfo(currentPDF.FullName);
var dest = Path.Combine(#"C:\Users\bkamide\Desktop\ExamplePDFS\", selectedHouse, sourceFile.FullName);
sourceFile.CopyTo(dest, true);
}
private void cbHouse_SelectedIndex(object sender, EventArgs e)
{
selectedHouse = cbHouse.SelectedIndex.ToString();
}
}
I tried in the send method to reset the openFileDialog and axAcroPDF (not sure if I did that right) to see if those were the processes that could be using the file. I also tried restarting my computer to make sure nothing in the background was using it as well. I also did try the using method but I was not quite sure how to implement it within this.

How to play a wav file from a ListBox?

Good afternoon. I'm making a small program on wpf. There is a ListBox in which wav files from a folder are loaded. There is a RadioButton (there are several of them), when you click on it, certain audio files are added to the list. But I do not know how to play the selected audio track when clicking on the wav element in the ListBox
SoundPlayer player;
string[] playList;
private void сmClassic(object sender, RoutedEventArgs e)
{
lbList.Items.Clear();
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\1395355\Desktop\Студент\C#\4_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
foreach (FileInfo fi in files)
{
lbList.Items.Add(fi.ToString());
}
string d = playList[lbList.SelectedIndex];
// player.Open(d); Yes, there is a mistake, but I was looking for an alternative
player.Play();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
}
}
Code from answer in original source.
private void сmClassic(object sender, RoutedEventArgs e)
{
DirectoryInfo dir = new DirectoryInfo(#"C:\Users\1395355\Desktop\Студент\C#\4_FourthProject\FourthProject\bin\Debug\music\classic");
FileInfo[] files = dir.GetFiles("*.wav");
lbList.ItemsSource = files;
}
private readonly SoundPlayer player = new SoundPlayer();
private void ListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
player.Stop();
if(lbList.SelectedItem is FileInfo file)
{
player.SoundLocation=file.FullName;
player.PlaySync();
}
else
{
player.SoundLocation=null;
}
}

Stuck with WMP in C#

I want to fetch 5 media files from a Directory [not with dialog box] and play with axwindowsmediaplayer in C# one after one. Songs to be played as playlist.
I am new to C#.
private void Form1_Load(object sender, EventArgs e)
{
/*
foreach(string dir in Directory.GetDirectories(#"E:\\Project"))
{
DirectoryInfo info = new DirectoryInfo(dir);
this.listBox1.Items.Add(info.Name);
}
foreach(string file in Directory.GetFiles("E:\\Project\\Surprise"))
{
FileInfo FileInfo = new FileInfo(file);
this.listBox1.Items.Add(FileInfo.Name);
}
*/
string path = #"E:\\Project\\Surprise";
foreach (string s in Directory.GetFiles(path, "*.mp4").Select(Path.GetFileName))
listBox1.Items.Add(s);
listBox1.SelectedItems.Clear();
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = listBox1.SelectedItem.ToString();
axWindowsMediaPlayer1.Ctlcontrols.play();
}

open pdf file selected in listbox

I have all the dirctories (2014, 2012), the files of each selected folder (.pdf) in the listbox 2
I get the dirctories by this code
if (FBD.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
DirectoryInfo[] diri_info = newDirectoryInfo(FBD.SelectedPath).GetDirectories();
foreach (DirectoryInfo diri in diri_info)
{
listBox1.Items.Add(diri);
}
and i get the files by this code
private void button1_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex >= 0)
{
DirectoryInfo dirictory_choisis = (DirectoryInfo)listBox1.SelectedItem;
FileInfo[] files = dirictory_choisis.GetFiles();
listBox2.Items.Clear();
foreach (FileInfo file in files)
{
listBox2.Items.Add(file);
}
}
else
{
MessageBox.Show("selectioner un dossier");
}
}
Now how I can open the selected file (.pdf) ?
i use this code but dosn't work ( throw an exception file dosn't found)
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo) listBox2.SelectedItem;
Process.Start(file.Name);
}
There is a syntax error in your code: "newDirectoryInfo"
By the way, file.Name only returns the name (not including path). You should replace that line with:
Process.Start(file.FullName);
So the listBox2_Click should be like this:
private void listBox2_Click(object sender, EventArgs e)
{
FileInfo file = (FileInfo)listBox2.SelectedItem;
Process.Start(file.FullName);
}

Using a ComboBox selection as directory source for another ComboBox, C#

So I have created a form that creates both a .txt file and a directory with the same name under the directory C:\Modules.
I have made it possible to select the .txt files from ModuleSelectorComboBox and now what I am having trouble getting to work is using the name of the file selected in ModuleSelectorComboBox to form part of the name of the directory in NoteSelectorComboBox.
public Default()
{
InitializeComponent();
System.IO.Directory.CreateDirectory(#"C:\Modules");
string[] files = Directory.GetFiles(#"C:\Modules");
foreach (string file in files)
ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void moduleToolStripMenuItem_Click(object sender, EventArgs e)
{
NewModule newmodule = new NewModule();
newmodule.Show();
}
private void ModuleSelectorComboBox_SelectedValueChanged(object sender, EventArgs e)
{
richTextBox1.Clear(); //Clears previous Modules Text
string ModulefileName = (string)ModuleSelectorComboBox.SelectedItem;
string filePath = Path.Combine(#"C:\Modules\", ModulefileName + ".txt");
if (File.Exists(filePath))
richTextBox1.AppendText(File.ReadAllText(filePath));
else
MessageBox.Show("There's been a problem. Please restart the program. \nError 1", "Error 1", //error 1 is file deleted while the program is running
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1);
}
private void button1_Click(object sender, EventArgs e)
{
ModuleSelectorComboBox.Items.Clear();
string[] files = Directory.GetFiles(#"C:\Modules");
foreach (string file in files)
ModuleSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}
private void NoteSelectorComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
string ModulefileName = (string)ModuleSelectorComboBox.SelectedItem;
string[] files = Directory.GetFiles(#"C:\Modules\" + ModulefileName); //THIS IS WHAT I HAVE TRIED BUT CANNOT GET TO WORK
foreach (string file in files)
NoteSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}
}
So if 'Module 1' is selected in the ModuleSelectorComboBox the directory listing for NoteSelectorComboBox will be set to C:\Modules\Module 1 (ie. C:\Modules\<NAME OF SELECTED MODULE From ModuleSelectorComboBox> and so the files in that folder would be shown in the ComboBox.
I have created an OnClick(); event that has now solved this issue.
private void button2_Click(object sender, EventArgs e)
{
string fileName = (string)ModuleSelectorComboBox.SelectedItem;
NoteSelectorComboBox.Items.Clear();
string[] files = Directory.GetFiles(#"C:\Modules\" + (string)ModuleSelectorComboBox.SelectedItem);
foreach (string file in files)
NoteSelectorComboBox.Items.Add(Path.GetFileNameWithoutExtension(file));
}

Categories

Resources