如何在Winforms C#中的另一个方法中使用ListView和TextBox对象?

wkftcu5l  于 2023-08-07  发布在  C#
关注(0)|答案(1)|浏览(116)

我的项目中有3个ListViews和3个TextBox。如何将它们结合起来使用WinForms C#中的通用方法?
这是我的例子;

// My common method.
    public void checkBarcode(System.Windows.Forms.TextBox txt_ProductName, System.Windows.Forms.ListView lst_Products)
    {
        //do some things

        // At the end, i want to clear textbox and listbox of which product's button call this method
        txt_ProductName.Clear();
        lst_Products.Clear();
    }
    

    // My form members.
    // How can i send textbox and listview objects below to my checkBarcode method?
    private void btn_CheckEggBarcode_Click(object sender, EventArgs e)
    {
        checkBarcode(txt_Egg, lst_Products);
    }

    private void btn_CheckMilkBarcode_Click(object sender, EventArgs e)
    {
        checkBarcode(txt_Milk, lst_Milk);
    }

    private void btn_CheckChocloteBarcode_Click(object sender, EventArgs e)
    {
        checkBarcode(txt_Choclote, lst_Choclote);
    }

字符串

6yjfywim

6yjfywim1#

尝试使用Tag属性

private void Init()
{
    btn_CheckEgg.Tag = new Tuple<TextBox, ListView>(txt_Egg, lst_Products);
    btn_CheckMilk.Tag = new Tuple<TextBox, ListView>(txt_Milk, lst_Milk);
    btn_CheckChoclote.Tag = new Tuple<TextBox, ListView>(txt_Choclote, lst_Choclote);
}

private void checkBarcode(System.Windows.Forms.TextBox txt_ProductName, System.Windows.Forms.ListView lst_Products)
{
    // do some things

    // At the end, I want to clear textbox and listbox of which product's 
    // button call this method
    txt_ProductName.Clear();
    lst_Products.Clear();
 }

private void btn_Click(object sender, EventArgs e)
{
    var btn = (Button)sender;
    var tuple = (Tuple<TextBox, ListView>)btn.Tag;

    checkBarcode(tuple.Item1, tuple.Item2);
}

字符串

相关问题