- [DEVEXPRESS] Chia sẻ source code cách tạo biểu đồ sơ đồ tổ chức công ty Org Chart trên Winform C#
- [C#] Hướng dẫn tạo Auto Number trên Datagridview winform
- [DATABASE] Hướng dẫn tạo Procedure String Split in Mysql
- [C#] Thiết lập dấu (,) hay dấu (.) ở định dạng số đúng với định dạng số Việt Nam
- [C#] Chia sẻ source code Game Spin Lucky Wheel
- [C#] Hướng dẫn Encode and Decode HTML
- Hướng dẫn tạo tài khoản Chat Open AI GPT tại Việt Nam
- [C#] Hướng dẫn thay đổi giao diện ứng dụng Winform theo giao diện của Windows
- [VB.NET] Hiệu ứng Acrylic, Mica, Tabbed Blur Effect trên Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên Combobox Edit
- [C#] Chia sẻ source code Orange Rain in Winform
- [DEVEXPRESS] Hướng dẫn sử dụng HTML Template trên XtraMessageBox Winform Devexpress 22.2.3
- [DEVEXPRESS] Hướng dẫn sử dụng HTML and CSS Code Viewer trên Winform
- [C#] Number Effect Counter up and down in winform
- [C#] Hướng dẫn Supend and Resume Process ID in Winform
- [C#] Hiển thị line number trên Richtextbox Winform
- [C#] Fake Blue Screen BSOD in winform
- [C#] Chia sẽ code demo sử dụng Async Parallel Foreach and For in Winform
- [C#] Sử dụng ActionBlock run X task at time winform
- [C#] Hướng dẫn sử dụng Property Grid để lưu và tải lại thông tin cấu hình user trên winform
[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!