Need help with OpenFileDialog event


D

Durango2008

Hello I am reposting since my initial post was not appropriate, hope I was
not too offensive.

I created a simple form with an OpenFileDialog object that allows me
to browse the FS

and select a file from it.
However when I do select a file and press Open the FileOk event does not get
called.
Here is the code I have right now.

public partial class Form1 : Form
{
private string imgFile = null;
private const string Default_Path = "C:\\";
OpenFileDialog ofd = new OpenFileDialog();
public Form1()
{
InitializeComponent();
InitializeOpenFileDialog();
}

private void InitializeOpenFileDialog()
{
ofd.Filter =
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
"All files (*.*)|*.*";
ofd.InitialDirectory = Default_Path;
ofd.Title = "My Image Browser";
}
private void button1_Click(object sender, EventArgs e)
{
string imagePath = null;
if (imageFileTB.Text.Length > 0)
imagePath = imageFileTB.Text;
if (Directory.Exists(imagePath))
ofd.InitialDirectory = imagePath;
ofd.ShowDialog();
}

private void openFileDialog1_FileOk_1(object sender, CancelEventArgs
e)
{
this.Activate();
imageFileTB.Text = ofd.FileName;
MessageBox.Show("**** you " + ofd.FileName);
}
}

I have setup the OpenFileDialog FileOk event to call the
openFileDialog1_FileOk_1 method.
I looked up an example on MSDN and that was not working either. I am sure I
may be

forgetting something simple.
Anyway, I appreciate any help on this :)
 
Ad

Advertisements

B

Bjørn Brox

Durango2008 skrev:
Hello I am reposting since my initial post was not appropriate, hope I was
not too offensive.

I created a simple form with an OpenFileDialog object that allows me
to browse the FS

and select a file from it.
However when I do select a file and press Open the FileOk event does not get
called.
Here is the code I have right now.

public partial class Form1 : Form
{
private string imgFile = null;
private const string Default_Path = "C:\\";
OpenFileDialog ofd = new OpenFileDialog();
public Form1()
{
InitializeComponent();
InitializeOpenFileDialog();
}

private void InitializeOpenFileDialog()
{
ofd.Filter =
"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" +
"All files (*.*)|*.*";
ofd.InitialDirectory = Default_Path;
ofd.Title = "My Image Browser";
}
private void button1_Click(object sender, EventArgs e)
{
string imagePath = null;
if (imageFileTB.Text.Length > 0)
imagePath = imageFileTB.Text;
if (Directory.Exists(imagePath))
ofd.InitialDirectory = imagePath;
ofd.ShowDialog();
}

private void openFileDialog1_FileOk_1(object sender, CancelEventArgs
e)
{
this.Activate();
imageFileTB.Text = ofd.FileName;
MessageBox.Show("**** you " + ofd.FileName);
}
}

I have setup the OpenFileDialog FileOk event to call the
openFileDialog1_FileOk_1 method.
I looked up an example on MSDN and that was not working either. I am sure I
may be

forgetting something simple.
Anyway, I appreciate any help on this :)

Test the result of ofd.ShowDialog() - if OK use ofd.FileName
 
D

Durango2008

Bjørn Brox said:
Durango2008 skrev:
Thank you for the response, but I am not sure what you mean exactly. I
tested
the result of ofd.ShowDialog() and it returns with OK. I am using
ofd.FileName when I call the FileOK eventhandler.
Which area of my code are you specifically talking about?
 
Ad

Advertisements

A

Adrian Magdas

Durango2008 skrev:

Thank you for the response, but I am not sure what you mean exactly.  I
tested
the result of ofd.ShowDialog() and it returns with OK.  I am using
ofd.FileName when I call the FileOK eventhandler.
Which area of my code are you specifically talking about?

private void button1_Click(object sender, EventArgs e)
{
string imagePath = null;
if (imageFileTB.Text.Length > 0)
imagePath = imageFileTB.Text;
if (Directory.Exists(imagePath))
ofd.InitialDirectory = imagePath;
if(ofd.ShowDialog() == DialogResult.OK)
{
imageFileTB.Text = ofd.FileName;
MessageBox.Show("Bad F word" + ofd.FileName);
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top