The Winform interface enables dynamic and personalized Configuration Management of menu lists and winform configuration management.

Source: Internet
Author: User

The Winform interface enables dynamic and personalized Configuration Management of menu lists and winform configuration management.

In our general application system, because the system is intended for different types of users, we will see more and more menus, even more than a hundred, however, there may be a few menus for our actual work, which is inconvenient to find. Therefore, personalized menu configuration is particularly important. This article is based on the concept of providing users with a dynamic configuration of visible menus, you only need to select your favorite and commonly used menus. The menu configuration is stored in the database, and the user experience is the same for different clients. This article mainly introduces a complete idea of implementing such a function. Some code logic is for reference.

1. Process of dynamic personalized configuration of Menu list

In some of our software, we may place menus on the top of the interface, or we may place tree lists on the left of the interface. In this case, either of them is possible, the left-side menu is used to configure an overview menu.

For example, the menu information is displayed on the left side based on user permissions, and the entire list is dynamically generated. The interface effect is as follows.

Then, a right-click menu is provided in the function list to refresh and manage menu configurations, as shown in the following interface.

Through the configuration function, we allow users to enter a configuration management interface, in which the configuration shows the menu they are interested in, and then save it. After saving, refresh the function menu display on the interface.

The above interface effects are used to introduce the general process of menu configuration management. The reason why we put the interface effects in the previous section is to give us a perceptual knowledge similar to the prototype design method, after learning about the relevant processing process, we can proceed to implement this processing logic through encoding.

2. Menu dynamic personalized configuration function implementation

The above describes the approximate interface effect. With reference, we can implement its implementation ideas through code.

1) parameter data storage

First, we need to understand that user configuration can be saved locally through XML or stored on the server through database storage. The latter can be the same everywhere on distributed clients, in this way, there will be no differences in user experience. Therefore, we use the database storage solution.

This storage follows the configuration management component (SettingsProvider.net) that I have previously introduced ), I gave a detailed introduction to the use of parameter configuration management in Winform development framework-Based on SettingsProvider.net.

This configuration management component, SettingsProvider.net, is also easy to use. You can select the objects stored locally or in the database.

First, define a stored parameter class, which is the object information required to use this component, as shown in the following code.

/// <Summary> /// controls the parameter configuration of the display menu for personnel management. /// </summary> public class UserMenuParameter {[DefaultValue ("")] [Description ("User ID")] public string UserID {get; set;} [Description ("user setting visible menu")] public Dictionary <string, bool> VisibleDict {get; set ;}}

When we need to obtain or store the object information, we initialize several management classes, as shown in the following code.

// Private SettingsProvider settings; private ISettingsStorage store; private UserMenuParameter parameter;

Then initialize these objects in the configuration management interface form, as shown in the following code.

// PortableStorage: Create a setting file in the running program directory to record parameter data // DatabaseStorage: store the user configuration parameter store = new DatabaseStorage (LoginUserInfo. ID); settings = new SettingsProvider (store); parameter = settings. getSettings <UserMenuParameter> ();

In this way, we can obtain the information of the corresponding record based on the user ID and convert it to the relevant object. If we need to write the modified information to the storage medium, the Code is as follows.

Try {parameter = settings. getSettings <UserMenuParameter> (); parameter. visibleDict = dict; parameter. userID = LoginUserInfo. ID; settings. savesetask< UserMenuParameter> (parameter); ProcessDataSaved (sender, e); // triggers an external event this. dialogResult = System. windows. forms. dialogResult. OK;} catch (Exception ex) {LogHelper. error (ex); MessageDxUtil. showError (ex. message); return ;}
2) Implementation of the Configuration Management Interface

After obtaining and storing parameters, we need to write an interface to manage user menu configurations, that is, the menu Configuration Management Interface we introduced earlier.

The following code defines the interface.

The data storage of parameters applies the Code described above. You need to initialize the display process of the tree menu based on your configuration items, and display the menu through the InitTree function.

Before displaying a menu, we first introduce the function menu display rules. The menu is invisible only when the parameter has a corresponding record and the explicit settings of this record are invisible, otherwise, the default menu is displayed.

