Reputation: 135
how do i pass variables or ID to a specific click event in a dynamic loop of a context menu..my goal is to load the names of the technician in context menu dynamically and each of their click event must pass their id then call my method named assign_tech()
my code so far and what i tried are here
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();
}
}
}
My code for the click event
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();
}
}
My code for the assign tech
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();
}
}
}
}
Upvotes: 1
Views: 448
Reputation: 16574
The ToolStripDropDownItem
inherits a Tag
property from ToolStripItem
specifically for this purpose. Tag
is an object
property, so you can assign anything you like to it. In the API documentation (here) they use it to hold a string, but you can put anything you like in there.
In your loadContext
method in the inner loop set the Tag
property of the newly-created ToolStripDropDownItem
to the ID of your tbl_technician
record:
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]);
}
Now in your handler you can extract the ID value and pass it to assign_tech
method:
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();
}
}
If you want to get a bit deeper you can load your record into an appropriate class instance and store that instance in the Tag
property so that you have more information available to you.
Upvotes: 1