ComboBox: the selected value remains in focus (blue)

I made a small sample program to illustrate my problem. The program consists of one form with one TableLayoutPanel: tableLayoutPanel1 one NumericUpDown: coins and one button: flip. When flip is pressed, pressing tableLayoutPanel1 is dynamically filled with coins. Add lines. Each line consists of one ComboBox with the "Heads" and "Tails" elements. One of these elements is 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. blue. How to stop this? I tried calling coin.SelectionLength = 0; but this does not work :-(.

For any answers in advance!

+3
4

DropDownStyle DropDownList . , , ( , ).

coin.DropDownStyle = ComboBoxStyle.DropDownList;
+4

:

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);
        }
    }
}

: ComboBox Resize Oddity

+3

:

coin.CanFocus = false;

"" , - , :

coin.TopLevelControl.Focus();

1. , , , , , , ; , SelectedIndexChanged, :

EDIT 2: EDIT 1. , !

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 line " this.ActiveControl = null;" of the SelectedIndexChanged event. Happy coding!

+2
source

Just use the following code for all ComboBoxes that you don’t want the text to be selected when resizing:

/// <summary>
/// Stop resizing of the Form from selecting ComboBox text.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <remarks>Used for all ComboBox'es</remarks>
private void cbo_Resize(object sender, EventArgs e)
{
    ((ComboBox)sender).Select(0, 0);
}
0
source

Source: https://habr.com/ru/post/1706722/


All Articles