NEWS

[C#] Show Modal Winform like Bootstrap

[C#] Show Modal Winform like Bootstrap
Đăng bởi: Thảo Meo - Lượt xem: 1798 16:15:49, 10/05/2023C#   In bài viết

Xin chào các bạn, bài viết hôm nay mình tiếp tục hướng dẫn các bạn cách tạo hiệu ứng show Modal Effect giống Boottrap Website.

[C#] Show Modal Dialog Effect Like Bootstrap Winform

Dưới đây, video demo ứng dụng:

Source code FormMain.cs:

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

namespace modal
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public static int parentX, parentY;
        private void button1_Click(object sender, EventArgs e)
        {
            Form modalBackground = new Form();
            using (modalForm modal = new modalForm(this)) {

                modalBackground.StartPosition = FormStartPosition.Manual;
                modalBackground.FormBorderStyle = FormBorderStyle.None;
                modalBackground.Opacity = .50d;
                modalBackground.BackColor = Color.Black;
                modalBackground.Size = this.Size;
                modalBackground.Location = this.Location;
                modalBackground.ShowInTaskbar = false;
                modalBackground.Show();
                modal.Owner = modalBackground;

                parentX = this.Location.X;
                parentY = this.Location.Y;

                modal.ShowDialog();
                modalBackground.Dispose();


            }
        }
    }
}

Source code FormModal.cs

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

namespace modal
{
    public partial class modalForm : Form
    {
        public Form _parent;
        public modalForm(Form parent)
        {
            InitializeComponent();
            this._parent = parent;
            
        }
        public bool isShow = true;

        private void modalEffect_Timer_Tick(object sender, EventArgs e)
        {
            if (isShow)
            {
                if (Opacity >= 1)
                {
                    modalEffect_Timer.Stop();
                }
                else
                {
                    Opacity += .04;
                }

                int y = Form1.parentY += 6; //INCREMENT
                this.Location = new Point(childFormX, y);
                if (y >= i)
                {
                    modalEffect_Timer.Stop();
                }
            }
            else {
                if (Opacity <= 0)
                {
                    modalEffect_Timer.Stop();
                    this.Close();
                }
                else
                {
                    Opacity -= .04;
                }

                int y = Form1.parentY -= 6; //DECREMENT
                this.Location = new Point(childFormX, y);
                if (y == i)
                {
                    modalEffect_Timer.Stop();
                    this.Close();
                }
            }

        }
        int i, childFormX, childFormY;

        private void button2_Click(object sender, EventArgs e)
        {
            button1_Click(sender, e);   
        }

        private void modalForm_Load(object sender, EventArgs e)
        {
            int parentFormX = _parent.Location.X;
            int parentFormY = _parent.Location.Y;
            childFormX = parentFormX + (_parent.Width - this.Width) / 2;
            childFormY = parentFormY + (_parent.Height - this.Height) / 2;

            i = childFormY;
            this.Location = new Point(childFormX, childFormY);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            i = 0;
            isShow = false;
            modalEffect_Timer.Start();


           //this.Close();
        }
    }
}

Thanks for watching!

DOWNLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[C#] Show Modal Winform like Bootstrap
Đăng bởi: Thảo Meo - Lượt xem: 1798 16:15:49, 10/05/2023C#   In bài viết

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

Đọc tiếp
.