Advertisement

Responsive Advertisement

III.7- Using PictureBox Control, III.8- Using OpenFileDialog Control

III.7- Using PictureBox Control

The PictureBox control is used for displaying images on the form. The Image property of the control allows you to set an image both at design time or at a runtime. Specifies whether the picture box accepts data that a user drags on it. Gets or sets the path or the URL for the image displayed in the control.

 

Properties of PictureBox 
  • AllowDrop Property
    Specifies whether the picture box accepts data that a user drags on it.

  • ImageLocationProperty
    Gets or sets the path or the URL for the image displayed in the control.

  • InitialImage Property
    Gets or sets the image displayed in the control when the main image is loaded.

  • WaitOnLoad Property
    Specifies whether or not an image is loaded synchronously. 


III.8- Using OpenFileDialog Control

C# OpenFileDialog control allows us to browse and select files on a computer in an application. A typical Open File Dialog looks like Figure 1 where you can see Windows Explorer like features to navigate through folders and select a file.

Creating a OpenFileDialog
We can create an OpenFileDialog control using a Forms designer at design-time or using the OpenFileDialog class in code at run-time (also known as dynamically). Unlike other Windows Forms controls, an OpenFileDialog does not have and not need visual properties like others. The only purpose of OpenFileDialog to display available colors, create custom colors and select a color from these colors. Once a color is selected, we need that color in our code so we can apply it on other controls.
Again, you can create an OpenFileDialog at design-time but it is easier to create an OpenFileDialog at run-time.
Design-time

To create an OpenFileDialog control at design-time, you simply drag and drop an OpenFileDialog control from Toolbox to a Form in Visual Studio. After you drag and drop an OpenFileDialog on a Form, the OpenFileDialog looks like Figure 2.




Adding an OpenFileDialog to a Form adds following two lines of code.

  1. private System.Windows.Forms.OpenFileDialog openFileDialog1;  
  2. this.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();  
Run-time

Creating a OpenFileDialog control at run-time is merely a work of creating an instance of OpenFileDialog class, set its properties and add OpenFileDialog class to the Form controls.

First step to create a dynamic OpenFileDialog is to create an instance of OpenFileDialog class. The following code snippet creates an OpenFileDialog control object.

  1. OpenFileDialog openFileDialog1 = new OpenFileDialog();  
ShowDialog method displays the OpenFileDialog.
  1. openFileDialog1.ShowDialog();  
Once the ShowDialog method is called, you can browse and select a file.

Setting OpenFileDialog Properties

After you place an OpenFileDialog control on a Form, the next step is to set properties.

The easiest way to set properties is from the Properties Window. You can open Properties window by pressing F4 or right click on a control and select Properties menu item. The Properties window looks like Figure 3.












Post a Comment

0 Comments