NumericUpDown


The NumericUpDown control displays a number with up and down arrows that you can use to change the number. If you click an arrow and hold it down, the number changes repeatedly. After a small delay, the changes start happening faster, so you can make some fairly large changes in a reasonable amount of time. You can also change the number by clicking on it and typing in a new value.

The following table lists the control’s most interesting properties.

Open table as spreadsheet

Property

Purpose

DecimalPlaces

Determines the number of decimal places that the control displays. This has no effect when Hexadecimal is True.

Hexadecimal

Determines whether the control displays the number using a hexadecimal format as in A1C when the control’s value is 2588 (decimal).

Increment

Determines the amount by which the values are modified when the user clicks an arrow.

InterceptArrowKeys

If this is True, then the user can also adjust the number’s value using the up and down arrow keys.

Maximum

Determines the largest value that the control allows.

Minimum

Determines the smallest value that the control allows.

ReadOnly

Determines whether the user can type in a new value. Note that the arrow keys and arrow buttons still work when ReadOnly is True. You can disable them by setting InterceptArrowKeys to False and Increment to 0.

TextAlign

Determines whether the number is aligned on the left, right, or center of the control.

ThousandsSeparator

If this is True, then the control displays thousands separators when the value is greater than 999. This has no effect when Hexadecimal is True.

UpDownAlign

Determines whether the up and down arrows are positioned on the left or right.

Value

The control’s numeric value.

The control’s more important event, ValueChanged, fires whenever the control’s numeric value changes, whether because the user changed it or because the program’s code changed it.

The Click event handler is not as useful for deciding when the control’s value has changed. It executes when the user changes the value by clicking an arrow button, but it does not execute if the user types a new value into the field or uses the arrow buttons. It also fires if the user clicks on the control’s number but doesn’t make any changes.

OpenFileDialog

The OpenFileDialog component displays a dialog box that lets the user select a file to open. A program calls the component’s ShowDialog method to display a file selection dialog. ShowDialog returns DialogResult.OK if the user selects a file and clicks OK, and it returns DialogResult.Cancel if the user cancels. The following code shows how a program might use the component to let the user select a configuration file:

  If dlgConfigurationFile.ShowDialog() = DialogResult.OK Then     ' Load the configuration information.     ... End If 

Figure G-15 shows the dialog in action.

image from book
Figure G-15: The OpenFileDialog component lets the user select a file.

This component provides many properties for determining the kinds of files the user can select. The following table describes the most useful of these.

Open table as spreadsheet

Property

Purpose

AddExtension

If True, then the component adds the default extension specified in the DefaultExt property to the file name if the user does not include an extension.

CheckFileExists

If True, then the component verifies that the file exists. If the user types in the name of a nonexistent file, then the component warns the user and refuses to close.

CheckPathExists

If True, then the component verifies that the file’s directory path exists. If the user types in a file and path and the path doesn’t exist, then the component warns the user and refuses to close.

DefaultExt

The default extension that the component adds to the file’s name if the user omits the extension. This property should have a value such as txt.

DereferenceLinks

If this is True and the user selects a shortcut file (.lnk), then the component returns the name of the file referenced by the shortcut rather than the link file.

FileName

Sets the first file selected when the dialog is initially displayed. When the user closes the dialog box, this property returns the name of the file selected. The dialog box retains this value so the next time it is displayed it begins with this file selected.

FileNames

Gets an array of all of the files selected (if the dialog box allows multiple selections).

Filter

A string giving the filter that the dialog box should use. This string holds pairs of display names (such as Bitmaps) and their corresponding filter expressions (such as *.bmp) separated by vertical bar characters (|). Separate multiple expressions within a filter entry with semicolons. For example, the value GIFs|*.gif|JPGs|*.jpg;*.jpeg|Both|* .gif;*.jpg;*.jpeg|All Files|*.* lets the user search for GIF files, JPG files, both GIFs and JPGs, or all files.

FilterIndex

Gives the index of the filter entry that the dialog box initially displays. The indexes start with 1.

InitialDirectory

Determines the path where the dialog box starts when it is displayed. If you later redisplay the same dialog box, it will start at the path determined by its FileName property, so it continues where it last left off. If you want to change InitialDirectory to start in some other directory, you also need to set FileName = “”.

MultiSelect

Determines whether the user can select multiple files.

ReadOnlyChecked

Determines whether the dialog box’s Open as read-only check box is initially selected. This has no effect unless ShowReadOnly is True. The dialog box retains this value so that the next time it is displayed it has the value that the user selected. If you want the box checked every time the dialog box appears, you must set ReadOnlyChecked to True before you display the dialog each time.

RestoreDirectory

If this value is True, then the dialog box restores its initial directory after the user closes it, if the user has navigated to some other directory. However, if you later redisplay the same dialog box, it will start at the path determined by its FileName property, so it continues where it last left off. That means if you want to restore the initial directory, you must also set FileName = “” before redisplaying the dialog.

ShowHelp

Determines whether the dialog box displays a Help button. If you set this to True, then the application should catch the dialog box’s HelpRequest event and give the user some help.

ShowReadOnly

Determines whether the dialog box displays an Open as read-only check box.

Title

Determines the dialog’s title text.

The OpenFileDialog component raises its FileOk event when the user tries to accept a file. You can use an event handler to catch the event and perform extra validation. Set the event’s e.Cancel value to True to stop the dialog box from accepting the selection.

The following code allows the dlgBitmapFile dialog box to accept only bitmap files. The code loops through the dialog box’s selected files. If it finds one with a name that doesn’t end in .bmp, then the program displays an error message, sets e.Cancel to True, and exits the function.

  ' Ensure that the user only selects bitmap files. Private Sub dlgBitmapFile_FileOk(ByVal sender As System.Object, _  ByVal e As System.ComponentModel.CancelEventArgs) Handles dlgBitmapFile.FileOk     For Each file_name As String In dlgBitmapFile.FileNames         ' See if this file name ends with .bmp.         If Not file_name.EndsWith(".bmp") Then             MessageBox.Show("File '" & file_name & "' is not a bitmap file", _                 "Invalid File Type", _                 MessageBoxButtons.OK, _                 MessageBoxIcon.Exclamation)             e.Cancel = True             Exit Sub         End If     Next file_name End Sub 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net