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>  ///handling personnel, Operator's selection control Package///  </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 void Operatorselectcontrol_load (object sender, 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 string Text    {      get      {        string result = "";        if (this.txtOperator.EditValue! = null)        {          result = this.txtOperator.EditValue.ToString ();        }        return result;      }      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 controls  ///</summary> public partial class Cardgradeselectcontrol: Xtrausercontrol {//<summary>///    Select event Handling changed    ///</summary> public    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 (selectedvaluechanged! = null) c12/>{        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 class Frmeditmember:baseeditform {//<summary>//////    -Create a temporary object that allows for the presence of GUIDs in attachment management    ///</summary> private MemberInfo tempinfo = new MemberInfo ();    Public Frmeditmember ()    {      InitializeComponent ();      This.txtCardGrade.SelectedValueChanged + = new EventHandler (txtcardgrade_selectedvaluechanged);    }    void Txtcardgrade_selectedvaluechanged (object sender, EventArgs e)    {      string gradeno = 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>////Data display functions///</summary> public override void Displaydata () {Initdictitem (); Data dictionary load (public) if (!string. IsNullOrEmpty (ID)) {#region display information MemberInfo info = BLLFACTORY&LT;WHC. Member.bll.member>.        Instance.findbyid (ID); if (info! = null) {Tempinfo = info;//re-assigns a value to the temporary object, pointing to the existing Record object Txthandno.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 upload setattachinfo (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>///New data storage///</summary>//<returns></returns> public Overrid      e bool Saveaddnew () {MemberInfo info = tempinfo;//must use the existing local variable, because some of the information may be used by the attachment SetInfo (info); Info.      Creator = LoginUserInfo.ID.ToString (); Info.      Createtime = DateTime.Now; Info.      dept_id = Loginuserinfo.deptid;      info.company_id = Loginuserinfo.companyid; try {#region new data//check if there are other records of the same keyword bool exist = BLLFACTORY&LT;WHC. Member.bll.member>. Instance.isexistkey ("Cardno", info.        Cardno);          if (exist) {messagedxutil.showtips ("specified" card number "already exists, cannot be added again, please modify");        return false; } bool succeed = BLLFACTORY&LT;WHC. Member.bll.member>.        Instance.insert (info);        if (succeed) {//can add other associated operations 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.winGridViewPager1.gridView1.CustomColumnDisplayText + = new DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventHandler (Gridview1_customcolumndisplaytext);

    void Gridview1_customcolumndisplaytext (object sender, 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.