C# 在动态单击事件(上下文菜单)上传递变量/ID
如何在上下文菜单的动态循环中将变量或ID传递给特定的单击事件。我的目标是在上下文菜单中动态加载技术人员的姓名,他们的每个单击事件必须传递其ID,然后调用名为assign_tech()的方法C# 在动态单击事件(上下文菜单)上传递变量/ID,c#,contextmenu,C#,Contextmenu,如何在上下文菜单的动态循环中将变量或ID传递给特定的单击事件。我的目标是在上下文菜单中动态加载技术人员的姓名,他们的每个单击事件必须传递其ID,然后调用名为assign_tech()的方法 到目前为止我的代码和我尝试的都在这里 void loadContext() { query = "SELECT * FROM `tbl_technician`"; using (MySqlConnection conn = constrings.GetDBCon
到目前为止我的代码和我尝试的都在这里
void loadContext()
{
query = "SELECT * FROM `tbl_technician`";
using (MySqlConnection conn = constrings.GetDBConnection())
{
try
{
conn.Open();
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
using (MySqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
ToolStripMenuItem cm = startToolStripMenuItem as ToolStripMenuItem;
cm.DropDownItems.Add(reader["technician"].ToString()).Click += MyMethod;
//cm.DropDownItems.Add(reader["technician"].ToString()).Click += (sender, e) => { MyMethod(sender, e); };
//cm.DropDownItems.Add(reader["technician"].ToString()).Click += (_sender, _e) =>
//{
// tech_id = Convert.ToInt32(reader[0]);
// //MessageBox.Show(tech_id.ToString());
//};
}
}
}
}
catch (Exception ex)
{
CMessageBox m = new CMessageBox("error\n" + ex);
m.ShowDialog();
}
finally
{
conn.Close();
}
}
}
单击事件的我的代码
void MyMethod(object sender, EventArgs e)
{
// MessageBox.Show("test");
DialogResult dr = new DialogResult();
DialogueBox db = new DialogueBox("Are you sure you want to assign this Record!");
dr = db.ShowDialog();
if (dr == DialogResult.Yes)
{
assign_tech();
loadData();
loadTechnicianData();
}
}
分配技术的我的代码
void assign_tech()
{
if (tech_id>0)
{
using (MySqlConnection conn = constrings.GetDBConnection())
{
try
{
conn.Open();
query = "UPDATE `tbl_job_order` SET `t_id` = @tech_id, status= 'Servicing' WHERE `tbl_job_order`.`j_id` = @id;";
using (MySqlCommand cmd = new MySqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@tech_id", tech_id);
cmd.Parameters.AddWithValue("@id", selected_id);
cmd.ExecuteNonQuery();
}
}
catch (Exception ex)
{
CMessageBox m = new CMessageBox("error \n" + ex);
m.ShowDialog();
}
finally
{
conn.Close();
}
}
}
}
ToolStripDropDownItem
专门为此从ToolStripItem
继承了一个Tag
属性Tag
是一个对象
属性,因此您可以为其指定任何您喜欢的内容。在API文档()中,他们使用它来保存字符串,但是您可以在其中放入任何您喜欢的内容
在内部循环的loadContext
方法中,将新创建的ToolStripDropDownItem
的Tag
属性设置为tbl\U技师的ID
记录:
while (reader.Read())
{
ToolStripMenuItem cm = startToolStripMenuItem as ToolStripMenuItem;
var item = cm.DropDownItems.Add(reader["technician"].ToString());
item.Click += MyMethod;
item.Tag = Convert.ToInt32(reader[0]);
}
现在,在处理程序中,您可以提取ID值并将其传递给assign\u tech
方法:
void MyMethod(object sender, EventArgs e)
{
// get tech id from sender's Tag
if (!(sender is TooStripDropDownItem item) || !(item.Tag is int tech_id))
return;
// MessageBox.Show("test");
DialogResult dr = new DialogResult();
DialogueBox db = new DialogueBox("Are you sure you want to assign this Record!");
dr = db.ShowDialog();
if (dr == DialogResult.Yes)
{
assign_tech(tech_id);
loadData();
loadTechnicianData();
}
}
如果您想更深入一点,可以将记录加载到适当的类实例中,并将该实例存储在
标记
属性中,以便获得更多可用信息。如何正确存储记录?我不介意学习新东西。现在我正在一个名为profile.cs的类上存储数据,但它们都是静态字符串和整数,不知道这是不是一种正确的方法。看看像Linq2DB或EntityFrameworks这样的ORM。它们让您可以像处理另一个对象集合一样处理表,并处理对数据库服务器进行实际查询时的大部分枯燥工作。Linq2DB是我个人的最爱。