Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C# ПІДРУЧНИКИ / c# / Manning - Windows.forms.programming.with.c#.pdf
Скачиваний:
108
Добавлен:
12.02.2016
Размер:
14.98 Mб
Скачать

6.5.2SETTING THE TITLE BAR

So far we have only set the title bar in our MainForm constructor. Realistically, we will likely want to set this from a number of places in our application, especially if we want to include information about the current album as part of the title bar.

Let’s create a SetTitleBar method to assign the title bar based on the current album name, if any. This method requires a means of extracting the base name from the current album file. This functionality is provided by the Path class, as described in .NET Table 6.4. The rather cumbersome GetFileNameWithoutExtension method obtains the base file name without the extension.

The code for the SetTitleBar method is described in the following table:

Set the version number of the MyPhotos application to 6.5.

 

 

SET THE APPLICATION TITLE BAR

 

 

 

 

Action

Result

 

 

 

1

In the MainForm.cs code

using System.IO;

 

window, indicate that

 

 

we are using the

 

 

System.IO namespace.

 

 

 

 

2

Add a new

private void SetTitleBar()

 

SetTitleBar method to

{

 

the MainForm class.

Version ver = new Version(

 

Application.ProductVersion);

 

 

 

 

 

3

Define a default title bar

if (_album.FileName == null)

 

when no album file is

{

 

set.

this.Text = String.Format("MyPhotos {0:#}.{1:#}",

 

ver.Major, ver.Minor);

 

 

 

 

}

 

 

 

4

When an album file is

else

 

set, include the base file

{

 

name in the title bar.

string baseFile = Path.

 

GetFileNameWithoutExtension(

 

 

 

 

_album.FileName);

 

 

this.Text = String.Format(

 

 

"{0} - MyPhotos {1:#}.{2:#}",

 

 

baseFile, ver.Major, ver.Minor);

 

 

}

 

 

}

 

 

 

We will make use of this new method in our implementation of a Click handler for the New menu.

FILES AND PATHS

179

.NET Table 6.4 Path class

The Path class represents an object stored on disk, whether a file or directory object. This class is sealed, and is part of the System.IO namespace. The Path class contains static methods for creating and managing disk objects.

 

DirectorySeparatorChar

A platform-specific directory separator

 

 

character. This is the backslash character

 

 

‘\’ on Windows systems.

Public Static

InvalidPathChars

A platform-specific array of characters.

Readonly

 

Each character is not permitted in a file

Fields

 

system path.

 

PathSeparator

A platform-specific path separator

 

 

character. The semicolon ‘;’ character

 

 

on Windows systems.

 

 

 

 

ChangeExtension

Changes or removes the file extension

 

 

for a file.

 

GetDirectoryName

Returns the directory path of a file.

 

GetExtension

Returns the extension of a file.

 

GetFileName

Returns the file and extension parts of a

 

 

file.

 

GetFileNameWithoutExtension

Returns the file name without its

Public Static

 

extension.

Methods

GetFullPath

Returns the fully qualified path for a given

 

 

 

path.

 

GetPathRoot

Returns the root of a given path.

 

GetTempFileName

Returns a unique temporary file name

 

 

and creates an empty file with that name

 

 

on disk.

 

HasExtension

Determines whether a path includes a

 

 

file extension.

 

 

 

6.5.3HANDLING THE NEW MENU

With the ability to manage an album file and directory in place, this is as good a place as any to implement a Click handler for our New menu. We will use our new SetTitleBar method to initialize the title bar for the application. This method is used here as well as later in this chapter to initialize the title bar when the current album changes.

180

CHAPTER 6 COMMON FILE DIALOGS

Соседние файлы в папке c#