ComboBox的事件comboBox_TextChanged并不总是触发

时间:2019-11-01 17:05:23

标签: c# winforms combobox selectedindexchanged

我有一个输入应用程序,可以从ComboBox中选择一个整数值。 随机函数具有种子,因此每次运行应用程序时都会生成相同的值序列。 通过对ComboBox进行TYPING输入,如果前2个数字构成ComboBox.Items列表中包含的值,并且添加第三个数字,以使结果值不在ComboBox.Items列表中,则_availableValuesForSelection_TextChanged事件处理程序将拒绝该数字。保留前两位数字。问题是,如果多次添加相同的数字,最终将接受结果值。我的结论是,几次后ComboBox的事件_availableValuesForSelection_TextChanged不会触发。 尝试添加231,其中ComboBox.Items列表中的值23存在,而230但不存在231。 代码是:

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 IntegerSelection
{
    public partial class Form_SelectInteger : Form
    {
        public Form_SelectInteger(List<int> selectableValuesSource)
        {
            InitializeComponent();

            _validValue = "";
            _selectableValuesSource = selectableValuesSource;
            _availableValuesForSelection = new ComboBox();
            // The Source should be set first and then the AutoCompleteMode, otherwise it throws a NotSupportedException.
        _availableValuesForSelection.BindingContext = new BindingContext(); // Create a new context.
        _availableValuesForSelection.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        _availableValuesForSelection.AutoCompleteSource = AutoCompleteSource.ListItems;
        _availableValuesForSelection.DropDownStyle = ComboBoxStyle.DropDown;
        _availableValuesForSelection.DroppedDown = false;
        _availableValuesForSelection.DrawMode = DrawMode.Normal;
        _availableValuesForSelection.Enabled = true;
        _availableValuesForSelection.BackColor = Color.LightBlue;
        _availableValuesForSelection.ForeColor = Color.DarkBlue;
        _availableValuesForSelection.IntegralHeight = false;
        _availableValuesForSelection.MaxDropDownItems = 1;
        _availableValuesForSelection.MaxDropDownItems = 80;
        _availableValuesForSelection.DropDownHeight = 510;
        _availableValuesForSelection.DropDownWidth = 50;
        _availableValuesForSelection.TabStop = true;
        _availableValuesForSelection.Visible = true;
        _availableValuesForSelection.Width = 100; 
        _availableValuesForSelection.Location = new Point((this.Width - _availableValuesForSelection.Width) / 2, 150);
        _availableValuesForSelection.DataSource = _selectableValuesSource; // DataSource should be the last line.
        _availableValuesForSelection.SelectedIndex = 5;
        this.Controls.Add(_availableValuesForSelection);

        _button_SelectionCompleteWasPressed = false;

        _availableValuesForSelection.TextChanged += new EventHandler(_availableValuesForSelection_TextChanged);  

        _selectedValue = (int)_availableValuesForSelection.Items[_availableValuesForSelection.SelectedIndex];
    }

    void _availableValuesForSelection_TextChanged(object sender, EventArgs e)
    {
            _textChangedEventDisabled = true;
            string resultingText = _availableValuesForSelection.Text;
            textBox1.Text = resultingText;
            if (_availableValuesForSelection.FindString(resultingText) == -1)
            {
                while (_availableValuesForSelection.FindString(resultingText) == -1)
                {
                    resultingText = resultingText.Substring(0, resultingText.Length - 1);
                }
                _availableValuesForSelection.DroppedDown = false;                    
                _availableValuesForSelection.SelectedIndex = _availableValuesForSelection.FindString(resultingText);
                _availableValuesForSelection.Text = resultingText;
                _availableValuesForSelection.SelectionStart = _availableValuesForSelection.Text.Length;
                _availableValuesForSelection.SelectionLength = 0;                    
            }
            _textChangedEventDisabled = false;            
    }

    private ComboBox _availableValuesForSelection;
    private List<int> _selectableValuesSource;

    private int _selectedValue;
    public int SelectedValue
    {
        get
        {
            return _selectedValue;
        }
    }

    private string _validValue;
    bool _textChangedEventDisabled = false;

    private bool _button_SelectionCompleteWasPressed;

    private void ValuesForSelection_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (sender is ComboBox)
        {
            ComboBox displayValuesForSelection = (ComboBox)sender;
            if (_button_SelectionCompleteWasPressed)
            {
                _selectedValue = (int)displayValuesForSelection.Items[displayValuesForSelection.SelectedIndex];
                DialogResult = DialogResult.OK;
            }
        }
    }

    private void button_SelectionComplete_Click(object sender, EventArgs e)
    {
        DialogResult = DialogResult.OK; // This line sets the DialogResult to OK to let the other form know that it wasn't forced closed or canceled.
        _availableValuesForSelection.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
        _availableValuesForSelection.AutoCompleteSource = AutoCompleteSource.ListItems;
        _availableValuesForSelection.DropDownStyle = ComboBoxStyle.DropDown;
        _button_SelectionCompleteWasPressed = true;
        _selectedValue = (int)_availableValuesForSelection.Items[_availableValuesForSelection.SelectedIndex];
        this.Close();
    }
}

}

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 IntegerSelection
{
    public partial class Form_Main : Form
    {
    Random rnd = new Random(0);

    public Form_Main()
    {
        InitializeComponent();

        PopulateValuesForSelection();
        Form_SelectInteger integerSelection = new Form_SelectInteger(_selectableValues);
        DialogResult result = integerSelection.ShowDialog(); // This line calls it as a Modal dialog, and retrieves its response once it's closed.
        if (result == DialogResult.OK) // Test to see if the DialogResult was "OK". 
        {
             _selectedValue = integerSelection.SelectedValue;
             textBox1.ReadOnly = true;
            textBox1.Text = _selectedValue.ToString();
        }
        else
        {
            this.Close();
            Application.Exit();
        }
    }

    private List<int> _selectableValues;
    private int _selectedValue;

    /// <summary>
    /// The values included into _selectableValues must be unique (no duplicates are allowed).
    /// </summary>
    private void PopulateValuesForSelection()
    {
        _selectableValues = new List<int>();
        HashSet<int> uniqueValues = new HashSet<int>();
        int i = 0;
        while (i < 256)
        { 
            int currentValue = rnd.Next(0, 1000);
            List<int> constituentDigits = SplitNumberIntoDigits(currentValue);
            HashSet<int> digits = new HashSet<int>(constituentDigits);
            if (!constituentDigits.Contains(-1) && uniqueValues.Add(currentValue))
            {
                i = uniqueValues.Count;
                _selectableValues.Add(currentValue);
            }
        }
        _selectableValues.Sort();
    }

    private List<int> SplitNumberIntoDigits(int number)
    {
        List<int> constituentDigits = new List<int>();
        while (number > 0)
        {
            int digit = number % 10; // Get the last digit in the number.
            constituentDigits.Insert(0, digit); // Insert the digit in the first location in the list, as it is the digit preceding the digit that is now in the first location in the list.
            number = number / 10; // Shorten the integer by one digit, from the right (the least significant digit).
        }

        return constituentDigits;
    }
}

}

请帮助。 预先谢谢你。

1 个答案:

答案 0 :(得分:1)

更新Text属性值时,引发TextChanged事件。研究了这一点之后,当您在输入无效字符后拒绝该值时,该值仍存储在对象内部的某个位置,并且在重复尝试时不会触发该事件。

鉴于每次在显示文本之前对其进行更改,都会触发TextUpdate事件。这用于验证文本输入,并且当以编程方式更改Text属性时不会触发。听起来正是您想要的。

查看文档: TextUpdate Event

您的代码将只需要更改一行即可工作:

_availableValuesForSelection.TextUpdate += new EventHandler(_availableValuesForSelection_TextChanged);