This ensures that all menus are visible to the current user before the parameters are configured. This menu is invisible only when the user is set to invisible.

/// <Summary> /// obtain whether the menu is visible. /// The menu is invisible only when the corresponding record exists and the explicit setting of the record is invisible. Otherwise, the default menu is visible. /// </Summary> /// <param name = "id"> menu ID </param> /// <returns> </returns> private bool GetVisibleMenu (string id) {bool result = true; if (parameter! = Null) {var dict = parameter. VisibleDict; if (dict! = Null & dict. ContainsKey (id) {result = dict [id] ;}} return result ;}

The relevant processing logic of the display menu is to determine whether to check the record based on the above judgment, as shown in the following code.

When storing records selected by users, we need to traverse the entire tree node, determine which options are selected, and then save it to the database.

/// <Summary> /// recursively retrieve the selected tree node set /// </summary> /// <param name = "node"> tree node </param> /// <param name = "dict"> Dictionary set </param> /// <returns> </returns> private Dictionary <string, bool> GetTreeSelection (TreeNode node, Dictionary <string, bool> dict) {if (node. tag! = Null) {var check = node. Checked; var menuId = string. Concat (node. Tag); if (! Dict. containsKey (menuId) {dict. add (menuId, check) ;}} foreach (TreeNode child in node. nodes) {GetTreeSelection (child, dict) ;}return dict ;}

Save the parameters as follows.

/// <Summary> /// Save the user configuration information /// </summary> private void btnOK_Click (object sender, EventArgs e) {// obtain the tree list selected by the user, stored in the Dictionary set var dict = new Dictionary <string, bool> (); foreach (TreeNode node in this. treeView1.Nodes) {GetTreeSelection (node, dict);} try {// obtain the parameter information again, set the new value, and save parameter = settings. getSettings <UserMenuParameter> (); parameter. visibleDict = dict; parameter. userID = LoginUserInfo. ID; settings. savesetask< UserMenuParameter> (parameter); ProcessDataSaved (sender, e); // triggers an external event this. dialogResult = System. windows. forms. dialogResult. OK;} catch (Exception ex) {LogHelper. error (ex); MessageDxUtil. showError (ex. message); return ;}}
3) handling of the main interface

After the preceding steps are completed, right-click the toolbar of the main interface and add a menu item to enter the configuration interface, as shown in the following logic code.

Private void tool_MenuSetting_ItemClick (object sender, DevExpress. xtraBars. itemClickEventArgs e) {MenuSetting () ;}/// <summary> // configure the menu item /// </summary> private void MenuSetting () {FrmMenuSetting dlg = new FrmMenuSetting (); dlg. onDataSaved + = (s, arg) =>{// after the user saves the parameters, the user is prompted to update the tree list InitToolbar () ;}; dlg. showDialog ();}

After the parameters are configured and saved on the interface, the tree menu of the interface will be updated in time.

In addition, the tree list on the main interface also needs to be adjusted based on the configuration parameter information. If the user does not display a menu, the main interface also needs to be displayed according to the configuration parameter control.

 

3. Summary

The above is the overall idea and implementation step code of dynamic personalized Configuration Management in the menu list. the main interface consideration is to consider the layout and functions of the interface based on the user's vision, if you are looking for several frequently-used menus in hundreds of menu items, each time it is a time-consuming and boring operation, you can provide a personalized interface based on your work conditions, display your own functions.

For example, in some cases, we want to control the display of menus through the toolbar, as shown in the following figure.

The configuration and maintenance interface is similar, but the display logic of the toolbar is different. The dynamic generation and processing of RibbonPage and its functional menus are as follows.

This article mainly hopes that readers can learn from the ideas of configuring storage and personalized menu management. The specific logic varies depending on the user interface and the controls used, however, the overall idea is the same.

For example, for configuration management of some parameters, you can use a unified configuration management interface for maintenance, just like the interface functions described in my previous articles.

I hope this article will inspire you and thank you for your patience.

Related 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.