SiamIT
Well-known member
- Joined
- Aug 3, 2021
- Messages
- 53
- Programming Experience
- 5-10
Greetings..
after some search on google, i found that i can use Microsoft.WindowsAPICodePack.Shell to add custom controls on file open/save dialogue box as needed.
so, i tried some, but i can't make it work to solve my purpose. I have added a button to preview music. But it throws a exception when it try to access FileName property. It looks like that property only accessible when click on "Open" button.
here is the code i tried:
and here is the error i get:
is there any way, i can get the file name (with full path) when i click on the preview music button?
thanks in advance for any help..
best regards
after some search on google, i found that i can use Microsoft.WindowsAPICodePack.Shell to add custom controls on file open/save dialogue box as needed.
so, i tried some, but i can't make it work to solve my purpose. I have added a button to preview music. But it throws a exception when it try to access FileName property. It looks like that property only accessible when click on "Open" button.
here is the code i tried:
Sample Code:
private void sTest2()
{
//initial propeties
CommonOpenFileDialog dlgOpen = new CommonOpenFileDialog()
{
AllowNonFileSystemItems = false,
EnsureFileExists = true,
EnsurePathExists = true,
EnsureReadOnly = true,
EnsureValidNames = true,
InitialDirectory = musicPath,
IsFolderPicker = false,
Multiselect = false,
Title = "Please Select The Music File You Like To Use As Background Music"
};
//filters
dlgOpen.Filters.Add(new CommonFileDialogFilter("MP3 Music File", "*.mp3"));
//custom controls
CommonFileDialogButton button = new CommonFileDialogButton("Preview Music");
dlgOpen.Controls.Add(button);
button.Click += PreviewMusic;
//custom event handler
dlgOpen.FileOk += MusicFileSelected;
CommonFileDialogResult dlgRet = dlgOpen.ShowDialog();
}
private void MusicFileSelected(object sender, CancelEventArgs e)
{
if (player.IsPlaying == true)
{
player.Stop();
}
}
private void PreviewMusic(object sender, EventArgs e)
{
CommonFileDialogButton button = (CommonFileDialogButton)sender;
CommonOpenFileDialog dlgOpen = (CommonOpenFileDialog)button.HostingDialog;
if (string.IsNullOrWhiteSpace(dlgOpen.FileName) == true)
{
MessageBox.Show("Please select a music first!");
return;
}
player.Play(dlgOpen.FileName);
}
and here is the error i get:
C#:
File name not available - dialog has not closed yet.
is there any way, i can get the file name (with full path) when i click on the preview music button?
thanks in advance for any help..
best regards