Click here to Skip to main content
15,896,606 members
Articles / Programming Languages / C#

Simple Popup Control

Rate me:
Please Sign up or sign in to vote.
4.93/5 (167 votes)
26 Mar 2013LGPL35 min read 1M   24.6K   523  
How to create a custom pop-up control in C#.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PopupControl;

namespace MoreComplexPopup
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();

            /*PopupComboBox pcb = new PopupComboBox();
            pcb.DropDownControl = new ComplexPopup();
            Controls.Add(pcb);*/

            gridToolTip = new Popup(gridCustomToolTip = new CustomToolTip());
            gridToolTip.AutoClose = false;
            gridToolTip.FocusOnOpen = false;
            toolTip = new Popup(customToolTip = new CustomToolTip());
            toolTip.AutoClose = false;
            //toolTip.FocusOnOpen = false;
            complex = new Popup(complexPopup = new ComplexPopup());
            complex.Resizable = true;
            complexPopup.ButtonMore.Click += delegate(object _sender, EventArgs _e)
            {
                ComplexPopup cp = new ComplexPopup();
                cp.ButtonMore.Click += delegate(object __sender, EventArgs __e)
                {
                    new Popup(new ComplexPopup()).Show(__sender as Button);
                };
                new Popup(cp).Show(_sender as Button);
            };

            dataGridView.Columns.Add("col1", "col1");
            dataGridView.Columns.Add("col2", "col2");
            dataGridView.Columns.Add("col3", "col3");

            dataGridView.Rows.Add("1", "A", "!");
            dataGridView.Rows.Add("2", "B", "@");
            dataGridView.Rows.Add("3", "C", "#");
            dataGridView.CellMouseEnter += new DataGridViewCellEventHandler(dataGridView_CellMouseEnter);
            dataGridView.CellMouseLeave += new DataGridViewCellEventHandler(dataGridView_CellMouseLeave);
            dataGridView.CellMouseMove += new DataGridViewCellMouseEventHandler(dataGridView_CellMouseMove);
        }

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MainForm());
        }

        void dataGridView_CellMouseMove(object sender, DataGridViewCellMouseEventArgs e)
        {
            gridCustomToolTip.Controls[0].Text = "Mouse pointer location: " + e.Location.ToString();
        }

        void dataGridView_CellMouseLeave(object sender, DataGridViewCellEventArgs e)
        {
            gridToolTip.Close();
        }

        void dataGridView_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
        {
            gridCustomToolTip.Controls[1].Text = string.Format("Column: {0}, Row: {1}", e.ColumnIndex, e.RowIndex);
            if (!gridToolTip.Visible)
            {
                Rectangle rect = dataGridView.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
                rect.X += 32;
                rect.Y += 32;
                gridToolTip.Show(dataGridView, rect);
            }
        }

        Popup toolTip;
        CustomToolTip customToolTip;
        Popup gridToolTip;
        CustomToolTip gridCustomToolTip;
        Popup complex;
        ComplexPopup complexPopup;

        private void buttonDropDown_Click(object sender, EventArgs e)
        {
            complex.Show(sender as Button);
        }

        private void label1_MouseHover(object sender, EventArgs e)
        {
            toolTip.Show(label1);
        }

        private void label1_MouseLeave(object sender, EventArgs e)
        {
            toolTip.Close();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Software Developer
Poland Poland
I am a graduate of Wroclaw University of Science and Technology, Poland.

My interests: gardening, reading, programming, drawing, Japan, Spain.

Comments and Discussions