OpenFileDialog Problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi everybody,

this is my first post to this newsgroup.

I have a problem with OpenFileDialog. I programmed a windows form that has a
button that opens a OpenFileDialog. If a user chooses a file and presses the
OpenFileDialogs "Open" button, the chosen file will be added to a list box.
If the user presses the "Save and Close" button of my form the content of the
list box will be written to an xml file.

Here is my code that adds the filename to the list box:
private void addFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Add Programs to supervise";
if (ofd.ShowDialog() == DialogResult.OK)
{
Log.Write("Adding " + ofd.FileName);
supervisedFilesListBox.Items.Add(ofd.FileName);
}
}
Unfortunately this does not work! The file name is added to the list box.
But - if my "Save and Close" button is clicked the form is closed but nothing
else happens.

If I replace the addFileButton_Click method by this one:
private void addFileButton_Click(object sender, EventArgs e)
{
supervisedFilesListBox.Items.Add(@"C:\autoexec.bat");
}
everything works fine. "C:\autoexec.bat" is added to the list box every time
I click the addFileButton and if I click the "Save and Close" button the xml
file is correctly generated.

I would be very glad if someone could tell me what I'm doing wrong!

Thanks in advance,
Sascha
 
Hi M. Genuini,

M. Genuini said:
Can you show what you do when you click the "Save and Close" button ?
this will become a plugin-dll for Mediaportal (mediaportal.sourceforge.net).
If the "Save and Close" button is clicked the following code is executed:

private void saveButton_Click(object sender, EventArgs e)
{
Log.Write("Enter saveButton_click");
using (MediaPortal.Profile.Xml xmlwriter = new
MediaPortal.Profile.Xml("ProcessStatus.xml"))
{
numberOfProcesses = supervisedProcessesListBox.Items.Count;
numberOfFiles = supervisedFilesListBox.Items.Count;
Log.Write("ProcessStatus: numberOfFiles: " + numberOfFiles);
xmlwriter.SetValue("ProcessStatus", "numberOfProcesses", numberOfProcesses);
int i;
for (i = 0; i < numberOfProcesses; i++)
{
xmlwriter.SetValue("ProcessStatus", "Process" + i,
supervisedProcessesListBox.Items);
xmlwriter.SetValue("ProcessStatus", "Process" + i + "_checked",
supervisedProcessesListBox.CheckedItems.Contains(supervisedProcessesListBox.Items));
}
xmlwriter.SetValue("ProcessStatus", "numberOfFiles", numberOfFiles);
for (i = 0; i < numberOfFiles; i++)
{
xmlwriter.SetValue("ProcessStatus", "File" + i,
supervisedFilesListBox.Items);
}
}

this.Close();
this.Dispose();
}

This method is never changed regardless if I use the addFileButton_Click
method with or without the OpenFileDialog...

As I mentioned in my first post my form is closed after the OpenFileDialog
was used but nothing else in the method saveButton_Click will be executed.
Not even the first line "Log.Write("Enter saveButton_click");"...

Ciao,
Sascha
 
Try this:
private void addFileButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
ofd.Title = "Add Programs to supervise";
ofd.FileOk+=new CancelEventHandler(ofd_FileOk);
ofd.ShowDialog();
}

private void ofd_FileOk(object sender, CancelEventArgs e)
{
OpenFileDialog dlg = sender as OpenFileDialog;
supervisedFilesListBox.Items.Add(ofd.FileName);
}
 
Hi Oleg369,

thanks for your quick response, but this didn't work either :-(
Same result: Filename (and path) is added to the list box, but when I click
on "Save and Close" nothing but closing my form happens...

Ciao,
Sascha
 
Hm.

I copied the entire project folder to another computer, compiled it there
with the same ide (vc# express) and on this computer everything works fine.

WTF?

Ciao,
Sascha
 
Back
Top