Click here to Skip to main content
15,896,453 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.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using PopupControl;

namespace PopupTest
{
    public partial class SearchControl : UserControl
    {
        SearchProviders searchProviders;
        Popup popup;

        public SearchControl()
        {
            InitializeComponent();
            popup = new Popup(searchProviders = new SearchProviders());
            borderLabel.Image = searchProviders.ProviderImage;
            borderLabel.Text = searchProviders.ProviderName;
            popup.Closed += popup_Closed;
            searchProviders.ProviderChanged += fireSearch_Event;
        }

        public event EventHandler<StringEventArgs> Search;

        private void popup_Closed(object sender, ToolStripDropDownClosedEventArgs e)
        {
            if (!textBox.Visible)
            {
                borderLabel.Image = searchProviders.ProviderImage;
            }
            borderLabel.Text = searchProviders.ProviderName;
        }

        private void borderLabel_MouseDown(object sender, MouseEventArgs e)
        {
            textBox.Visible = true;
            textBox.Focus();
        }

        private void buttonDropDown_Click(object sender, EventArgs e)
        {
            popup.Width = Width;
            popup.Show(this);
        }

        private void textBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = false;
            if (e.KeyChar == '\r')
            {
                e.Handled = true;
                FireSearch();
            }
        }

        private void fireSearch_Event(object sender, EventArgs e)
        {
            FireSearch();
        }

        private void FireSearch()
        {
            if (textBox.Text.Length > 0 && Search != null)
            {
                Search(this, new StringEventArgs(searchProviders.SearchString + textBox.Text.Replace(' ', '+')));
            }
        }

        private void textBox_Enter(object sender, EventArgs e)
        {
            borderLabel.Image = null;
        }

        private void textBox_Leave(object sender, EventArgs e)
        {
            if (textBox.Text.Length == 0)
            {
                borderLabel.Image = searchProviders.ProviderImage;
                textBox.Visible = false;
            }
        }
    }
}

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