using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading; using System.Windows.Forms; using System.IO; using System.Media; using System.Diagnostics; namespace SoundBoard { public partial class formMain : Form { public SoundPlayer player = new SoundPlayer(); public Random randomNum = new Random(); public ListBox listBoxEntity = new ListBox(); public string _workingDirectory; public string[] _soundFiles; public string[] _soundDirectories; public bool willLoopSound = false; public formMain() { InitializeComponent(); } private void formMain_Load(object sender, EventArgs e) { _workingDirectory = Path.GetDirectoryName(Application.ExecutablePath); _soundFiles = Directory.GetFiles(_workingDirectory); foreach (string sound in _soundFiles) { CreateUITabs("Working Directory", _workingDirectory); } _soundDirectories = Directory.GetDirectories(_workingDirectory); foreach (string sounds in _soundDirectories) { CreateUITabs(Path.GetFileName(sounds), sounds); } fileSystemWatcherMain.Path = _workingDirectory; fileSystemWatcherMain.IncludeSubdirectories = true; fileSystemWatcherMain.Created += new FileSystemEventHandler(fileSystemUpdateDetected); //file or folder created fileSystemWatcherMain.Deleted += new FileSystemEventHandler(fileSystemUpdateDetected); //file or folder moved or deleted fileSystemWatcherMain.Renamed += new RenamedEventHandler(fileSystemUpdateDetected); //for any file changes like file extension changes soundboardTabControl.SelectedIndexChanged += new EventHandler(CheckForDisabledListBox); //disables ore enables the Show on Disk file menu object listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0]; if (listBoxEntity.Enabled == true) { listBoxEntity.SelectedIndex = 0; } } private void fileSystemUpdateDetected(object sender, FileSystemEventArgs e) { //perform the UI refresh operation RefreshUIControls(); } private void CheckForDisabledListBox(object sender, EventArgs e) { ListBox listBoxEntity = new ListBox(); try { listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0]; } catch (Exception spam) { return; } if (listBoxEntity.Enabled == false) { mainMenuFileShowOnDisk.Enabled = false; } else { listBoxEntity.SelectedIndex = 0; mainMenuFileShowOnDisk.Enabled = true; } } private void RefreshUIControls() { _workingDirectory = Path.GetDirectoryName(Application.ExecutablePath); _soundDirectories = Directory.GetDirectories(_workingDirectory); soundboardTabControl.Controls.Clear(); foreach (string sounds in _soundFiles) { CreateUITabs("Working Directory", _workingDirectory); } foreach (string files in _soundDirectories) { CreateUITabs(Path.GetFileName(files), files); } } private void CreateUITabs(string tabName, string directoryPath) { //add tabs dynamically int soundControlHeight = soundboardTabControl.Height; int soundControlWidth = soundboardTabControl.Width; string[] directoryFiles; //files in the target directory TabPage tab = new TabPage(); ListBox localListBox = new ListBox(); if (soundboardTabControl.Controls.ContainsKey(tabName.Replace(" ", "") + "Tab")) { } else { tab.Text = tabName; //display the folder's full name to the user tab.Name = tabName.Replace(" ", "") + "Tab"; //create a legal name for the tab's (name) property tab.Height = soundControlHeight - 20; tab.Height = soundControlWidth; soundboardTabControl.Controls.Add(tab); localListBox.Height = soundControlHeight - 20; localListBox.Width = soundControlWidth - 7; localListBox.Name = tabName.Replace(" ", "") + "ListBox"; tab.Controls.Add(localListBox); localListBox.Click += new EventHandler((sender, e) => ListBoxOnLeftClick(sender, e, localListBox)); //create event handler for the lsit box and pass the object in } directoryFiles = Directory.GetFiles(Path.GetFullPath(directoryPath)); foreach (string files in directoryFiles) { switch (Path.GetExtension(files))//check to make sure the file is a sound file { case ".wav": case ".mp3": localListBox.Items.Add(Path.GetFileNameWithoutExtension(files)); break; default: break; } } if (localListBox.Items.Count == 0) { localListBox.Items.Add("No sound files found in the " + tabName + " directory."); localListBox.Enabled = false; if (soundboardTabControl.SelectedIndex == 0) { mainMenuFileShowOnDisk.Enabled = false; } } } private void ListBoxOnLeftClick(object sender, EventArgs e, ListBox list) { //check to make sure something is actually selected if (list.SelectedItem == null) { return; } //get the file's location on disk, double back if it's not a .wav file string fileLocation; if (soundboardTabControl.SelectedIndex == 0) { fileLocation = _workingDirectory + "\\" + list.SelectedItem.ToString() + ".wav"; if (!File.Exists(fileLocation)) { fileLocation = _workingDirectory + "\\" + list.SelectedItem.ToString() + ".mp3"; } } else { fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + list.SelectedItem.ToString() + ".wav"; if (!File.Exists(fileLocation)) { fileLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + list.SelectedItem.ToString() + ".mp3"; } } if (File.Exists(fileLocation))//check for the file's existance { player.SoundLocation = fileLocation; player.Load(); //try playing the sound file try { if (willLoopSound) { player.PlayLooping(); } else { player.Play(); } } catch (InvalidOperationException Ex) { MessageBox.Show("The file '" + list.SelectedItem.ToString() + "' couldn't be played.", "Invalid Sound File"); } } else { MessageBox.Show("The file '" + list.SelectedItem.ToString() + "' couldn't be found.", "File Not Found"); } } private void btnRandom_Click(object sender, EventArgs e) { int iterations = (int)randomIterationNumericUpDown.Value; listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0]; if (listBoxEntity.Enabled == false) { //the ListBox is disabled and therefore contains no sounds, so return return; } //disable the Random Button and the NumericUpDown box btnRandom.Enabled = false; randomIterationNumericUpDown.Enabled = false; loopSelectedSoundCheckBox.Enabled = false; if (mainMenuPlayAll.Checked == true) { playRandomSoundBothColumns(iterations); btnRandom.Enabled = true; randomIterationNumericUpDown.Enabled = true; loopSelectedSoundCheckBox.Enabled = true; return; } for (int i = 0; i < iterations; i++) { if (soundboardTabControl.SelectedIndex == 0) { listBoxEntity.SelectedIndex = randomNum.Next(listBoxEntity.Items.Count); if (File.Exists(_workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav")) { player.SoundLocation = _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"; } else if (File.Exists(_workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3")) { player.SoundLocation = _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"; } else { MessageBox.Show("The file '" + listBoxEntity.SelectedItem.ToString() + "' couldn't be found.", "File Not Found"); return; } player.Load(); player.PlaySync(); } else { listBoxEntity.SelectedIndex = randomNum.Next(listBoxEntity.Items.Count); if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav")) { player.SoundLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"; } else if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3")) { player.SoundLocation = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"; } else { MessageBox.Show("The file '" + listBoxEntity.SelectedItem.ToString() + "' couldn't be found.", "File Not Found"); return; } player.Load(); player.PlaySync(); } } //renable the controls btnRandom.Enabled = true; randomIterationNumericUpDown.Enabled = true; loopSelectedSoundCheckBox.Enabled = true; } private void loopSelectedSoundCheckBox_CheckedChanged(object sender, EventArgs e) { if (loopSelectedSoundCheckBox.Checked == true) { willLoopSound = true; } else { willLoopSound = false; player.Stop(); } } private void playRandomSoundBothColumns(int iterations) { } private void mainMenuFileShowOnDisk_Click(object sender, EventArgs e) { string filePath; string arg; listBoxEntity = (ListBox)soundboardTabControl.SelectedTab.Controls[0]; if (soundboardTabControl.SelectedIndex == 0) { filePath = _workingDirectory + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"; arg = "/select, " + '"' + filePath + '"'; Process.Start("explorer.exe", arg); } else { filePath = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".wav"; if (File.Exists(filePath)) { Process.Start("explorer.exe", "/select, " + '"' + filePath + '"'); } else if (File.Exists(_workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3")) { filePath = _workingDirectory + "\\" + soundboardTabControl.SelectedTab.Text + "\\" + listBoxEntity.SelectedItem.ToString() + ".mp3"; Process.Start("explorer.exe", "/select, " + '"' + filePath + '"'); } } } private void mainMenuHelpAbout_Click(object sender, EventArgs e) { aboutForm aboutDialogForm = new aboutForm(); aboutDialogForm.ShowDialog(); aboutDialogForm.Dispose(); } private void mainMenuFileExit_Click(object sender, EventArgs e) { Close(); } private void mainMenuPlayAll_Click(object sender, EventArgs e) { MessageBox.Show("Under construction", "To Be Added", MessageBoxButtons.OK, MessageBoxIcon.Asterisk); mainMenuPlayAll.Checked = false; } } }