简体   繁体   English

从另一个表单捕获文本框事件

[英]Catch Textbox event from another Form

I have an event in my Form: 我的表格中有一个活动:

 public void filterByType_TextChanged(object sender, EventArgs e)
    {
        dSearch = new D_Search(this);
        dSearch.filterD(); }

which calls a function in another class. 它在另一个类中调用一个函数。 What I want to do is that I want to notice in my class which Textbox was altered and do something. 我想做的是,我想在班上注意到哪些Textbox被更改并做了一些事情。 So there are multiple functions like the one above and they all call the "filterD()" function in my DSearch class. 因此,有很多类似上面的函数,它们都在我的DSearch类中调用了“ filterD()”函数。 I tried 我试过了

if (sender == form1.filterByType_TextChanged)
        { sqlCmd = new SqlCommand("SELECT * FROM" } //SQL Statement
        }

     datTable = new DataTable();
        sqlDatAdapter = new SqlDataAdapter(sqlCmd.CommandText, 
        connection);
        sqlDatAdapter.Fill(datTable);

        form1.setDataGrid = datTable;

but he can't find "sender" I also tried to create a new Button within the function in my Form and pass it but it doesn't seem to work. 但是他找不到“发送者”,我也试图在Form的函数中创建一个新的Button并将其传递,但​​它似乎不起作用。

try this - 尝试这个 -

In form 1 在表格1中

private void textBox1_TextChanged(object sender, EventArgs e)
{
     var dSearch = new D_Search(this);
     MessageBox.Show(dSearch.filterD(sender));
}

D_Search class D_Search类别

public class D_Search
{
    Form1 frm = null;

    public D_Search(Form1 frm1)
    {
        frm = frm1;
    }

    public string filterD(object sender)
    {
        string val = String.Empty;
        if (sender == frm.textBox1)
        {
            val = (sender as TextBox).Text;
        }
        return val;
    }
}

also if you want to access filterByType_TextChanged textbox in other class then change its modifier property to Internal 另外,如果您想访问其他类中的filterByType_TextChanged文本框,则将其修饰符属性更改为Internal

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

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