Realization of real-time data update of Winform development framework

In winform development, there are various transfer values ​​between pages. The following introduces the page refresh operation through events

1. The inheritance relationship of the interface layer of the Winform development framework

Before opening, I will first introduce the inheritance relationship of the interface layer in the Winform development framework. First, I will divide all the forms into three categories, one is the ordinary form, the other is the edit form, and the other is the list display window body. The integration of forms can encapsulate and handle many things. Good encapsulation and inheritance can improve our efficiency and reduce duplication of code. Its role is no longer discussed and emphasized here.

Adopt window inheritance to unify the interface to a great extent, and provide good encapsulation for common interface operations, such as basic data editing, new window accumulation encapsulation of carriage return, arrow keys, data refresh, and exception handling , Data inspection, data storage, data update and other interfaces, providing great convenience for the data processing of the form.
The data query display form takes into account the needs of multi-document display, generally inherits the appropriate base class, encapsulates some commonly used interface layout, in order to achieve the corresponding interface processing effect.

For the three types of forms described above, the inherited interface base classes are the following. Ordinary forms inherit from BaseForm , edit forms inherit from BaseEditForm , and list display forms inherit from BaseDock . It is as follows in the project.

In the UML design diagram, we see that his relationship is as follows, where the green border is our base class above, and the red border is our actual form object. From this figure, we can clearly see him Relationship.

BaseEditForm is relatively more complex than the other two base forms. In addition to providing a few buttons for saving data, it can also be used to view data before and after. His initialization interface and application example interface are shown below.

2. Realization of real-time update of data in Winform development framework

After introducing the above inheritance relationship, let's take a look at how to implement the timely update of data based on this inheritance mode, that is, the effect of synchronizing the data records in the list after saving the data form.

1) First add event handling in the base class interface BaseEditForm

    public  partial  class BaseEditForm: BaseForm 
    { 
        public  event EventHandler OnDataSaved; // trigger to save subform data

2) Then add the handling of the event as shown below.

Copy code
        ///  <summary> 
        /// Event trigger after processing data saving
         ///  </ summary> 
        public  virtual  void ProcessDataSaved ( object sender, EventArgs e) 
        { 
            if (OnDataSaved! = null ) 
            { 
                OnDataSaved (sender, e); 
            } 
        }
Copy code

3) When data is saved, an event that triggers immediate data update is triggered

Copy code
        ///  <summary> 
        /// Save data (new and edited save)
         ///  </ summary> 
        public  virtual  bool SaveEntity () 
        { 
            bool result = false ;
             if (! string .IsNullOrEmpty (ID)) 
            { 
                / / Edited save 
                result = SaveUpdated (); 
            } 
            else 
            { 
                // New save 
                result = SaveAddNew (); 
            } 

            return result; 
        } 

        ///  <summary> 
        ///Update existing data
         ///  </ summary> 
        ///  <returns> </ returns> 
        public  virtual  bool SaveUpdated () 
        { 
            return  true ; 
        } 

        ///  <summary> 
        /// Save the newly added data
         ///  </ summary> 
        ///  <returns> </ returns> 
        public  virtual  bool SaveAddNew () 
        { 
            return  true ; 
        } 

        ///  <summary> 
        /// save
         ///  </ summary> 
        ///  <param name = " close ">Close form </ param>
        private  void SaveEntity ( bool close) 
        { 
            // Check the validity of the input 
            if ( this .CheckInput ()) 
            { 
                // Set the busy state of the mouse 
                this .Cursor = Cursors.WaitCursor;
                 try 
                { 
                    if ( this .SaveEntity ()) 
                    { 
                        ProcessDataSaved ( this .btnOK, new EventArgs ()); 

                        MessageDxUtil.ShowTips ( " Save Successful " );
                         if (close)
                        {
                            this.DialogResult = DialogResult.OK;
                            this.Close();
                        }
                        else
                        {
                            this.ClearScreen();                            
                        }                        
                    }
                }
                catch (Exception ex)
                {
                    this.ProcessException(ex);
                }
                finally
                {
                    // 设置鼠标默认状态
                    this.Cursor = Cursors.Default;
                }
            }
        }
Copy code

The above data storage operations belong to the base class, we pay special attention to this code

 if (this.SaveEntity())
 {
     ProcessDataSaved(this.btnOK, new EventArgs());

4) The operation of the list display interface

After implementing the above operations, these do not need to do any operations in the subclass of BaseEditForm, just need to implement the following code in the specific list display interface class.

We know that the list interface generally has a specific data refresh function package (such as the BindData function), then when we create new data, the implementation code is like this.

Copy code
        /// <summary>
        /// 新增数据操作
        /// </summary>
        private void btnAddNew_Click(object sender, EventArgs e)
        {
            FrmEditLaw dlg = new FrmEditLaw();
            dlg.OnDataSaved += new EventHandler(dlg_OnDataSaved);
            dlg.ShowDialog();
        }

        void dlg_OnDataSaved(object sender, EventArgs e)
        {
            BindData();
        }
Copy code

In this way, when we maintain data in the new form (data addition, data editing), once the save operation is triggered, these latest data records are also displayed in the list.

Published 28 original articles · Like 15 · Visits 110,000+

Guess you like

Origin blog.csdn.net/z3h0a5n8g8x9i9a2o3/article/details/9014199