在Windows应用程序上使用Enter键进行搜索? [英] searching using enter key on windows application?

查看:80
本文介绍了在Windows应用程序上使用Enter键进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,

我是c#的新手,当用户在文本框中键入要搜索的内容时,我只有一个文本框和一个gridview,单击搜索按钮时,Gridview会成功填充.
但我担心的是搜索按钮的使用,请使用Enter键在gridview中搜索数据.


请帮助我完善这个

提前thx !!!!

Dear All,

I am new to c# , i have one text box and one gridview when user type something in textbox to search, the gridview is filled succesfully when clicked on search button .
but my concern is instaed of search button use ENTER KEY TO search data in gridview.


pls help me to compelete this

thx in advance !!!!

推荐答案



您的WebForm代码为

Hi,

Your WebForm code is

<div>

           <asp:TextBox ID="TextBox1" onkeypress="return EnterEvent(event)" runat="server"></asp:TextBox>
           <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
           <br />
           <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

       </div>



你应该写一个js函数



and you should write a js function

<script>
        function EnterEvent(e) {
            if (e.keyCode == 13) {
                __doPostBack('<%= Button1.ClientID %>', "");
            }
        }
    </script>



而且按钮点击代码很简单,



And Button Click code is simple,

protected void Button1_Click(object sender, EventArgs e)
      {
          Label1.Text = "Search completed";
      }




您可以像这样捕获Enter KeyPress.

问候




You can catch Enter KeyPress like that.

Regards


这是我在datagridview中搜索数字值的代码:
This is the code how I search for a number value in a datagridview:
private void SearchProject()
{
    int rowIndex = -1;
    bool found = false;

    dgvProjects.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
    try
    {
        //Set a string to search for
        int searchValue = int.Parse(tbSearchValue.Text);

        foreach (DataGridViewRow row in dgvProjects.Rows)
        {
            int compareValue = int.Parse(row.Cells[2].Value.ToString());

            if (compareValue.Equals(searchValue))
            {
                found = true;
                rowIndex = row.Index;
                dgvProjects.Rows[row.Index].Selected = true;
                dgvProjects.FirstDisplayedScrollingRowIndex = rowIndex;
                break;
            }
        }

        if (!found)
            MessageBox.Show("Project number not found.\n\nBe sure you're searching for the right project number.", "Project not found",
                MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
    }
    catch (FormatException)
    {
        MessageBox.Show("Your input is not a number.", "Input Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}


row.Cells [2]是要查看的列.因此,我正在搜索用户在文本框中第3列的所有行(foreach块)中输入的值(它们基于零,因此我使用值[2]). br/>
调用按钮上的SearchProject()单击事件:


row.Cells[2] is the column where to look at. So I am searching for values, entered by a user in a textbox, in all rows (the foreach block)in the 3rd column (they are zero-based, for that I''m using value [2]).

Call the SearchProject() on a button Click Event:

private void btnSearch_Click(object sender, EventArgs e)
{
    SearchProject();
}


或当用户在文本框中按Enter时:


Or when the user presses Enter in the textbox:

private void tbSearchValue_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
        SearchProject();
}


private void findjobsap_KeyPress(object sender, KeyPressEventArgs e)
           {
               if (e.KeyChar == (char)13)
               {
                   string sql = "select [DocEntry],[JobCardNo],[ITEMNAME],[OD] ,[PlanQty],[FinalInspectionRemarks] ,[OrderDate] ,[Division] from SAPProduction.ProductionInventoryReport WHERE JobCardNo =''" + findjobsap.Text.ToString() + "''";
                   SqlDataAdapter da = new SqlDataAdapter(sql, objConn1);
                   DataSet ds = new DataSet();
                   objConn1.Open();
                   da.Fill(ds, "ProductionInventoryReport");
                   dataGridView1.DataSource = ds;
                   //dataGridView1.DataBind();
                   dataGridView1.DataMember = "ProductionInventoryReport";
                   objConn1.Close();
                   e.Handled = true;
               }


      }


这篇关于在Windows应用程序上使用Enter键进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