Author Topic: DoubleClick Event for the ComboBox Control.  (Read 2004 times)

0 Members and 1 Guest are viewing this topic.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
DoubleClick Event for the ComboBox Control.
« on: January 13, 2008, 09:17:17 PM »
While doing some research for this ; Using a Palette for testing Routines.
http://www.theswamp.org/index.php?PHPSESSID=2g7tgnalp0qkm01kumcc1odt32&topic=20868.0

I realised that the DoubleClick Event does not fire for the ComboBox Control.

This workaround is worth posting for posterity ..

If the code is not self explanatory, just yell :-)

The solution is attached.
This is the FormCode  ComboTestForm1.cs

// CodeHimBelongaKwb ©  Jan 2008

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace Combo_1
{
    public partial class ComboTestForm1 : Form
    {
        public ComboTestForm1()
        {
            InitializeComponent();
        }
        // Create a new dictionary of Description strings,
        // with 'Command' string keys.
        //
        public Dictionary<string, string> commandStuff =
            new Dictionary<string, string>();

        private void Form1_Load(object sender, EventArgs e)
        {           
            commandStuff.Add("Command_B1",
                "Command_B1 : Brief Description goes here.");
            commandStuff.Add("Command_B2",
                "Command_B2 : Brief Description goes here.");
            commandStuff.Add("Command_B3",
                "Command_B3 : Brief Description goes here.");
            commandStuff.Add("Command_C4",
                "Command_C4 : Brief Description goes here.");
            commandStuff.Add("Command_C5",
                "Command_C5 : Brief Description goes here.");

            this.textBox1.Text = "Select a Command Name from ComboBox";

            foreach (KeyValuePair<string, string> kvp in commandStuff)
            {
                this.comboBox1.Items.Add(kvp.Key);                   
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.textBox1.Text = commandStuff[comboBox1.SelectedItem.ToString()];
            this.textBox2.Clear();
        }

        //  Record the last click as now
        DateTime lastClick = DateTime.Now;
        // This is a hack to allow the Combo Double click to be identified.
        // Set the Combo MouseClick Event to call this when fired.
        private void ComboBox1_MouseClick(object sender, MouseEventArgs e)
        {
            //  Test if in the last time plus the milliseconds it takes for a double click
            //  if the differentiation is less than (SystemInformation.DoubleClickTime)
            //  activate the code, as there was a double click.
            if ((DateTime.Now < lastClick.AddMilliseconds(SystemInformation.DoubleClickTime)))
            {
                this.textBox2.Text = comboBox1.SelectedItem.ToString(); 
            }
            //  Record the last click as now again
            lastClick = DateTime.Now;
        }

        // THIS IS REDUNDANT !!
        //
        private void comboBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.textBox2.Text = comboBox1.SelectedItem.ToString();           
        }
        //
        private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter | e.KeyChar == (char)Keys.Space)
            {
                this.textBox2.Text = comboBox1.SelectedItem.ToString();
                e.Handled = true;
            }             
        }
    }
}

« Last Edit: January 13, 2008, 09:21:13 PM by Kerry Brown »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.