Export (0) Print
Expand All

FileDialog.FileOk Event

Occurs when the user clicks on the Open or Save button on a file dialog box.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public event CancelEventHandler FileOk

For information about handling events, see Consuming Events.

The following code example demonstrates using the FileNames property, handling the FileOk event and using the Application.DoEvents method. When the example runs, a user can select graphics files from an OpenFileDialog object. The selected files are displayed in the form. The Application.DoEvents method forces a repaint of the form for each graphics file opened. To run this example paste the following code in a form containing a PictureBox named PictureBox1, an OpenFileDialog named OpenFileDialog1, and a Button named fileButton. Call the InitializePictureBox and InitializeOpenFileDialog methods from the form's constructor or Load method. When the example is running, display the dialog box by clicking the button.


	private void InitializePictureBox()
	{
		this.pictureBox1 = new System.Windows.Forms.PictureBox();
		this.pictureBox1.BorderStyle = 
			System.Windows.Forms.BorderStyle.FixedSingle;
		this.pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
		this.pictureBox1.Location = new System.Drawing.Point(72, 112);
		this.pictureBox1.Name = "pictureBox1";
		this.pictureBox1.Size = new System.Drawing.Size(160, 136);
		this.pictureBox1.TabIndex = 6;
		this.pictureBox1.TabStop = false;
	}

	private void InitializeOpenFileDialog()
	{
		this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();

		// Set the file dialog to filter for graphics files.
		this.openFileDialog1.Filter = 
			"Images (*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF|" + 
			"All files (*.*)|*.*";

		// Allow the user to select multiple images.
		this.openFileDialog1.Multiselect = true;
		this.openFileDialog1.Title = "My Image Browser";
		
	}

	private void fileButton_Click(System.Object sender, System.EventArgs e)
	{
		openFileDialog1.ShowDialog();
	}


	// This method handles the FileOK event.  It opens each file 
	// selected and loads the image from a stream into pictureBox1.
	private void openFileDialog1_FileOk(object sender, 
		System.ComponentModel.CancelEventArgs e)
	{

		this.Activate();
		 string[] files = openFileDialog1.FileNames;

		// Open each file and display the image in pictureBox1.
		// Call Application.DoEvents to force a repaint after each
		// file is read.        
		foreach (string file in files )
		{
			System.IO.FileInfo fileInfo = new System.IO.FileInfo(file);
			System.IO.FileStream fileStream = fileInfo.OpenRead();
			pictureBox1.Image = System.Drawing.Image.FromStream(fileStream);
			Application.DoEvents();
			fileStream.Close();

			// Call Sleep so the picture is briefly displayed, 
			//which will create a slide-show effect.
			System.Threading.Thread.Sleep(2000);
		}
		pictureBox1.Image = null;
	}




To run this example paste the following code in a form containing
a PictureBox named PictureBox1, an OpenFileDialog named OpenFileDialog1, 
and a button named fileButton.  Call the InitializePictureBox and 
InitializeOpenFileDialog methods from the form's constructor or 
Load method. When the example is running, display the dialog by clicking
the button.    


.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Community Additions

ADD
Show:
© 2014 Microsoft