0
votes

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.

3
Invert the assignments: comboBox1.DroppedDown = true; comboBox1.Text = "A";. Move these lines to the Load event / OnLoad() method. Or OnShown() (probably better).Jimi
@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

3 Answers

0
votes

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;
}
0
votes

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

0
votes

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