C# 组合框的事件组合框\u TextChanged并不总是激发

C# 组合框的事件组合框\u TextChanged并不总是激发,c#,winforms,combobox,selectedindexchanged,C#,Winforms,Combobox,Selectedindexchanged,我有一个从组合框中选择整数值的输入应用程序。 随机函数有一个种子,因此每次运行应用程序时都会生成相同的值序列。 通过在ComboBox中键入input,如果前两位数字构成ComboBox.Items列表中包含的值,并添加第三位数字,使结果值不包含在ComboBox.Items列表中,则_availableValuesForSelection_TextChanged事件处理程序将拒绝此数字。前两位数字保持不变。问题是,如果同一个数字被加上几次,最终得到的值就会被接受。我的结论是,经过几次之后,组合

我有一个从组合框中选择整数值的输入应用程序。 随机函数有一个种子,因此每次运行应用程序时都会生成相同的值序列。 通过在ComboBox中键入input,如果前两位数字构成ComboBox.Items列表中包含的值,并添加第三位数字,使结果值不包含在ComboBox.Items列表中,则_availableValuesForSelection_TextChanged事件处理程序将拒绝此数字。前两位数字保持不变。问题是,如果同一个数字被加上几次,最终得到的值就会被接受。我的结论是,经过几次之后,组合框的Selection\u TextChanged事件“AvailableValues”不会触发。 尝试添加231,其中值23存在于ComboBox.Items列表中,也可以添加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();
    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间整数选择
{
公共部分类表单\u SelectInteger:表单
{
公共表单\u SelectInteger(列出可选择的值资源)
{
初始化组件();
_有效值=”;
_selectableValuesSource=selectableValuesSource;
_availableValuesForSelection=新建组合框();
//应该先设置源代码,然后设置自动完成模式,否则会引发NotSupportedException。
_availableValuesForSelection.BindingContext=新建BindingContext();//创建新上下文。
_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;
_选择的可用值。宽度=100;
_availableValuesForSelection.Location=新点((this.Width-_availableValuesForSelection.Width)/2150);
_availableValuesForSelection.DataSource=\u selectableValuesSource;//数据源应该是最后一行。
_availableValuesForSelection.SelectedIndex=5;
this.Controls.Add(\u availableValuesForSelection);
_按钮\选择完成被按下=错误;
_availableValuesForSelection.TextChanged+=新事件处理程序(\u availableValuesForSelection\u TextChanged);
_selectedValue=(int)_availableValuesForSelection.Items[_availableValuesForSelection.SelectedIndex];
}
void\u availableValuesForSelection\u TextChanged(对象发送方,事件参数e)
{
_textChangedEventDisabled=true;
字符串结果文本=_availableValuesForSelection.Text;
textBox1.Text=结果文本;
if(_availableValuesForSelection.FindString(结果文本)=-1)
{
while(_availableValuesForSelection.FindString(resultingText)=-1)
{
resultingText=resultingText.Substring(0,resultingText.Length-1);
}
_availableValuesForSelection.DroppedDown=false;
_availableValuesForSelection.SelectedIndex=\u availableValuesForSelection.FindString(结果文本);
_availableValuesForSelection.Text=结果文本;
_availableValuesForSelection.SelectionStart=\u availableValuesForSelection.Text.Length;
_availableValuesForSelection.SelectionLength=0;
}
_textChangedEventDisabled=false;
}
专用组合框_可用于选择的值;
私有列表_可选择的值资源;
私有整数_selectedValue;
公共整数选择值
{
得到
{
返回_selectedValue;
}
}
私有字符串_validValue;
bool\u textChangedEventDisabled=false;
按下私人布尔按钮选择完成;
所选索引的私有无效值已更改(对象发送方,事件参数e)
{
如果(发件人是组合框)
{
组合框显示ValuesForSelection=(组合框)发送方;
如果(\u按钮\u选择完成被按下)
{
_selectedValue=(int)displayValuesForSelection.Items[displayValuesForSelection.SelectedIndex];
DialogResult=DialogResult.OK;
}
}
}
私有无效按钮\u选择完成\u单击(对象发送者,事件参数e)
{
DialogResult=DialogResult.OK;//此行将DialogResult设置为OK,让另一个窗体知道它没有被强制关闭或取消。
_availableValuesForSelection.AutoCompleteMode=AutoCompleteMode.SuggestAppend;
_availableValuesForSelection.AutoCompleteSource=AutoCompleteSource.ListItems;
_availableValuesForSelection.DropDownStyle=ComboBoxStyle.DropDown;
_按钮\选择完成被按下=真;
_selectedValue=(int)_availableValuesForSelection.Items[_availableValuesForSelection.SelectedIndex];
这个。关闭();
}
}
}

使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Dat
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;
    }
}
_availableValuesForSelection.TextUpdate += new EventHandler(_availableValuesForSelection_TextChanged);