Click here to Skip to main content
15,897,519 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 SearchProviders : UserControl
    {
        public SearchProviders()
        {
            InitializeComponent();
        }

        private void radioButton_Click(object sender, EventArgs e)
        {
            SetColors(radioButton1);
            SetColors(radioButton2);
            SetColors(radioButton3);
            SetColors(radioButton4);
            (Parent as Popup).Close();
            if (ProviderChanged != null)
            {
                ProviderChanged(this, EventArgs.Empty);
            }
        }

        private void radioButton_MouseEnter(object sender, EventArgs e)
        {
            RadioButton radio = sender as RadioButton;
            radio.ForeColor = Color.White;
            radio.BackColor = SystemColors.HotTrack;
        }

        private void radioButton_MouseLeave(object sender, EventArgs e)
        {
            SetColors(sender as RadioButton);
        }

        private static void SetColors(RadioButton radioButton)
        {
            if (radioButton.Checked)
            {
                radioButton.ForeColor = SystemColors.HighlightText;
                radioButton.BackColor = SystemColors.Highlight;
            }
            else
            {
                radioButton.ForeColor = SystemColors.WindowText;
                radioButton.BackColor = SystemColors.Window;
            }
        }

        public event EventHandler ProviderChanged;

        public string SearchString
        {
            get
            {
                if (radioButton1.Checked)
                {
                    return "http://www.google.com/search?q=";
                }
                if (radioButton2.Checked)
                {
                    return "http://www.codeproject.com/info/search.asp?searchkw=";
                }
                if (radioButton3.Checked)
                {
                    return "http://en.wikipedia.org/w/index.php?title=Special:Search&search=";
                }
                return "http://search.msdn.microsoft.com/search/default.aspx?siteId=0&tab=0&query=";
            }
        }

        public string ProviderName
        {
            get
            {
                if (radioButton1.Checked)
                {
                    return "Google";
                }
                if (radioButton2.Checked)
                {
                    return "Code Project";
                }
                if (radioButton3.Checked)
                {
                    return "Wikipedia";
                }
                return "MSDN";
            }
        }

        public Image ProviderImage
        {
            get
            {
                if (radioButton1.Checked)
                {
                    return radioButton1.Image;
                }
                if (radioButton2.Checked)
                {
                    return radioButton2.Image;
                }
                if (radioButton3.Checked)
                {
                    return radioButton3.Image;
                }
                return radioButton4.Image;
            }
        }
    }
}

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