Use enhanced Windows Forms to create rich user interfaces for your. Net Program

Source: Internet
Author: User
Tags configuration settings
Source:. NET Framework 2.0: craft a rich UI for your. Net app with enhanced Windows Forms support

This article is based on the preview version of Microsoft Visual Studio 2005 before its release. It was previously codenamed "Whidbey ". All the information may change in the official version.

Content discussed in this article:

  • New Content of Windows Forms controls
  • Use 'topic'
  • Bind and display data
  • Other detailed data features
  • This article uses the following technologies: C # And Windows Forms

    Microsoft's. NET Framework Version 1.0 does well meet our requirements .. Net has important significance in the following two aspects. It not only fundamentally changes the developer's construction and release of Windows ApplicationsProgramAnd created a huge technical platform with excellent scaling features. Visual studio2005 is a good example, it performs well in user customization and improved functionality. These changes affect Microsoft.. NET Framework, from the core system. windows. forms namespaces are extended to a variety of supporting technologies, both old and new, including ADO.. net, objectspaces, and clickonce deployment.

    Visual Studio 2005 can improve production efficiency, including enhanced integrated development environment, improved controls and data designer, andCodeGenerator. Because you don't have to write code in a variety of predefined formats manually, code writers may be frustrated, and we can have more time to enjoy our lives.

    System. Windows. Forms



    Some important work has been done in system. Windows. forms, and the exciting new technology includes Windows Forms. As I wrote this article, the number of available namespaces and the composition of public types and members increased by 67% and 127% respectively, which undoubtedly marks Visual Studio 2005 as an excellent product. Of course, we do not have enough space to talk about everything in this article. Figure 1 shows us an overview of the main improvements. These new and enhanced improvements cover a wide range of areas, for example, better topic support, improved data binding, and the gridview control.

    Category Description
    Layout Control Splitcontainer, flowlayout, and tablelayout container

    Controls used to manage controls at design time and runtime.
    Data Binding Control The gridview control is used to enrich the forms, query methods, and processing methods of table style data. Datacontainer is used to simplify the design of data binding and Data Binding user interfaces. It supports VCR-style user interface design and master-Detail user interface design.
    User Interface construction component The mainmenu component now supports adding images to menu items. The winbar control provides the same toolbar as Microsoft Office. The webbrowser control is a hosted browser that encapsulates shdocvw. dll.
    Controls and components Controlarray restores a single interface
    Management It is used to manage many controls, which can be of different types. Componentarray is a good method to replace controlarray. It is oriented to components.
    Support for asynchronous calls The components that work in the background cover the creation and management of working threads (technically not from the system. windows. forms, But windows forms can be implemented ). audio files are loaded in the sound component. The image file is loaded in the image box control.
    Basic improvements Supports the Automatic completion function of the text box and composite list box controls to automatically complete many system resources, including ie browsing history and START | running commands. The composite list box and list box controls support formatting the list items that are most used.

    Figure 1 new windows Form Features

    Windows topic support

    When you create a new windows form project as before, you will find a new change: the default Windows theme support. A Windows topic is a set of personalized user settings that display the appearance of the Windows user interface. You can right-click the desktop and choose "properties"> "topic" in the displayed Properties dialog box to specify the desktop topic.

    Because Windows themes are specified by users, Windows Forms assume the responsibility for expressing them. Although the existing. NET Framework supports windows themes, version 1.0 requires developers to add a file with a special name at a specified location in the file system. Version 1.1 supports windows theme by providing the application. enablevisualstyles method and setting the flatstyle attribute value of each control to "system", thus simplifying the implementation process of version 1.0. Visual Studio 2005 will further improve this process. When creating a new Windows Forms project, the enablevisualstyles method is called by default so that the form supports windows themes by default:

     
    Public class form1: system. windows. forms. FORM {... [stathread] Static void main () {application. enablevisualstyles (); application. run (New form1 ());}...}

    Previously, setting the flatstyle attribute value of the control to "system" means that the operating system determines the display appearance of the control. Currently, when the flatstyle attribute is set to the default value "standard", most controls of Visual Studio 2005 will present themselves based on the Windows topic currently in use. This is basically the same as setting the system first and then saving your settings in the designer. No matter whether the flatstyle attribute is "standard" or "system", the form dynamically responds to changes in the subject at runtime, but does not need to write a line of code, this topic sensitivity is beneficial to developers.

    Control layout and Configuration

    To support Windows themes, you only need to create a project or form and add the required controls. However, it is time-consuming to layout and configure controls on the form, especially those complex forms. For ease of use, the Windows Form Designer adds some new features to reduce the design time consumed by these trivial tasks. These features include adsorption alignment, edit attribute mode, and smart tag, making the operation control easier. You can still use the layout Toolbar of Visual Studio to align controls horizontally or vertically. You only need to manipulate those controls on the form once. In addition, you can use adsorption to achieve the same effect as drag-and-drop controls, while avoiding the waste of design time when selecting controls and toolbar buttons when using layout toolbar. The adsorption alignment is represented by one or more viscous blue lines with controls aligned with adjacent controls under the guidance of these lines.


    Figure 2 edit attributes

    Another new feature is the edit attribute mode. It allows you to use the tab key to traverse controls and directly change their attributes on the page. Click a menu item with the same name in the context menu of the Windows Forms designer to activate the edit attribute mode. The next step is to traverse the control and modify the attributes as required. The procedure is shown in figure 2. After editing, click "back to layout mode" to return to normal editing mode. Setting properties is an easy way to change controls. It is not easy to use another more appropriate control to completely replace the current control. You need to delete the control, drag a new control into the form, and reconfigure it. To cope with complex, multi-step design tasks like this, Smart Tag comes in handy. It combines the multi-step tasks during design into one step, the menu item icon is displayed near the relevant control as its tag. Smart tag can also handle simple design-time tasks, which often happens, for example, you can drag a text box into a form to change its "text" attribute.

    Control and Data Binding

    Another improvement in the integrated development environment is that Windows Forms in Visual Studio 2005 provide new controls and improve popular old controls such as text boxes and composite list boxes. Figure 3 shows a small browser created with the newly added and improved controls in use. It requires less code than the previous version of Windows Forms.


    Figure 3 use the web browser of the new control

    This simple example shows how to use the enhanced toolbox in Visual Studio 2005 to create a wide range of programs and reduce code writing. For example, it takes about 10 minutes to design a fully functional small browser and write only 85 lines of code to implement all traditional Web browsing events, it also implements the URL access history function. The actual work of webpage navigation and HTML Rendering is completed by the webbrowser control in the right-side block container. The split bar and left and right block containers are provided by a splitcontainer control.

    The composite list box displayed in Figure 3 shows the new automatic completion support function. The autocompletemode function is implemented by three attributes: autocompletemode, which allows you to select a style enumeration value to set the autocomplete style:

     
    Enum autocompletemode {none = 0x0, // No autocompletion autosuggest = 0x1, // possible matches chosen from drop-down list autoappend = 0x2, // possible matches // appended to text // while typing autosuggestappend = 0x3 // autosuggest and // autoappend combined}

    In addition to selecting "NONE", you must set the autocompletesource attribute for other options. The value is the autocompletesource enumerated value pre-set by the system:

    Enum autocompletesource {filesystem = 0x1, // File System historylist = 0x2, // All URLs from history list recentlyusedlist = 0x4, // All URLs from recently used list allurl = 0x6, // historylist + recentlyusedlist allsystemsources = 0x7, // filesystem + allurl customsource = 0x40, // autocompletecustomsource none = 0x80 // No source}

    To set the custom source compound list box, we need to provide automatic completion options, which can be from the set of elements stored in the autocompletecustomsource attribute. Currently, the composite list box and text box are only controls that provide the Automatic completion function.

    Gridview

    Most widgets in the toolbox support data binding and use the underlying data binding components to bind various data sources. Data Binding is critical in many cases, especially in prototype and rapid development. The Windows Forms development team has significantly enhanced data binding namespaces with improved typed datasets, including gridview and datacontainer, and enhanced design-time support for rapid development. These features make your development of regional-style user interfaces or table-style user interfaces as easy as possible.

    Sometimes table-style user interfaces are more suitable for displaying bound data. On the other hand, Microsoft included the DataGrid Control in its previous. NET Framework. For Visual Studio 2005, the Windows Forms development team respondedCommunityAnd decide to construct a new table control, system. Windows. Forms. gridview (see figure 4 ).


    Figure 4 gridview Column

    Perhaps the most obvious difference between a DataGrid and a table view is the gridview object model, which has been abstracted into a natural table structure based on columns and rows, allows developers to manipulate each cell through a large number of intuitive positioning functions, including the following:

      • Supports a wide range of Custom User interfaces through styles, formats, la S, and selections
      • Compared with the DataGrid, it has the ability to display more types of data and adds a set of richer types, including images. Highlights such as freezing columns (similar to those in Excel) and reordering of row columns (similar to those in outlook ).
      • Its child controls have over 100 events in terms of navigation, editing, verification, self-painting, and error handling.

    Based on our extensive design experience, these new features allow us to quickly customize controls without writing a lot of code, in fact, the specific code of the DataGrid is inserted into the methods, attributes, and events of the gridview. One example is to select a row by clicking a cell in the DataGrid, which is now exposed in the form of the selectionmode attribute of the gridview. Of course, none of them are perfect. When they do not show enough, developers should rely on their extended features to combine controls with custom features. The underlying implementation of the gridview control is based on a variety of cells, rows, and columns, You can inherit and expand. In general, the control of gridview as a table style is more eye-catching than the DataGrid.

    Typed Dataset

    The gridview control is a member of the control family that can bind arrays, sets, and typed datasets. The advantage of a typed dataset is that the. NET framework allows controls to be bound in the designer to help developers see the designed user interface. Gridview automatically generates data for each field in the data source during design. Visual Studio 2005 enhances typed datasets to make our lives easier. You will reduce the time spent designing the form. You can now present the metadata in the data source in a typed dataset, including foreign key constraints, incremental seed, and step control. See Figure 5.


    Figure 5 default Typed Dataset

    The designer also generates a fill query, which encapsulates the SELECT statement for querying related tables and is implemented in a typed dataset with the same name. Sometimes, the Select, insert, update, and delete statements initially specified by the system are not useful. For example, the filling method returns all employee information, and in many cases, you only need to return the information of one employee. In this case, it is wise to use the specified query statement and fill it in. The Typed Dataset Designer allows you to add your own query statements by selecting "add" or "query" in the context menu of the appropriate table.

    The "Data Table query wizard" dialog box helps you construct your own query statements. Then, the data table is updated by new data. After you configure a typed dataset, You can bind it to one or more controls on the user interface. In Visual Studio 2005, a typed dataset can be directly bound to controls that implement data source and data member attributes without adding a DataSet object to the form.

    Defaultinstance

    Anyone who has used a large number of typed datasets knows that, in general, each new typed dataset class is used only in one place of the application. Because the Windows Forms development team understands us and wants to make it easier for us, they have already set a static property defaultinstance in each common Typed Dataset class and called it to get this single instance. Implementation:

     
    Public static employeetds defaultinstance {get {If (_ defaultinstance = NULL) {_ defaultinstance = new northwind ();} return _ defaultinstance ;}}

    You can use the defaultinstance attribute as follows:

    Public class form1: system. windows. forms. FORM {... private void form1_load (Object sender, system. eventargs e) {// load data employeetds. defaultinstance. loaddata ();}...}

    The data binding control uses the defaultinstance type name to set their data source attributes. The default type name is "project namespace. Type dataset name, and the subsequent steps for specifying data members remain unchanged.

    Datacontainer Control

    The gidview control is an example of complex control binding. It means that the control knows how to manage data binding to support query, insert, update, and delete operations. On the contrary, simple data binding controls do not know this. You need to use the data source binding manager to implement the same functions as complex binding controls, especially the user interface with the VCR style controls. When you prefer list-style data instead of table-style data, you can use simple data binding controls. In this method, you need to manually add a series of appropriate controls to the form in different regions, such as labels and text boxes. The results are as shown in figure 6.


    Figure 6 basic user interface

    When you can manually implement such a detailed form, you can also bind a typed dataset to the datacontainer in Visual Studio 2005 to implement the form at one time. You only need to simply select the menu item named this form from the smart tag of datacontainer.

    This feature automatically generates all control regions for you based on the selected data source. In addition, datacontainer loads and saves the data, and adds the navigation and editing controls of the VCR style by default without writing code. The result is shown in figure 7.


    Figure 7 regional user interface with VCR Control

    If your interface requirements change and you find that the table-style user interface is more appropriate, you can use datacontainer to accomplish such an arduous task, you only need to select the appropriate Smart Tag option from datacontainer. The designer deletes all existing controls and replaces them with a DataGrid (in a future version of Visual Studio, it may be a gridview ).

    Master-Detail binding

    The multi-functional datacontainer is also a powerful tool for building the master-Detail user interface. This interface provides a form-based visual effect to display the one-to-many relationships commonly used in relational databases. For example, to display the employee-order relationship, you only need to simply add the order table to the typed dataset to configure it correctly.


    Figure 8 master-detail form

    Now that we have generated the master User Interface (as shown in figure 7), we need to drag a gridview to the form and set its data source attribute to datacontainer, set the data member attribute to order-order details (here is order-employee foreign key constraint). The result is shown in figure 8.

    Clickonce deployment

    When you have set all the controls in the form and compiled the code for them to work together, you will deploy this application. Deploying an enterprise-level Windows Forms Application for a large number of users will face a severe challenge of installation and version change, therefore, developers of the previous generation are more willing to turn to web applications and enjoy the ease of deployment from the rich Windows user interfaces.

    In. net, Microsoft began to create an underlying structure, which made publishing Windows Forms applications as easy as publishing ASP. NET applications. Before Visual Studio 2005 ,. the. NET Framework supports no-touch deployment. It allows Windows Forms applications to be deployed from a URL or a unified name conversion (UNC) file path and run in a sandbox in the client, this sandbox features code access security (CAS. In Visual Studio 2005, the no-touch deployment evolved to clickonce deployment, and some new features provided windows form program developers with an underlying structure that is as smooth as deploying web applications. In particular, clickonce deployment also provides support for some improved functions, such as program sending, version update and rollback, more control over client application installation, update license, and design-time configuration support included in all structures of Visual Studio.

    Configure streamlined deployment

    During design, you can configure clickonce deployment by setting the attributes of the release attribute page in the project Configuration Attribute of visual studio2005. Depending on your underlying structure and user group, different deployment resources (file server, web server, or FTP Server) have specific meanings. Clickonce deployment allows you to specify which methods are to be released, installed, and installed (for example, through web pages ). You can configure the application to determine whether the application is installed on the physical drive of the customer's machine. If the client installation is complete, you can specify whether to perform an update check before the program is loaded or run, or how long it takes to perform an update check, or force Update-it is a useful feature for key update operations. It can also specify the prerequisites before the installation program, as is true for the. NET Framework Program itself. You can also set some security settings on the security properties page.

    When you are about to release an application, you can click "project" | "release project" to generate and release your application in one step. It generates and releases both applications. The publishing process is managed by the Publishing Wizard. It mainly displays your publishing configuration settings and allows you to change these configurations before publishing. The final pop-up form in the release process lists all the options that need to be determined, and provides an option to determine whether to publish the program or return the modification settings. The release wizard then publishes the application as required based on your selection.

    Deployment Program

    The deployment starts when you open the deployment web page and click the hyperlink required to publish the program. The application is linked to a. Deploy file instead of a self-running program .. The deploy file is one of the two files required for clickonce deployment. It specifies the Application Deployment and update requirements. The second file is the. manifest file, which specifies the file required for running the program. This is equivalent to the Assembly List of the program .. Deployand .manifestfiles are processed by clickoncedeployment services on the client side, and system.deploymentframework.service.exe. This service is installed along with the. NET Framework, and A. Deploy file is required during the first startup. It requires confirmation of the download in the form of a dialog box, and the user does not see any background operations. After the download is complete, the real application will be copied to the apphost.exe program and run by it. The application is stored in the local cache until you specify that no client is installed. This. Deploy file is always required for application startup in the future, and is executed in the local cache until the. debug file specifies that a new version is available on the server.

    If the application is running in a field that requires higher security configurations (such as the internet or LAN), The clickonce deployment program can provide users with access licenses to access programs at the corresponding level to ensure proper execution of the program. Without a license, the program will never be started and will not be given a chance to run it.

    Version Control and rollback for clickonce deployment

    If a program is deployed, an icon is added to the Start menu and added to the control panel | Add/delete program. From adding or deleting a program, you can completely uninstall the application or return to the previous version, if the previous version exists. The version control function of the assembly line deployment Program depends on the version number specified by the assemblyversion attribute. The assemblyversion attribute is in assemblyinfo. the CS file is generated, assemblyinfo. the CS file is generated by default for every Windows form project. Like this:

     
    [Assembly: assemblyversion ("1. 0. *")]

    the version number is used to determine whether a new version can be used for download. It is also used to name the deployment folder and the client temporary folder where the program is located. The names of the two folders are converted into program name_primary number _ secondary number _ generation number _ revision number.
    clickonce deployment is a very powerful technology that brings us many benefits and cannot be described in this article. However, the main motivation for using clickonce deployment is to facilitate deployment, installation, and control of versions. For more information, see "clickonce: deploy and update your Smart Client projects using a central server Article .

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.