0

I have a form with a ComboBox, which is populated with 3 items.
When I add the statements: comboBox1.Text = "A"; and comboBox1.DroppedDown = true;
the first item of the drop-down list is automatically selected: the comboBox1.Text shows "Abc" in stead of "A".
Here is the code:

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 testComboBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        
            comboBox1 = new ComboBox();
            PopulateComboBox();            
            comboBox1.Location = new Point((this.Width - comboBox1.Width) / 2, 80);            
            this.Controls.Add(comboBox1);

            comboBox1.Text = "A";
            comboBox1.DroppedDown = true;
        }

        ComboBox comboBox1;

        private void PopulateComboBox()
        {
            comboBox1.Items.Add("Abc");
            comboBox1.Items.Add("Abcd");
            comboBox1.Items.Add("Abcde");
        }

        private void button_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

How can I disable the automatic selection of the first item in the Items collection of the ComboBox, so that the comboBox1.Text will show "A" and not "Abc"?
I am not looking for a one-time work-around. I need a GENERAL SOLUTION.

user2102327
  • 59
  • 2
  • 6
  • 19
  • Invert the assignments: `comboBox1.DroppedDown = true; comboBox1.Text = "A";`. Move these lines to the `Load` event / `OnLoad()` method. Or `OnShown()` (probably better). – Jimi Feb 19 '21 at 13:23
  • https://stackoverflow.com/questions/60107053/stop-changing-text-when-combobox-is-dropped-down/60123714#60123714 – Loathing Feb 19 '21 at 22:17
  • @Loathing I am very grateful to you for your comment. I saw in the thread: Prevent AutoSelect behavior of a ystem.Window.Forms.ComboBox (C#) that the extension class: ComboBoxAutoSelectEx really helped a couple of persons. I copied it and it compiles all right. I have to confess though that I have no idea what to do with it. Would you please be so kind as to post a few lines of code in order to exemplify how I may incorporate it into my program and use it in conjunction with comboBox1? Thank you so much for your good will, your effort and your time. – user2102327 Feb 20 '21 at 08:29

4 Answers4

0

set this code comboBox1.SelectedText = null;

public Form1()
{
    InitializeComponent();
        
   comboBox1 = new ComboBox();
   PopulateComboBox();            
   comboBox1.Location = new Point((this.Width - comboBox1.Width) / 2, 80);            
   this.Controls.Add(comboBox1);

   comboBox1.SelectedText = "A";
   comboBox1.DroppedDown = true;
   comboBox1.SelectedText = null;
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
0

With the help of the thread that Loathing pointed to, I copied the extension Class ComboBoxAutoSelectExtension and in the form I just added the line of code: ComboBoxAutoSelectExtension.AutoSelectOff(comboBox1);

user2102327
  • 59
  • 2
  • 6
  • 19
0

If you copy the ComboBoxAutoSelectEx from the link in the comment, then the only thing you should have to do in your own Form1 code is:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        
        comboBox1 = new ComboBox();

        String text = "A";
        comboBox1.Text = text;
        comboBox1.Select(text.Length,1); // put cursor at the end of text
        ComboBoxAutoSelectEx.AutoSelectOff(comboBox1); // Added

        PopulateComboBox();            
        comboBox1.Location = new Point((this.Width - comboBox1.Width) / 2, 80);            
        this.Controls.Add(comboBox1);

    }

    protected override void OnLoad(EventArgs e) { // Added
        base.OnLoad(e);
        comboBox1.DroppedDown = true;           
    }

    ComboBox comboBox1;

    private void PopulateComboBox()
    {
        comboBox1.Items.Add("Abc");
        comboBox1.Items.Add("Abcd");
        comboBox1.Items.Add("Abcde");
    }

    private void button_Exit_Click(object sender, EventArgs e)
    {
        this.Close();
    }
}
Loathing
  • 5,109
  • 3
  • 24
  • 35
0

I see no way to disable auto selection of an item. I can only replace the wrong selection with the input text that was previously known. This bug occurs on both opening or closing the dropdown. Here is an example code when opening the dropdown.

    // record
    box_txt = comboBox1.Text;
    box_pos = comboBox1.SelectionStart;

    // drop down
    comboBox1.DroppedDown = true;

    // replace
    comboBox1.Text = box_txt;
    comboBox1.SelectionStart = box_pos;
Ray Chakrit
  • 404
  • 5
  • 7