简体   繁体   English

在 C# Winforms 中同时自动完成两个文本框

[英]Autocomplete Two TextBoxes Simaltaneously in C# Winforms

I am in need of a push in the right direction.我需要朝着正确的方向前进。 I have a form which has 2 texboxes - one which has the Autocomplete functionality enabled and gets the data from a DataReader (txtBx1), and the other (txtBx2) which I am trying to complete using the value of txtBx1.我有一个有 2 个 texboxes 的表单——一个启用了自动完成功能并从 DataReader (txtBx1) 获取数据,另一个 (txtBx2) 我试图使用 txtBx1 的值来完成。

So, when I start typing into txtBx1, the autocomplete works as expected, but what I want is for txtBx2 to be populated at the same time with data from second column of the query used to retrieve the data.因此,当我开始输入 txtBx1 时,自动完成功能按预期工作,但我想要的是同时使用来自用于检索数据的查询的第二列的数据填充 txtBx2。

For instance, in txtBx1, I am typing in a Module Reference number, so I want the Module Name to be populated in txtBx2 whilst the automplete in txtBx1 is 'taking place' or once it 'completes'.例如,在 txtBx1 中,我输入了模块参考编号,因此我希望将模块名称填充到 txtBx2 中,而 txtBx1 中的自动完成正在“发生”或一旦“完成”。 So far what I have is;到目前为止,我所拥有的是;

 private void Form2_Load(object sender, EventArgs e)
        {

            OracleConnection oraCon = new OracleConnection("Data Source=(DESCRIPTION="
                             + "(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=xxxxxxxx)(PORT=1521)))"
                             + "(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME = TBL)));"
                              + "User Id=username;Password=password;");
            oraCon.Open();


            OracleCommand ocmodref = new OracleCommand(" SELECT module.reference as ModuleRef, module.name as ModuleName FROM TBL_Module module ", oraCon);
          //OracleCommand ocmodname = new OracleCommand(" SELECT module.name as ModuleName FROM TBL_Module module where module.reference = '" + txtBx1.Text + "' ", oraCon);


            OracleDataReader DRModRef = ocmodref.ExecuteReader();
            //OracleDataReader DRModName = ocmodname.ExecuteReader();

            AutoCompleteStringCollection automodreftext = new AutoCompleteStringCollection();
            //AutoCompleteStringCollection automodnametext = new AutoCompleteStringCollection();

            while (DRModRef.Read() )
            {
               
                automodreftext.Add(DRModRef.GetString(0));
                automodreftext.Add(DRModRef.GetString(1));
                //automodnametext.Add(DRModName.GetString(0));
            }
            
            txtBx1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtBx1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtBx1.AutoCompleteCustomSource = automodreftext;

            txtBx2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txtBx2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            txtBx2.AutoCompleteCustomSource = automodreftext;

            oraCon.Close();
        }

I have commented out the bits I tried earlier which didn't work but I have left them in there just in case you spot something I missed.我已经注释掉了我之前尝试过的那些不起作用的部分,但我把它们留在了那里,以防你发现我错过的东西。 Also, I have tried triggering the TextChanged event for txtBx1 so that when the text changes as the user searches for a Module Reference, this autocompletes txtBx2 with the name, but alas, no joy!此外,我尝试触发 txtBx1 的 TextChanged 事件,以便当用户搜索模块引用时文本发生更改时,这会自动完成 txtBx2 的名称,但唉,不高兴!

I'd be more than grateful if you could steer me in the right path.如果您能引导我走上正确的道路,我将不胜感激。

Best Regards,此致,

To display the matching Name to the selected Ref# and vise-versa Utilize TextChanged events as so:要显示与所选 Ref# 匹配的名称,反之亦然使用 TextChanged 事件,如下所示:

private void txtBx1_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    StringCollection customSource = tb.AutoCompleteCustomSource;
    int index = customSource.IndexOf(tb.Text);

    if (index >= 0)
    {
        txtBx2.Text = txtBx2.AutoCompleteCustomSource[index];
    }
    else
    {
        txtBx2.Text = tb.Text;
    }
}

private void txtBx2_TextChanged(object sender, EventArgs e)
{
    TextBox tb = (TextBox)sender;
    StringCollection customSource = tb.AutoCompleteCustomSource;
    int index = customSource.IndexOf(tb.Text);

    if (index >= 0)
    {
        txtBx1.Text = txtBx1.AutoCompleteCustomSource[index];
    }
    else
    {
        txtBx1.Text = tb.Text;
    }
}

just make sure you set txtBx2.AutoCompleteCustomSource = automodnametext and apply AutoComplete transform of your desire to the raw string.只需确保您设置txtBx2.AutoCompleteCustomSource = automodnametext并将您希望的 AutoComplete 转换应用于原始字符串。

The expected behavior would copy the text between the two textboxes whenever typing into only one;预期的行为会在只输入一个文本框时复制两个文本框之间的文本; and upon selecting/typing a matching string, the other textbox would also show the desired target string (if transformation is applied).并且在选择/键入匹配的字符串时,另一个文本框也将显示所需的目标字符串(如果应用了转换)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM