J'ai le code suivant :
SaveFileDialog saveGC1File = new SaveFileDialog();
private void GCSavePlacementOneButton_Click(object sender, EventArgs e)
{
// Initialize the SaveFileDialog to specify the .txt extension for the file.
saveGC1File.DefaultExt = "*.txt";
saveGC1File.Filter = ".txt Files|*.txt|All Files (*.*)|*.*";
saveGC1File.RestoreDirectory = true;
try
{
string placementOneSave = placementOneListBox.Items.ToString();
// Save the contents of the formattedTextRichTextBox into the file.
if (saveGC1File.ShowDialog() == DialogResult.OK && saveGC1File.FileName.Length > 0)
placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText);
// Throws a FileNotFoundException otherwise.
else
throw new FileNotFoundException();
}
// Catches an exception if the file was not saved.
catch (Exception)
{
MessageBox.Show("There was not a specified file path.", "Path Not Found Error",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
Cependant, en utilisant Visual Studio 2010, la ligne dans la boucle "if" qui indique :
"placementOneSave.SaveFile(saveGC1File.FileName, RichTextBoxStreamType.PlainText);"
A un rouge sous la ligne "SaveFile". J'ai déjà utilisé ce code pour enregistrer un fichier et je ne sais pas pourquoi il ne fonctionne pas pour une ListBox.
QUESTIONS
- Comment puis-je enregistrer une ListBox d'une manière similaire à une RichTextBox ?
- Est-il possible de modifier ce code pour que l'utilisateur choisisse l'endroit où la ListBox sera sauvegardée ?