如何使用c#从用户控件调用类文件的click事件 [英] how to call click event of class file from user control using c#

查看:151
本文介绍了如何使用c#从用户控件调用类文件的click事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个用户控件和一个类文件,用户控件包含保存按钮。当用户点击保存按钮然后我想使用来自infocore类文件的ItemClickedEventHandler调用Insert()请帮帮我。



C#:用户控制

I had created one user control and one class file, The user control contain save button. When user click save button then i want call the Insert() using ItemClickedEventHandler from infocore class file please help me.

C#: User Control

public partial class toolbar : UserControl
{
    public delegate void ItemClickedEventHandler(System.Object sender, ItemClickEventArgs e);
    public toolbar()
    {
        InitializeComponent();
        inf = new IAToolBar.infocore();
    }
    public event ItemClickedEventHandler ItemClicked;


    public class ItemClickEventArgs
    {
        public string flag;
        public ItemClickEventArgs(string flg)
        {
            flag = flg;
        }
    }

    private void btnsave_Click(object sender, EventArgs e)
    {
        if (ItemClicked != null)
        {
            ItemClicked(sender, new ItemClickEventArgs("Save"));
        }
    }
}



C#:infocore.cs


C#: infocore.cs

public void Insert(System.Windows.Forms.Form f)
{
connection.Open();
try
{
query = "INSERT INTO " + f.Tag.ToString().Trim() + " (" + queryfields + ") VALUES (" + formvalues + ") ";
MySqlCommand cmd = new MySqlCommand(query, connection);
cmd.ExecuteNonQuery();
MessageBox.Show("Save Sucessfully", "Inventory Accountancy", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), "Inventory Accountancy", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
connection.Close();
}
}

推荐答案

参见本教程 [ ^ ]有关如何订阅事件处理程序的详细信息。
See this tutorial[^] for details of how to subscribe to the event handler.


这篇关于如何使用c#从用户控件调用类文件的click事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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