我创建了一个用户控件和一个类文件,用户控件包含保存按钮。当用户单击“保存”按钮然后我想使用来自infocore类文件的ItemClickedEventHandler调用Insert()时请帮帮我。
C#:用户控制
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
public void Insert(System.Windows.Forms.Form f, e)
{
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();
}
}
答案 0 :(得分:1)
创建ItemClicked
类的实例时,您需要订阅活动toolbar
。
我没有看到你的整个代码,这是一个示例:
C#:infocore.cs - 假设您在此处创建工具栏实例
//Creating toolbar object
toolbar objToolBar = new toolbar();
objToolBar.ItemClicked += new toolbar.ItemClickedEventHandler(objToolBar_ItemClicked);
//Handler to ItemClicked Event
void objToolBar_ItemClicked(object sender, MainWindow.toolbar.ItemClickEventArgs e)
{
//Call Insert Method From here
}