User interface development based on Windows Forms class library
Покупка
Основная коллекция
Издательство:
Южный федеральный университет
Автор:
Абрамян Михаил Эдуардович
Год издания: 2021
Кол-во страниц: 278
Дополнительно
Вид издания:
Учебник
Уровень образования:
ВО - Бакалавриат
ISBN: 978-5-9275-3830-0
Артикул: 786642.01.99
The tutorial focuses on developing a graphical user interface based on the Microsoft Windows Forms class library, which is a part of the .NET Framework since version 1.0. The tutorial is presented in the form of detailed descriptions of 23 projects
that demonstrate various aspects of user interface development for Windows applications. Projects can be implemented in the Microsoft Visual Studio 2015-2019 IDE. Description of projects is accompanied by numerous comments. Typical errors
that arise during the development of Windows applications are considered, ways to fix them are indicated. The final section contains 65 study assignments designed to consolidate the learning material. The textbook is intended for students specializing in science and engineering.
Тематика:
ББК:
УДК:
ОКСО:
- ВО - Бакалавриат
- 01.03.02: Прикладная математика и информатика
- 01.03.03: Механика и математическое моделирование
- 02.03.02: Фундаментальная информатика и информационные технологии
- 09.03.02: Информационные системы и технологии
ГРНТИ:
Скопировать запись
Фрагмент текстового слоя документа размещен для индексирующих роботов
MINISTRY OF SCIENCE AND HIGHER EDUCATION OF THE RUSSIAN FEDERATION SOUTHERN FEDERAL UNIVERSITY Mikhail E. Abramyan USER INTERFACE DEVELOPMENT BASED ON WINDOWS FORMS CLASS LIBRARY Textbook for students of computer science and programming Rostov-on-Don - Taganrog Southern Federal University Press 2021
УДК 004.438.NET ББК 32.973.202 А16 Published by decision of the Educational-Methodical Commission of the I. I. Vorovich Institute of Mathematics, Mechanics, and Computer Science of the Southern Federal University (minutes No. 5 dated April 12, 2021) Reviewers: doctor of Technical Sciences, Professor of the Department of Computer Science of the Rostov State Transport University, Professor M. A. Butakova; candidate of Physical and Mathematical Sciences, Head of the Department of Computer Science and Computational Experiment of the I. I. Vorovich Institute of Mathematics, Mechanics, and Computer Science of the Southern Federal University, Docent S. S. Mikhalkovich Публикуется с разрешения издательства БХВ, являющегося владельцем прав на первое издание книги на русском языке: Абрамян М.Э., Visual C# на примерах, 2008. Published with the permission of the publishing house BHV, which is the copyright holder of the first edition of the book in Russian: Абрамян М.Э., Visual C# на примерах, 2008. Abramyan, M. E. А16 User interface development based on Windows Forms class library : textbook / M. E. Abramyan ; Southern Federal University. - Rostov-on-Don ; Taganrog : Southern Federal University Press, 2021. - 278 p. ISBN 978-5-9275-3830-0 The tutorial focuses on developing a graphical user interface based on the Microsoft Windows Forms class library, which is a part of the .NET Framework since version 1.0. The tutorial is presented in the form of detailed descriptions of 23 projects that demonstrate various aspects of user interface development for Windows applications. Projects can be implemented in the Microsoft Visual Studio 2015-2019 IDE. Description of projects is accompanied by numerous comments. Typical errors that arise during the development of Windows applications are considered, ways to fix them are indicated. The final section contains 65 study assignments designed to consolidate the learning material. The textbook is intended for students specializing in science and engineering. УДК 004.438.NET ISBN 978-5-9275-3830-0 ББК 32.973.202 © ООО “БХВ”, 2017 © Southern Federal University, 2021
C Preface ................................................................. 8 1. Developing projects in Microsoft Visual Studio environment ......... 10 1.1. Creating, saving, and opening a project ........................ 10 1.2. Adding a new form to the project and placing a new control on the form ........................................................... 12 1.3. Setting properties of forms and controls ....................... 16 1.4. Defining event handlers......................................... 18 1.5. Making changes to the program text ............................. 19 1.6. Application launch...............................................20 2. Console application: DISKINFO project .............................. 22 2.1. Creating a console application ..................................22 2.2. Receiving the information about current disk ....................24 2.3. Using command line arguments ................................... 28 3. Exception handling: EXCEP project....................................30 3.1. Handling a specific exception and exception groups ............. 30 3.2. Handling any exception...........................................34 3.3. Re-throwing a handled exception..................................35 4. Events: EVENTS project................................................37 4.1. Connecting an event to a handler ................................37 4.2. Disconnecting a handler from an event............................41 4.3. Connecting another handler to an event ......................... 43 5. Forms: WINDOWS project..............................................46 5.1. Setting the visual properties of forms. Opening forms in normal and modal mode .......................................................46 5.2. Checking the state of the subordinate form.......................49 5.3. Controls adapting to fit the window ............................ 51 5.4. Modal and non-modal buttons of the dialog window ................52 5.5. Setting the active form control .................................54 5.6. Request for confirmation of closing the form ....................54 6. Sharing event handlers and working with keyboard: CALC project ..... 57 6.1. Event handler for multiple controls..............................57 6.2. Calculations with control of the correctness of the input data . 58 6.3. The simplest techniques to speed up work using keyboard .........60 6.4. Using a keyboard event handler .................................61 6.5. Control over changes to the input data..........................62
7. Working with date and time: CLOCK project......................... 64 7.1. Displaying the current time on the form ...................... 64 7.2. Implementation of the stopwatch capabilities ................. 66 7.3. Alternative options for executing commands using the mouse ... 70 7.4. Displaying the current status of the clock and stopwatch on the taskbar..................................................... 71 8. Text input: TEXTBOXES project .................................... 72 8.1. Additional highlighting of the active text box ............... 72 8.2. Changing the tab order of text boxes ......................... 74 8.3. Blocking exit from an empty text box ......................... 76 8.4. Informing the user about the error ........................... 77 8.5. Providing additional information about the error ............. 78 8.6. Form-level error checking..................................... 78 9. Mouse event handling: MOUSE project............................... 80 9.1. Dragging with the mouse. Setting the z-order of controls on a form.......................................................... 80 9.2. Resizing with the mouse ...................................... 84 9.3. Using additional cursors ..................................... 86 9.4. Handling a situation with simultaneous pressing of several mouse buttons........................................... 87 9.5. Dragging and resizing a control of any type. Using the find and replace tool ................................... 90 10. Drag-and-drop: ZOO project ...................................... 93 10.1. Dragging labels on a form.................................... 93 10.2. Dragging labels to text boxes................................ 96 10.3. Interaction oflabels......................................... 97 10.4. Actions in case of dragging to invalid target ............... 99 10.5. Additional coloring of source and target while dragging .... 100 10.6. Customizing the cursor in drag-and-drop mode ............... 101 10.7. Information about the current state of the program. Buttons with images............................................... 102 10.8. Restoring the initial state ................................ 105 11. Cursors and icons: CURSORS project ............................. 107 11.1. Using standard cursors ..................................... 107 11.2. Setting the cursor for a form and waiting mode indication .. 109 11.3. Connecting new cursors to the project and saving them as embedded resources............................................. 111 11.4. Working with icons.......................................... 112 11.5. Placing an icon of application in the notification area .... 113 12. Menus and processing of text files: TEXTEDIT1 project........... 116 12.1. Menu creation .............................................. 116 12.2. Saving text to a file ...................................... 119
12.3. Clearing the editing area and opening an existing file ..... 121 12.4. Request to save changes .................................... 124 13. Advanced menu options, color and font setting: TEXTEDIT2 project .......................................................... 126 13.1. Setting the font style (menu items as checkboxes) .......... 126 13.2. Setting text alignment (menu items as radio buttons) ....... 128 13.3. Setting the color of symbols and background color (third-level menu commands and the Color dialog box)................... 130 13.4. Setting font properties using the Font dialog box .......... 131 14. Editing commands, context menus: TEXTEDIT3 project ............. 134 14.1. Editing commands ........................................... 134 14.2. Special visualization of unavailable editing commands. Working with the clipboard...................................... 136 14.3. Creating a context menu..................................... 138 15. Toolbar: TEXTEDIT4 project.......................................140 15.1. Creation a toolbar and shortcut buttons. Adding images to menu items .................................................. 140 15.2. Using shortcut buttons that behave as checkboxes and radio buttons .............................................. 143 16. Status bar and hints: TEXTEDIT5 project ........................ 147 16.1. Using the status bar ....................................... 147 16.2. Inaccessible shortcut buttons............................... 148 16.3. Hiding the toolbar and status bar........................... 149 16.4. Displaying hints on the status bar.......................... 149 17. Formatting a document: TEXTEDIT6 project ....................... 152 17.1. Replacing the TextBox control with the RichTextBox control . 152 17.2. Correcting the state of shortcut buttons and menu commands when changing the current format................................ 155 17.3. Setting paragraph properties................................ 157 17.4. Display the current row and column ......................... 159 17.5. Loading and saving text without format settings ............ 161 18. Colors: COLORS project ..........................................163 18.1. Defining a color as a combination of four color components. Track bars and scroll bars...................................... 163 18.2. Inverting colors and output color constants ................. 167 18.3. Grayscale colors ............................................ 168 18.4. Displaying color names ...................................... 169 18.5. Controls and their associated labels ........................ 171 18.6. Anchoring controls .......................................... 172 19. Drop-down list and list box: LISTBOXES project ................. 175 19.1. Creating and using drop-down lists........................... 175 19.2. List box: adding and removing items ......................... 177
19.3. Additional list operations................................... 179 19.4. Performing list operations with the mouse ................... 182 20. Checkboxes and checked list boxes: CHECKBOXES project............ 186 20.1. Checkboxes and checking their state ......................... 186 20.2. Global setting of CheckedListBox items ...................... 189 20.3. Using checkboxes with three states .......................... 190 21. Viewing images: IMGVIEW project.................................. 193 21.1. Displaying a directory tree.................................. 193 21.2. View images from image files in the selected directory....... 200 21.3. Docking of controls and its features ........................ 206 21.4. Setting the image view mode.................................. 208 21.5. Saving information about the state of the program in the Windows registry............................................. 211 21.6. Restoring information from the Windows registry.............. 213 22. MDI application: JPEGVIEW project ............................... 216 22.1. Opening and closing child forms in MDI application .......... 216 22.2. Standard actions with child forms ........................... 220 22.3. Adding a list of open child forms to the menu ............... 222 22.4. Closing all child forms at the same time .................... 223 22.5. Image scaling ............................................... 223 22.6. Automatic resizing of child forms ........................... 224 22.7. Additional control tools .................................... 225 22.8. Scrolling the image using the keyboard....................... 228 23. Splash screen application: TRIGFUNC project ..................... 231 23.1. Creating a table of trigonometric function values ........... 231 23.2. Displaying the splash window when loading the program........ 235 23.3. Using the splash window as an information window ............ 237 23.4. Displaying the progress of the program loading............... 238 23.5. Early termination of the program............................. 240 23.6. Dragging the splash window................................... 241 24. Creating controls at runtime: HTOWERS project.................... 243 24.1. Creating a start position ................................... 243 24.2. Redrawing the tower when changing the number of blocks ...... 244 24.3. Dragging blocks to a new location ........................... 245 24.4. Restoring the start position and counting the number of block movings ................................................... 248 24.5. Information about solving the problem ....................... 249 24.6. Demo mode implementation..................................... 250 25. Study assignments................................................ 253 25.1. General requirements ........................................ 253 25.2. CONSOLE project: console applications, file and directory processing....................................... 253
25.3. DIALOGS project: form interaction ............................256 25.4. SYNC project: control synchronization ........................259 25.5. DRAGDROP project: drag-and-drop mode..........................262 25.6. TIMER project: timer-controlled programs .....................265 25.7. REGISTRY project: dialog boxes and working with the Windows registry ...........................................269 25.8. MDIFORMS project: MDI applications ...........................273 References.............................................................277
Preface This textbook focuses on developing a graphical user interface based on the Windows Forms class library. This library appeared in .NET Framework 1.0 (the Microsoft Visual Studio .NET 2003 IDE), was significantly improved in .NET Framework 2.0 (the Microsoft Visual Studio 2005 IDE) and after that practically did not change; nevertheless, it was included in all subsequent versions of Microsoft Visual Studio. Despite the presence of the newer Windows Presentation Foundation class library, which is related to the development of the graphical interface and provides more features, the Windows Forms library retains its position, due to greater ease of use and convenient visual design tools. At the same time, the capabilities of this library are quite enough for the development of fully functional Windows desktop applications of medium complexity. In addition, the Windows Forms library contains a number of important concepts related to GUI design. Therefore, studying it in a course on the basics of user interface development seems to be quite justified. The learning material of the book is presented in the form of descriptions of 23 examples, which are fully functional software projects. Each of the examples focuses on a specific topic indicated in its name, but much additional information is provided in the description of the project development process and accompanying comments. We focuse on best practices for developing event-driven applications and the efficient use of Windows Forms library components. The tools providing a convenient and reliable dialogue between the program and the user are discussed in detail. We also discuss common errors that occur when using the various Windows Forms library classes and show how to fix them. Most of the examples are slightly modified versions of the examples given in [1]. The exception is the IMGVIEW example, which has been changed more significantly by eliminating several obsolete controls (DriveListBox, DirListBox, and FileListBox) and using the TreeView control instead, which allows visualizing hierarchical lists of data. The book assumes an acquaintance with the basics of programming in the C# language at the level of knowledge of the system of basic types and control statements (see, for instance, [2]). The in-depth knowledge of the .NET object model is not required. Mostly C# 3.0 is used, which allows you to develop projects in the Visual Studio 2008 and higher. Among the innovations, only interpolated strings are used, which appeared in C# 6.0 (Visual Studio 2015); in earlier versions, you can use the string.Format function instead. The LINQ queries (implemented in C# 3.0) are used only in the projects LISTBOXES and IMGVIEW
(Chapters 19 and 21) and are commented in detail. Chapter 1 provides a detailed description of the Microsoft Visual Studio tools used to develop Windows applications. The last chapter of the book contains 65 study assignments divided into 7 groups related to the following topics: • developing console applications, • interaction between windows of an application, • synchronizing controls and sharing event handlers, • implementing the drag-and-drop mode, • creating timer-driven programs, • use of standard dialog boxes and the Windows registry, • developing MDI applications. Most assignments contain references to sections of the book that describe the required controls and how to use them.
1. Developing projects in Microsoft Visual Studio environment Currently, the most common version of Microsoft Visual Studio is version 2019, targeting the .NET 4.8 platform and the C# 8.0 language, although most of the features discussed are available for earlier versions. In particular, the Windows Forms library itself has remained unchanged since the release of .NET 3.5, C# 3.0, and Visual Studio 2008. 1.1. Creating, saving, and opening a project When you start Visual Studio, the Start Page is automatically loaded into it, which allows you to quickly load one of the previously developed projects (the Open recent list), open any other existing project (the Open a project or solution item), and also create a new project (the Create a new project item). All these actions are also available from the menu of the Visual Studio environment: • File | New | Project... or Ctrl+Shift+N - create a new project; • File | Open | Project/Solution... or Ctrl+Shift+O - open an existing project; • File | Recent Projects and Solutions - open one of the recently developed projects. The Visual Studio environment is organized in such a way that you cannot create a “stand alone” project. Each project must be contained in a special entity called a solution, which can be described as a group of related projects. Only one solution can be loaded into the Visual Studio environment at a time; loading another solution leads to automatic closing of the previous solution. When creating a new project, a dialog box appears on the screen, in which first of all you need to select the language used (in our case, C# or Visual C#), and then the project template and its name. As a template for all considered projects (except for the first two projects DISKINFO and EXCEP), you should choose Windows Forms App (or Windows Forms Application). As to the name, it is recommended to use the name of the example given in the title of the corresponding chapter, for example, EVENTS (see Chapter 4). Note that the project name can contain not only digits and Latin letters, but also other characters allowed in file names, including spaces, although this feature is not recommended for use. In Visual Studio, when creating a new project, you must immediately specify the directory to save it (the Location text box). Project placement is also affected by information related to the solution in which the project will be placed,