- [VB.NET] Lấy địa chỉ Bios Serial Number trên Winform
- [C#] Giới thiệu và sử dụng thư viện AutoMapper
- [DEVEXPRESS] Hướng dẫn Custom Summary in Footer trong Gridview C#
- [C#] Dependency Injection in Winform
- [SQLSERVER] Hướng dẫn tìm kiếm nâng cao trên sql
- [C#] Hướng dẫn sử dụng SetTimeOut trên Winform like Javascript
- [DATABASE] In cây thông noel bằng sqlserver
- [C#] Hướng dẫn fix lỗi hiển thị UTF-8 khi sử dụng WebClient Download String
- [DATABASE] Hướng dẫn mã hóa và giải mã sử dụng thuật toán AES 256 trên sqlserver
- [DATABASE] Base64 Encode and Decode trong Sqlserver
- [C#] Vì Mẹ anh bắt phải Fake địa chỉ MacAddress
- [C#] Hướng dẫn xuất dữ liệu từ DataGridview ra file Excel
- [C#] Hướng dẫn khởi động lại chương trình ứng dụng winform
- [C#] Sự khác nhau giữa String.IsNullOrEmpty và String.IsNullOrWhiteSpace
- [C#] Hướng dẫn đọc file hình ảnh định dạng WEBP và chuyển đổi WebP sang JPG
- [C#] Kiểm tra phiên bản Microsoft Office đang sử dụng trên máy tính
- [C#] Hướng dẫn chuyển đổi tập tin hình ảnh XPS sang Bitmap
- [C#] Giới thiệu Component WebView2 của Microsoft
- [C#] Hướng dẫn lưu tất cả hình ảnh từ File Excel vào thư mục window
- [DATABASE] Hướng dẫn import và export hình ảnh image từ Sqlserver
[C#] Tạo Modal Dialog Winform giống trên website
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#:
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!