Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

Tool Bar

Toolbar is also added like other resources (menu, image list, etc). It is represented in .Net by the System.Windows.Forms.ToolBar class. The ToolBar class contains a collection called Buttons which can be used to add/remove items from the toolbar. An image list can also be attached with the toolbar. When you click the Buttons property in the properties window, Visual Studio presents you the familiar interface to add and remove buttons. Event handlers can be added for the toolbar just by double clicking the button in the designer.

Date Time Picker

Date Time Picker control is commonly used in the windows environment to allow the user select a particular date. In .Net it is represented by the System.Windows.Forms.DateTimePicker class. It can simply be selected from the toolbox and placed on the form in the designer. Usually we only change its Name property. The following screen shot shows a form containing date time picker control.

The most frequently used event for date time picker is ValueChanged. It is triggered when the user selects a new date from the control. The following event handler uses this event to print the value of selected date in the message box

private void dtpSelectDate_ValueChanged(object sender, System.EventArgs e)

{

string msg = dtpSelectDate.Text;

msg += "\r\n Day: " + dtpSelectDate.Value.Day.ToString();; msg += "\r\n Month: " + dtpSelectDate.Value.Month.ToString();; msg += "\r\n Year: " + dtpSelectDate.Value.Year.ToString();;

msg += "\r\n Day of Week: " + dtpSelectDate.Value.DayOfWeek.ToString();; MessageBox.Show(msg);

223

Programmers Heaven: C# School

}

In the code above, we have separately collected and printed the day, month, year and day of the week in the message box. When the program is executed following output is resulted.

Windows Standard Dialog Boxes

Windows allows developers to provide some commonly used dialog boxes in their applications. They include the Open File, Save File, Font Settings, Color Selection and Print dialog boxes. We will discuss the first four of these dialogs here. The dialog boxes are also added to the form as a resource which means they don't have any permanent visual representation and pop up only when needed by the developer and the application.

Open File Dialog Box

This is the common dialog box presented in a Windows environment for opening files. It is an instance of System.Windows.Forms.OpenFileDialogBox. The following screen shot shows the common open file dialog.

224

Programmers Heaven: C# School

The important properties include:

Property

Description

DefaultExt

The default extention for opening files. It uses wild card technique for filtering file names. If

 

the value of DefaultExt is *.doc only then files with extention 'doc' will be visible in the open

 

file dialog.

FileName

The full path and file name of the selected file.

InitialDirectory

The initial directory (folder) to be opened in the dialog.

MultiSelect

Boolean property. Represents whether multiple file selection is allowed or not.

DialogResult

DialogResult enumeration that shows whether user has selected the OK or the Cancel button to

 

close the dialog box.

Using the Open File Dialog Box

Usually the open file dialog box is presented on the screen when a button is pressed or a menu item is selected. The following button event handler presents the open file dialog box and prints the name of file selected in a message box.

private void btnOpenFile_Click(object sender, System.EventArgs e)

{

DialogResult res = openFileDialog.ShowDialog(); if(res == DialogResult.OK)

{

MessageBox.Show(openFileDialog.FileName, "File selected");

}

225

Programmers Heaven: C# School

}

Here we first presented the dialog box on the screen using its ShowDialog() method. This method presents the dialog box on screen and returns the DialogResult enumeration which represents how the user closes the dialog box. In the next line we check whether user the pressed OK button to close the dialog box. If yes then we printed the name of selected file in the message box. When the above code is executed, it shows the following result.

It is clear from the above message box that the FileName property returns the complete path of the selected file.

Save File Dialog Box

The save file dialog box is used to allow the user to select the destination and name of the file to be saved. It is an instance of System.Windows.Forms.SaveFileDialog. It is very similar to the open file dialog box. The following code will show the save file dialog box on the screen and print the name of the file to be saved in the message box

private void btnSaveFile_Click(object sender, System.EventArgs e)

{

DialogResult res = saveFileDialog.ShowDialog(); if(res == DialogResult.OK)

{

MessageBox.Show(saveFileDialog.FileName, "File saved");

}

}

226

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