NEWS

[DEVEXPRESS] Hướng dẫn sử dụng Calendar Control và cho vào PopupEdit

[DEVEXPRESS] Hướng dẫn sử dụng Calendar Control và cho vào PopupEdit
Đăng bởi: Thảo Meo - Lượt xem: 4309 15:25:08, 30/07/2020C#   In bài viết

Xin chào các bạn, hôm nay mình sẽ hướng dẫn các bạn Custom calendar Control trong Devexpress C# Winform, và đưa vào Popup Edit.

[DEVEXPRESS] Custom Calendar Control C#

Dưới đây là giao diện demo ứng dụng:

calendar_control_demo

Khi các bạn chọn vào sẽ dễ dàng add những ngày lễ, ngày kỷ niệm của mình vào đây.

Full source code C# Calendar control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Calendar;
using DevExpress.Skins;
using DevExpress.Utils.Svg;

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

        private void InitCellDataProvider()
        {
            this.calendarControl2.CellStyleProvider = new MyCellStyleProvider();
        }
        public enum CellDataType { Undefined, Work, Family, Event }

        public class MyCustomCellData
        {
            public DateTime Date { get; set; }
            public SvgImage SvgGlyph { get; set; }
            public string InfoText { get; set; }
            public CellDataType CellType { get; set; }

            public string Description { get; set; }
            public bool SpecialDate { get; set; }
        }

        public class MyCellStyleProvider : ICalendarCellStyleProvider
        {

            List<MyCustomCellData> cellsCore;
            protected List<MyCustomCellData> Cells
            {
                get
                {
                    if (cellsCore == null)
                        cellsCore = CreateCells();
                    return cellsCore;
                }
            }

            protected virtual List<MyCustomCellData> CreateCells()
            {
                List<MyCustomCellData> res = new List<MyCustomCellData>();             

                res.Add(new MyCustomCellData() { Date = new DateTime(2020, 3, 28), CellType = CellDataType.Work, Description="SINH NHẬT\n THẢO MEO", InfoText = "TEST", SvgGlyph = Properties.Resources.Party });


                res.Add(new MyCustomCellData() { Date = new DateTime(2020, 3, 3), CellType = CellDataType.Event, InfoText = "TEST", Description="QUỐC TẾ\n PHỤ NỮ",SpecialDate= true, SvgGlyph = Properties.Resources.Flight });

                res.Add(new MyCustomCellData() { Date = new DateTime(2020, 3, 18), CellType = CellDataType.Family, InfoText = "TEST", Description="LIVERPOOL VS CHELSEA", SvgGlyph = Properties.Resources.Game });

                res.Add(new MyCustomCellData() { Date = new DateTime(2020, 3, 26), CellType = CellDataType.Family, InfoText = "TEST", Description = "THÀNH LẬP ĐOÀN",  SvgGlyph = Properties.Resources.Meeting });

                res.Add(new MyCustomCellData() { Date = new DateTime(2020, 3, 1), CellType = CellDataType.Undefined, InfoText = "TEST", Description = "ĐI SIÊU THỊ", SvgGlyph = Properties.Resources.Shopping });



                return res;
            }

            public MyCustomCellData GetCell(DateTime date)
            {
                return Cells.FirstOrDefault((c) => c.Date.Date == date.Date);
            }
            void ICalendarCellStyleProvider.UpdateAppearance(CalendarCellStyle cell)
            {
                MyCustomCellData cellInfo = GetCell(cell.Date);
                if (cellInfo == null)
                    return;

                cell.Description = cellInfo.Description;
                if (cell.Description != null)
                {
                    cell.DescriptionAppearance = (AppearanceObject)cell.Appearance.Clone();
                    cell.DescriptionAppearance.Font = new Font(cell.Appearance.Font.FontFamily, 7.0f, FontStyle.Bold);
                    cell.DescriptionAppearance.TextOptions.WordWrap = WordWrap.Wrap;
                }
                if (cell.State == DevExpress.Utils.Drawing.ObjectState.Normal)
                {
                    cell.Appearance.BackColor = GetCellColor(cellInfo, cell);
                    cell.Appearance.ForeColor = CheckForeColor(cell.Appearance.ForeColor, cell.Appearance.BackColor);
                }
                if (cellInfo.SpecialDate)
                    cell.Appearance.Font = new Font(cell.Appearance.Font.FontFamily, 20.0f, FontStyle.Bold);
            }
            Color CheckForeColor(Color foreColor, Color backColor)
            {
                if (backColor.A == 0 || foreColor.A == 0)
                    return foreColor;
                if (foreColor.R * 0.299 + foreColor.G * 0.587 + foreColor.B * 0.114 > 128)
                    return Color.Black;
                return foreColor;
            }

            protected virtual Color GetCellColor(MyCustomCellData cellData, CalendarCellStyle cellStyle)
            {
                switch (cellData.CellType)
                {
                    case CellDataType.Event:
                        return SchedulerSkins.GetSkin(cellStyle.PaintStyle.LookAndFeel).Colors.GetColor("ResourceColor02", Color.FromArgb(255, 209, 240, 253));
                    case CellDataType.Family:
                        return SchedulerSkins.GetSkin(cellStyle.PaintStyle.LookAndFeel).Colors.GetColor("ResourceColor03", Color.FromArgb(255, 229, 253, 177));
                    case CellDataType.Work:
                        return SchedulerSkins.GetSkin(cellStyle.PaintStyle.LookAndFeel).Colors.GetColor("ResourceColor04", Color.FromArgb(255, 255, 228, 239));
                }
                return Color.Empty;
            }
        }

        private void calendarControl2_ContextButtonCustomize(object sender, CalendarContextButtonCustomizeEventArgs e)
        {
            MyCellStyleProvider provider = (MyCellStyleProvider)this.calendarControl2.CellStyleProvider;
            MyCustomCellData data = provider.GetCell(e.Cell.Date);
            if (data == null || string.IsNullOrEmpty(data.InfoText))
            {
                e.Item.Visibility = ContextItemVisibility.Hidden;
                return;
            }
            e.Item.AllowGlyphSkinning = DefaultBoolean.True;
            e.Item.Tag = data;
            e.Item.ImageOptions.SvgImage = data.SvgGlyph;
        }

        private void calendarControl2_Click(object sender, EventArgs e)
        {  
           popup_date.ClosePopup();           
        }

  
        private void calendarControl2_DateTimeChanged(object sender, EventArgs e)
        {
            var value = calendarControl2.EditValue;
            popup_date.EditValue = value;
        }
    }
}

Thanks for watching!

DONWLOAD SOURCE

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[DEVEXPRESS] Hướng dẫn sử dụng Calendar Control và cho vào PopupEdit
Đăng bởi: Thảo Meo - Lượt xem: 4309 15:25:08, 30/07/2020C#   In bài viết

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

Đọc tiếp
.