简体   繁体   English

c# 文本框 select 所有文本更改事件

[英]c# textbox select all textchanged event

I want to check the database table from txtSearch by entering barcode with txtSearch_TextChanged event, and after the txtSearch_TextChanged event I want to select all txtSearch text, it is work as well when the barcode is correct, also I would like to select all txtSearch when the barcode is not correct.我想通过使用 txtSearch_TextChanged 事件输入条形码来检查 txtSearch 中的数据库表,在 txtSearch_TextChanged 事件之后我想要 select 所有 txtSearch 文本,当条形码正确时它也可以工作,我也想 select 所有 txtSearch 当条形码不正确。

private void txtSearch_TextChanged(object sender, EventArgs e)
{
    try
    {

             String _pcode;
             double _price;
             double _qty;
            cn.Open();
            cm = new SqlCommand("select * from tblProduct where barcode like '" + txtSearch.Text + "'", cn);
            dr = cm.ExecuteReader();
            dr.Read();
            if (dr.HasRows)
            {
                _pcode = dr["pcode"].ToString();
                _price = double.Parse(dr["price"].ToString());
                _qty = double.Parse(txtQty.Text);
                dr.Close();
                cn.Close();
                AddToCart(_pcode, _price, _qty);
               txtSearch.SelectionStart = 0;
            txtSearch.SelectionLength = txtSearch.Text.Length;
            }
            else 
            {
                dr.Close();
                cn.Close();
            }
        }
    catch (Exception ex)
    {
        cn.Close();
        MessageBox.Show(ex.Message);
    }
}

Does it work when I type any barcode that it is in tblProduct and the all text from txtSearch will be selected, I want to the text from txtSearch will be Selected when any barcode is not in the tblProduct, I try this else { dr.Close();当我键入 tblProduct 中的任何条形码并且将选择 txtSearch 中的所有文本时它是否有效,我希望当任何条形码不在 tblProduct 中时选择来自 txtSearch 的文本,我试试这个 else { dr.Close (); cn.Close(); cn.关闭(); txtSearch.SelectionStart = 0; txtSearch.SelectionStart = 0; txtSearch.SelectionLength = txtSearch.Text.Length; txtSearch.SelectionLength = txtSearch.Text.Length; } it just select the first number, and I haven seen any error yet from the code.它只是第一个数字 select,我还没有从代码中看到任何错误。

    private void txtSearch_TextChanged(object sender, EventArgs e)
    {
        try
        {
            // Your code 
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        txtSearch.TextChanged -= txtSearch_TextChanged;

        txtSearch.Text = txtSearch.Tag + txtSearch.Text;
        txtSearch.Tag = txtSearch.Text;

        txtSearch.SelectionStart = 0;
        txtSearch.SelectionLength = txtSearch.Text.Length;

        txtSearch.TextChanged += txtSearch_TextChanged;
    }

    private void txtSearch_KeyDown(object sender, KeyEventArgs e)
    {
        switch (e.KeyCode)
        {
            case Keys.Back:
                //remove last character from txtSearch.Tag
                txtSearch.Tag = txtSearch.Tag != null && !string.IsNullOrWhiteSpace(txtSearch.Tag.ToString())
                                    ? txtSearch.Tag.ToString().Remove(txtSearch.Tag.ToString().Length - 1) 
                                    : string.Empty;
                break;
            case Keys.Delete:
                txtSearch.Tag = string.Empty;
                break;
                // handle other keys here
        }
    }

here is the solutions, thank you for your time这是解决方案,谢谢您的宝贵时间

private void txtSearch_TextChanged(object sender, EventArgs e) { try { if (txtSearch.Text.Trim().Length == 1) { tmrDelay.Enabled = true; private void txtSearch_TextChanged(object sender, EventArgs e) { try { if (txtSearch.Text.Trim().Length == 1) { tmrDelay.Enabled = true; tmrDelay.Start(); tmrDelay.Start(); tmrDelay.Tick += new EventHandler(tmrDelay_Tick); tmrDelay.Tick += new EventHandler(tmrDelay_Tick); } } catch (Exception ex) { MessageBox.Show(ex.Message); } } catch (Exception ex) { MessageBox.Show(ex.Message); } }

    }
  void tmrDelay_Tick(object sender, EventArgs e)
    {
        try
        {
            tmrDelay.Stop();
            string strCurrentString = txtSearch.Text.Trim().ToString();
            if (strCurrentString != "")
            {            
                String _pcode;
                double _price;
                double _qty;
                cn.Open();
                cm = new SqlCommand("select * from tblProduct where barcode like '" + txtSearch.Text + "'", cn);
                dr = cm.ExecuteReader();
                dr.Read();
                if (dr.HasRows)
                {
                    _pcode = dr["pcode"].ToString();
                    _price = double.Parse(dr["price"].ToString());
                    _qty = double.Parse(txtQty.Text);
                    dr.Close();
                    cn.Close();
                    AddToCart(_pcode, _price, _qty);
                }
                else
                {
                    MessageBox.Show("soory the bar code does not exist", "", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                dr.Close();
                cn.Close();
                txtSearch.Text = "";
            }
            txtSearch.Focus();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

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

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