Back to Devexpress

XtraUserControl

windowsforms-114564-controls-and-libraries-forms-and-user-controls-xtrausercontrol.md

latest3.3 KB
Original Source

XtraUserControl

  • Mar 14, 2021
  • 2 minutes to read

The XtraUserControl class replaces the standard System.Windows.Forms.UserControl component. It provides the capability to create a stand-alone module populated with controls and components which can be reused throughout your application.

csharp
//Create an XtraUserControl that represents a login form
public class LoginUserControl : XtraUserControl {
    public LoginUserControl() {
    LayoutControl lc = new LayoutControl();
    lc.Dock = DockStyle.Fill;
    TextEdit teLogin = new TextEdit();
    TextEdit tePassword = new TextEdit();
    CheckEdit ceKeep = new CheckEdit() { Text = "Keep me signed in" };
    lc.AddItem(String.Empty, teLogin).TextVisible = false;
    lc.AddItem(String.Empty, tePassword).TextVisible = false;
    lc.AddItem(String.Empty, ceKeep);
    this.Controls.Add(lc);
    this.Dock = DockStyle.Fill;
    }
}

//Show an XtraUserControl-based login form inside a dialog
private void simpleButton1_Click(object sender, EventArgs e) {
    LoginUserControl myControl = new LoginUserControl();
    DevExpress.XtraEditors.XtraDialog.Show(myControl, "Sign in", MessageBoxButtons.OKCancel);
}
vb
'create an XtraUserControl that represents a login form
Public Class LoginUserControl
  Inherits XtraUserControl

    Public Sub New()
    Dim lc As New LayoutControl()
    lc.Dock = DockStyle.Fill
    Dim teLogin As New TextEdit()
    Dim tePassword As New TextEdit()
    Dim ceKeep As New CheckEdit() With {.Text = "Keep me signed in"}
    lc.AddItem(String.Empty, teLogin).TextVisible = False
    lc.AddItem(String.Empty, tePassword).TextVisible = False
    lc.AddItem(String.Empty, ceKeep)
    Me.Controls.Add(lc)
    Me.Dock = DockStyle.Fill
    End Sub
End Class

'show an XtraUserControl-based login form inside a dialog
Private Sub simpleButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim myControl As New LoginUserControl()
    DevExpress.XtraEditors.XtraDialog.Show(myControl, "Sign in", MessageBoxButtons.OKCancel)
End Sub

To add XtraUserControls at design-time, right-click your project and select the “Add DevExpress Item” option. This invokes the Template Gallery that allows you to add blank XtraUserControls and DevExpress forms, as well as template-based forms.

When compared to a standard WinForms UserControl, the XtraUserControl provides the following advantages:

  • End-users can scroll the XtraUserControl content when hovering it, without needing to focus it first;
  • Communicates with the Layout and Data Layout Controls component to pass the correct control sizes;
  • Allows you to utilize smart tags and Designer dialogs for controls inside an XtraUserControl (or its descendant) directly from a form that hosts this container (visual inheritance);
  • Supports DevExpress skins and ensures look-and-feel consistency across the application.