DevExpress processing operation of common interface in WinForm development

Source: Internet
Author: User

When we develop the WinForm program, we need to periodically initialize some of the controls on the interface, or often simply encapsulate it so that we can reuse it in the interface design process. This article mainly introduces some of my projects in the regular interface processing operations and code, in order to develop for everyone to provide the necessary reference.

1. Select the user's control encapsulation operation

In some system modules, we need to select the operation of the system personnel as the manager, as shown in the following interface scenarios.

We have noticed that the interface will pop up a new layer for us when we choose, and it is convenient to display the relevant information in detail in the list, and also to support the search.

When we finish the selection, we see that the interface will be displayed with only one person's name, without any additional space.

How is this interface effect implemented? The following is a detailed introduction.

1) First we define a custom control to inherit from the Xtrausercontrol.

    /// <summary>    /// Selection Control package    for personnel and operators /// </summary>     Public Partial class Operatorselectcontrol:xtrausercontrol    {

2) then drag a Searchlookupedit control onto our new user control Operatorselectcontrol on the DevExpress interface Toolbox.

Adjusting the layout-related properties of the user interface controls will be shown in the following interface.

3) In Design view, we set its display field for the Searchlookupedit control's GridView, as shown below, where each field is primarily bound FieldName (attribute or field) and caption (display name).

After the necessary processing is done, we can set the display and storage of the control by code or designer, and bind its data source (call its own function as needed), as shown below.

        Private voidOperatorselectcontrol_load (Objectsender, EventArgs e) {            if(! This. DesignMode) {txtoperator.properties.  ValueMember ="ID"; Txtoperator.properties. DisplayMember ="FullName"; TxtOperator.Properties.DataSource=securityhelper.getsimpleusers (); }        }

4) For convenience, we can further deal with the display of the control and return the value of the content, we want to bind the value or get the value of the time, the Text property can be used, then we reload the custom control of the Text property.

         Public Override stringText {Get            {                stringresult =""; if( This. txtoperator.editvalue! =NULL) {result= This. txtOperator.EditValue.ToString (); }                returnresult; }            Set            {                 This. Txtoperator.editvalue =value; }        }

5) The operation of this control is complete, and after compiling the code, we can see the latest control icon on the Toolbox.

Then we drag the relevant controls to where needed (if the interface and controls in the same project, notice the need to remove duplicate project references), recompile the system code, then the beginning of this article the interface effect can appear.

2, membership card level selection action

Just said, the choice of personnel can use this pop-up list selection method, in fact, many places can be used in this way to choose, such as some relatively less recorded information is very suitable for this display, such as in the member management, membership card level selection, you can also use this way.

The practice of this module is similar to the first, a little different is that I need to select the membership card discount is also associated with the display, then we should deal with it?

1) defining controls and event handling

To facilitate the triggering of events after the control selection, I have defined an event handler here SelectedValueChanged

 /// <SUMMARY>  ///  card-level selection control  /// </summary>  public  partial  class   Cardgradeselectcontrol: Xtrausercontrol { /// <summary>< /span>  Select event handling that has changed  /// </SUMMARY>  public  Span style= "color: #0000ff;" >event  EventHandler selectedvaluechanged; 

Then, when the editvalue of the internal control changes, the events inside it trigger our custom events, as shown below.

        Private void txtcardgrade_editvaluechanged (object  sender, EventArgs e)        {            if NULL             {                selectedvaluechanged (sender, E);            }        }

2) event invocation of custom controls in the form interface

In the member editing interface, we handle custom events for this card-level selection control.

