October 21, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Creating a App using JScript .NET and Windows Forms

  • February 22, 2002
  • By Essam Ahmed
  • Send Email »
  • More Articles »

Examining the PictureBox control

A PictureBox control allows you to display images on your form, as shown on the right side of Figure 1. The control supports various types of graphic formats, including GIF and JPG. Load an image into the PictureBox control based on the contents of an Image object, which (in this example) manages a Bitmap object. It sounds complicated, but it's actually straight-forward and logical once you see the code, which I'll present shortly.

The ComboBox control (drop down menu above the PcitureBox control) manages the contents of the PictureBox control at runtime, as shown in Figure 4.

Figure 4 - Selecting a picture to display

Whenever the user selects an item from the ComboBox, an event handler executes which checks to determine if an image file exists and then loads the image into the PictureBox control. Here's the code:

function cboPicture_OnChanged( source:Object, e: EventArgs ) {
  if(cboPicture.SelectedItem.ToString()=="") return;
  var fileName:String=new String(cboPicture.SelectedItem.ToString()+".jpg");
  if(!File.Exists(fileName))
  {
    MessageBox.Show("Cannot load picture...",
      "Error",
      MessageBoxButtons.OK,
      MessageBoxIcon.Exclamation,
      MessageBoxDefaultButton.Button1);
    return;
  }
  
  var pic : Bitmap = Bitmap(Image.FromFile(fileName));
  pictureBox1.Image = pic;
}

The function composes a file name based on the currently selected item's text and a ".jpg" extension. The function uses the File class's static Exists method to ensure that the image file the user selects exists in the same directory as the sample application. If the file exists, the listing creates an instance of a Bitmap object based on the contents of an Image object, which is based on the contents of an image file. The last action the code takes is to assign the Bitmap to the PictureBox's Image property, causing the currently selected image to be displayed.

The listing checks if the required image file exists before attempting to load it because the Image object throws an exception if it cannot load the file. An exception is a relatively (computationally) expensive operation, which you should try to avoid running into to make your code execute as fast as possible. There are many cases, like this one, where you can anticipate the exception and take steps to avoid the exception in the first place, often using less expensive operations.

Working with a Timer

A Timer control is a non-visual control that can raise events based on a time interval. When you run the sample application, it appears that the code executes in parallel since the progress bar at the bottom of Figure 1 updates every two seconds - even when you interact with other controls on the form.

The Timer class resides in the System.Timers namespace - set up a timer as shown in the following listing:

// Appears in the class's declaration...
private var localTimer : System.Timers.Timer ;

// Appears in the class's constructor and InitializeComponent function...
localTimer =  new System.Timers.Timer();
localTimer.Interval=2000;
localTimer.AutoReset=true
localTimer.add_Elapsed(OnTimer);
localTimer.Enabled=true;

// Elsewhere in the class...
function OnTimer( source:Object, e: ElapsedEventArgs )
{
  if(progressBar.Value>=100)
    progressBar.Value=0;
  progressBar.PerformStep();
}

The listing creates an instance of a new timer and sets its Interval property (the frequency at which the timer fires). The AutoReset property, when true, resets the timer to zero after each Interval - essentially setting up the timer to fire again. The timer does not start until you set its Enabled property to true; the OnTimer function handles the Elapsed event each time it fires (every 2000 milliseconds, or two seconds). The OnTimer function works with the ProgressBar control to update it every two seconds.

The ProgressBar control's PerformStep method increments the position of the bar's progress indicator, as defined by the bar's Step property. The ProgressBar control has a number of properties that you can set to customize its behavior and appearance - see the sample code for details.





Page 3 of 4



Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel