본문 바로가기
C#/DevExpress

DevExpress / Winform ComboBoxEdit 목록에 마우스를 가져다 댈 때 Tooltip을 출력

by HyunS_ 2019. 4. 7.

ComboBoxEdit 목록에 마우스를 가져다 대면 Tooltip이 출력됩니다.

 

using DevExpress.Utils.Win;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Popup;
using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApp2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            
            comboBoxEdit.Popup += comboBoxEdit_Popup;

            comboBoxEdit.Properties.Items.Add("1");
            comboBoxEdit.Properties.Items.Add("2");
            comboBoxEdit.Properties.Items.Add("3");
            comboBoxEdit.Properties.Items.Add("4");
        }

        #region comboBoxEdit_Popup(sender, e)
        /// <summary>
        /// 팝업을 출력합니다.
        /// </summary>
        /// <param name="sender">이벤트를 처리합니다.</param>
        /// <param name="e"></param>
        private void comboBoxEdit_Popup(object sender, EventArgs e)
        {
            PopupListBoxForm form = (sender as IPopupControl).PopupWindow as PopupListBoxForm;
            form.ListBox.MouseMove += listBox_MouseMove;
            form.ListBox.MouseLeave += listBox_MouseLeave;
        }

        #endregion

        #region listBox_MouseLeave(sender, e)
        /// <summary>
        /// 마우스 포인터가 떨어졌을 때 동작합니다.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox_MouseLeave(object sender, EventArgs e)
        {
            toolTipController.HideHint();
        }

        #endregion

        #region listBox_MouseMove(sender, e)
        /// <summary>
        /// 마우스 포인터가 움직일 때 발생합니다.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void listBox_MouseMove(object sender, MouseEventArgs e)
        {
            PopupListBox listBoxControl = sender as PopupListBox;
            ComboBoxEdit cmb = listBoxControl.OwnerEdit as ComboBoxEdit;
            int index = listBoxControl.IndexFromPoint(new Point(e.X, e.Y));
            if (index == -1)
            {
                toolTipController.HideHint();
            }
            else
            {
                string item = cmb.Properties.Items[index].ToString();
                toolTipController.ShowHint(item, listBoxControl.PointToScreen(new Point(e.X, e.Y)));
            }
        }

        #endregion
    }
}
728x90

댓글