In handling the event, we obtain the corresponding card level discount (Getdiscountbygradeno), and then bind to the interface of the control display, so that the linkage effect is achieved.

     Public Partial classFrmeditmember:baseeditform {/// <summary>        ///Create a temporary object that allows you to get the existing GUID in attachment management/// </summary>        PrivateMemberInfo Tempinfo =NewMemberInfo ();  PublicFrmeditmember () {InitializeComponent (); this.txtCardGrade.SelectedValueChanged + = new EventHandler (txtcardgrade_selectedvaluechanged); }        voidTxtcardgrade_selectedvaluechanged (Objectsender, EventArgs e) {            stringGradeno =Txtcardgrade.text; if(!string. IsNullOrEmpty (Gradeno)) {This.txtDiscount.Value = bllfactory<cardgrade>.            Instance.getdiscountbygradeno (Gradeno); }        }

3. Display processing for creating people and creating time

In the editing interface of our form data, we may need to display the creator and creation time of the data for friendly display. How this effect is implemented, let's take a look.

1) Data Display time processing

When the data is displayed, it may be the display of the new interface or the display of existing records, which we need to differentiate.

If there is already data, we need to parse the ID of the creator, show the time available, and if it is a new interface, we will display the name of the current logged-in user and display the current time.

In design mode, set the control to readonly=true without editing the value of the control.

        /// <summary>        ///functions for displaying data/// </summary>         Public Override voidDisplaydata () {Initdictitem ();//Data dictionary loading (public)            if(!string. IsNullOrEmpty (ID)) {#regionDisplay informationMemberInfo Info= BLLFACTORY&LT;WHC. Member.bll.member>.                Instance.findbyid (ID); if(Info! =NULL) {Tempinfo= info;//assign a value to the temporary object to point to the existing Record objectTxthandno.text=info.                    Handno; Txtcardno.text=info.                    Cardno; Txtcardcode.text=info.                    Cardcode; Txtcardstatus.setcomboboxitem (info. Cardstatus.tostring ());Txtopenuser.text=info. Openuser;
.............. Txtcreator.text = Securityhelper.getfullnamebyid (info. Creator); Txtcreatetime.setdatetime (info. Createtime); } #endregion } Else { This. Txtopendate.datetime =DateTime.Now; This. Txthandno.text =Idutils.newid (); this.txtCreateTime.DateTime = DateTime.Now; This.txtCreator.Text = loginuserinfo.fullname;//defaults to the current logged-on user } This. Portraitcontrol1.memberid =tempinfo.id; This. Portraitcontrol1.bindpicture (tempinfo.id); //Tempinfo is the specified object when the object exists, new is a new object, but there are some initialized GUIDs for attachment uploadsSetattachinfo (Tempinfo); }

However, when the user saves the operation, if it is new data, we need to record the current time and the current user's information into the database, so we need to add some code to the data related processing.

        /// <summary>        ///data saving in new state/// </summary>        /// <returns></returns>         Public Override BOOLsaveaddnew () {MemberInfo info= Tempinfo;//You must use the local variables that exist because some of the information may be used by attachmentsSetInfo (info); info. Creator = LoginUserInfo.ID.ToString (); Info.            Createtime = DateTime.Now; Info.            dept_id = Loginuserinfo.deptid;            info.company_id = Loginuserinfo.companyid; Try            {                #regionNew data//Check if there are other records of the same keyword                BOOLexist = BLLFACTORY&LT;WHC. Member.bll.member>. Instance.isexistkey ("Cardno", Info.                Cardno); if(exist) {messagedxutil.showtips ("The specified "loyalty card number" already exists and cannot be added repeatedly, please modify"); return false; }                BOOLsucceed = BLLFACTORY&LT;WHC. Member.bll.member>.                Instance.insert (info); if(succeed) {//additional associated actions can be added                    return true; }                #endregion            }            Catch(Exception ex) {logtexthelper.error (ex); Messagedxutil.showerror (ex.            Message); }            return false; }

For convenience, we typically store the ID of the person in the database, but when the list is displayed, we also need to convert the corresponding person ID to the person's name.

             This New DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler (Gridview1_customcolumndisplaytext);
        voidGridview1_customcolumndisplaytext (Objectsender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e) {            if(E.column.fieldname = ="Operator"|| E.column.fieldname = ="Editor"|| E.column.fieldname = ="Creator")            {                if(E.value! =NULL) { e.displaytext = Securityhelper.getfullnamebyid (e.value.tostring ()); }            }        }

DevExpress processing operation of common interface in WinForm development

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.