0

Good day guys, i have a problem on creating submenu's click menu, normally i can cast it on each of their properties but my submenus are from database and i dont know how to add the click event hardcoded

my code so far

void loadTechnicianData()
        {
            TechnicianPanel.Controls.Clear();
            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())
                            {
                                Tech_UC uc = new Tech_UC();
                                uc.TechHeader.Text = reader["technician"].ToString();
                                uc.TechHeader.BackColor = Color.FromName(reader["color_assigned"].ToString());
                                uc.id = Convert.ToInt32(reader["t_id"].ToString());
                                TechnicianPanel.Controls.Add(uc);

                                ToolStripMenuItem cm = startToolStripMenuItem as ToolStripMenuItem;
                                cm.DropDownItems.Add(reader["technician"].ToString());

                            //here i need to call a click event to call a method void outside the loop
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    CMessageBox m = new CMessageBox("error\n" + ex);
                    m.ShowDialog();
                }
                finally
                {
                    conn.Close();
                }
            }
        }

1 Answer 1

1

Try this:

cm.DropDownItems.Add(reader["technician"].ToString()).Click += MyMethod;

With:

void MyMethod(object sender, EventArgs e);

You can use a delegate or a lambda expression instead of a method if short and not reusable:

cm.DropDownItems.Add(reader["technician"].ToString()).Click += (_sender, _e) =>
{
  ...
};
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.