NEWS

[DEVEXPRESS] Hướng dẫn nhúng Usercontrol vào hộp thoại XtraDialog

[DEVEXPRESS] Hướng dẫn nhúng Usercontrol vào hộp thoại XtraDialog
Đăng bởi: Thảo Meo - Lượt xem: 5176 14:48:09, 25/09/2020DEVEXPRESS   In bài viết

Xin chào các bạn, bài viết sẻ hướng dẫn các bạn cách sử dụng XtraDialog để mở một xtraUserControl như một Dialog Result trong Devexpress C#.

[DEVEXPRESS] Using XtraDialog Usercontrol C#

Giao diện demo ứng dụng sử dụng XtraEditor:

xtra_dialog_devexpress

Các bạn thấy ở hình trên cái form show Dialog là một UserControl, và mình nhét nó vào Xtradialog 

usercontrol_dev

Cái XtraDialog này hoạt động giống thằng XtraMessageDialog trong Devexpress

Đầu tiên các bạn tạo 1 class StudentItem.cs

Dùng để gởi nó vào contructor của userControl, để khi chúng ta bấm OK, sẽ trả dữ liệu về qua phương thức DataBindding.

  1. File StudentItem.cs
namespace UsingXtraDialog
{
    public class StudentItem
    {
        public int ID { set; get; }
        public string Name { set; get; }
        public string Address { set; get; }
    }
}

2. File uc_student usercontrol:

namespace UsingXtraDialog
{
    public partial class uc_Student : DevExpress.XtraEditors.XtraUserControl
    {
        public uc_Student(StudentItem studentItem)
        {
            InitializeComponent();
            txt_id.DataBindings.Add("EditValue", studentItem, "ID");
            txtName.DataBindings.Add("EditValue", studentItem, "Name");
            txtAddress.DataBindings.Add("EditValue", studentItem, "Address");
        }
    }
}

3. Form Main

namespace UsingXtraDialog
{
    public partial class Form1 : DevExpress.XtraEditors.XtraForm
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btn_showDialog_Click(object sender, EventArgs e)
        {
            var studentItem = new StudentItem();
            var myControl = new uc_Student(studentItem);
            DialogResult result = DevExpress.XtraEditors.XtraDialog.Show(this, myControl, "Demo XtraDialog - https://laptrinhvb.net", MessageBoxButtons.OKCancel);
            if (result == DialogResult.OK)
            {
                var id = studentItem.ID;
                var name = studentItem.Name;
                var address = studentItem.Address;

                var infoStudent = $"ID: {id}\r\nName: {name}\r\nAddress: {address}";
                rtf_result.Text = infoStudent;
            }
                
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn nhúng Usercontrol vào hộp thoại XtraDialog
Đăng bởi: Thảo Meo - Lượt xem: 5176 14:48:09, 25/09/2020DEVEXPRESS   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.