NEWS

[C#] Tạo Modal Dialog Winform giống trên website

[C#] Tạo Modal Dialog Winform giống trên website
Đăng bởi: Thảo Meo - Lượt xem: 3467 09:07:42, 09/12/2020C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình sẻ tiếp tục hướng dẫn các bạn cách tạo Modal trên ứng dụng Winform giống Website.

[C#] Create Modal Dialog in Winform 

Chức năng ứng dụng, khi các bạn click vào một button hay bất kỳ đối tượng nào chúng ta sẽ Show Form Dialog đó lên.

Và đồng thời làm mờ Form Main.

Khi chúng ta click ra bên ngoài của Form Modal thì nó tự đóng lại.

Dưới đây là giao diện demo ứng dụng Modal Dialog winform C#:

modal_csharp_winform

Source code Form Dialog .cs:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DropDownDialog
{
    public partial class frmDialog : Form
    {     
        public frmDialog(Point startLocation)
        {
            InitializeComponent();
            this.Text = string.Empty;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.ControlBox = false;
            this.ShowInTaskbar = false;
            this.TopMost = true; 
            this.Capture = true; 
            this.Left = startLocation.X;
            this.Top = startLocation.Y;
            this.Load += FrmDialog_Load;
            this.FormClosing += FrmDialog_FormClosing;
        }

        private void FrmDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            Form1.Default.panel.Hide();
            Form1.Default.panel.SendToBack();
        }

        private void FrmDialog_Load(object sender, EventArgs e)
        {
            var data = new DataTable();
            data.Columns.Add("id", typeof(string));
            data.Columns.Add("name", typeof(string));
            data.Columns.Add("address", typeof(string));
            data.Rows.Add("MASV001", "Nguyễn Thảo", "Vũng Tàu");
            data.Rows.Add("MASV002", "Nguyễn Đình Tuyên", "Quảng Bình");
            data.Rows.Add("MASV003", "Nguyễn Phương Nhi", "Kiên Giang");
            data.Rows.Add("MASV004", "Nguyễn Thị Cẩm Tú", "TP.HCM");
            data.Rows.Add("MASV005", "Cái Trí Minh", "Đồng Nai");
            data.Rows.Add("MASV006", "Võ Sơn Băng", "Vĩnh Long");
            dataGridView1.DataSource = data;
        }



        protected override void OnMouseDown(MouseEventArgs e)
        {            
            if (this.RectangleToScreen(this.ClientRectangle).Contains(Cursor.Position))
                base.OnMouseDown(e); 
            else
                this.Close(); 
        }

       
        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                Form1.Default.BackColor = Color.Red;
            }
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton3.Checked)
            {
                Form1.Default.BackColor = Color.Blue;
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                Form1.Default.BackColor = Color.Green;
            }
        }
    }

   
}

Source code Form Main.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace DropDownDialog
{
    public partial class Form1 : Form
    {
        frmDialog dialog;
        public  GlassyPanel panel;
        private static Form1 _defaultInstance;
        public static Form1 Default
        {
            get
            {
                if (_defaultInstance == null || _defaultInstance.IsDisposed)
                {
                    _defaultInstance = new Form1();

                }
                return _defaultInstance;
            }
            set => _defaultInstance = value;
        }
        public Form1()
        {
            InitializeComponent();
            panel = new GlassyPanel();
            panel.Width = this.Width;
            panel.Height = this.Height;
            panel.Dock = DockStyle.Fill;
            this.Controls.Add(panel);
            panel.Hide();
            panel.SendToBack();
        }

        private void btnDropDown_Click(object sender, EventArgs e)
        {          
            Point location = this.PointToScreen(new Point(btnDropDown.Left, btnDropDown.Bottom));  
            dialog = new frmDialog(location);
            panel.Show();
            panel.BringToFront();
            dialog.Show();
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            if(dialog != null)
            {
                dialog.Close();
            }
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Tạo Modal Dialog Winform giống trên website
Đăng bởi: Thảo Meo - Lượt xem: 3467 09:07:42, 09/12/2020C#   In bài viết

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

Đọc tiếp
.