tags:

views:

406

answers:

3

Hello all, I have made a small example program to illustrate my problem. The program consist of one form with one TableLayoutPanel: tableLayoutPanel1 one NumericUpDown: coins and one Button: flip. When flip is pressed the tableLayoutPanel1 is dynamically filled with coins.Value rows. Each row consist of one ComboBox with the items "Heads" and "Tails". One of these items are selected by drawing a random number for each row. The code is as follows:

    namespace WindowsApplication1
    {
      public partial class Form1 : Form
      {
        public Form1()
        {
          InitializeComponent();     
        }


        private void draw_Click(object sender, EventArgs e)
        {
          tableLayoutPanel1.Controls.Clear();
          string[] results = { "Heads", "Tails" };
          int nCoins = Convert.ToInt32(coins.Value);
          Random random = new Random();
          for(int i = 0; i < nCoins; i++) {
            ComboBox coin = new ComboBox();
            coin.Items.AddRange(results);
            coin.SelectedIndex = random.Next(0, 2);
            tableLayoutPanel1.Controls.Add(coin, 0, i);
          }
       }
    }
 }

My problem is that the selected item in each ComboBox has focus, i.e. is blue. How do I stop this from happening? I have tried calling coin.SelectionLength = 0; but this does not work :-(.

Thaks in advance for any answers!

A: 

Try putting this before you set the selected item:

coin.CanFocus = false;

Or "blur" the element by focusing something else after you've set the selected item:

coin.TopLevelControl.Focus();

EDIT 1: I'm not sure if this will work for you in your situation, but I tested this out and it seems to do what you wish, or mostly; you have to click an item in the list to trigger the SelectedIndexChanged event, but it's closer nonetheless:

EDIT 2: I removed the code from EDIT 1. I think this works now!

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            button1.Click += new EventHandler(button1_Click);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            tableLayoutPanel1.Controls.Clear();
            string[] results = { "Heads", "Tails" };
            int nCoins = Convert.ToInt32(coins.Value);
            Random random = new Random();
            for (int i = 0; i < nCoins; i++)
            {
                ComboBox coin = new ComboBox();
                coin.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
                coin.Items.AddRange(results);
                coin.SelectedIndex = random.Next(0, 2);
                tableLayoutPanel1.Controls.Add(coin, 0, i);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ActiveControl = null;
        }

    }
}

The key is in the "this.ActiveControl = null;" line of the SelectedIndexChanged event. Happy Coding!

Cory Larson
Thanks Cory for your answer, but I do not think this works.I am not allowed to set (only get) the CanFocus property.Also I added the coin.TopLevelControl.Focus();call after tableLayoutPanel1.Controls.Add(coin, 0, i);but this did not have any effect.Andreas
A: 

You should set the DropDownStyle to DropDownList like this. Then it never shows a blue highlight even if it has the focus because the text is not editable (which is what you want anyway).

coin.DropDownStyle = ComboBoxStyle.DropDownList;
JayMcClellan
Thanks Jay,this works but the problem is that in my real application I need to be able to edit the text.Andreas
A: 

Try this:

public class CustomComboBox : ComboBox
{
    private const int WM_SIZE = 0x0005;

    protected override void WndProc(ref Message m)
    {
        base.WndProc(ref m);

        // A fix for ComboBoxStyle.DropDown mode.
        if (DropDownStyle == ComboBoxStyle.DropDown
            && (m.Msg & WM_SIZE) == WM_SIZE)
        {
            Select(0, 0);
        }
    }
}

See: ComboBox Resize Oddity

Eren Aygunes