在儿童点击时从数据库调用事件

时间:2017-10-01 12:19:16

标签: c#

我已经在数据库中存储了表单名称和事件名称,在表单加载菜单项和从数据库中获取的子菜单,在子单击时我从数据库中获取了事件名称并存储在变量中,现在我想要调用那个中的事件变量

检查一下:

// on form load
private void MainForm_Load(object sender, EventArgs e)
{
        MnuStrip = new MenuStrip();
        base.Controls.Add(this.MnuStrip);
        MnuStrip.BackColor = Color.FromArgb(4, 71, 3);
        MnuStrip.ForeColor = Color.Orange;
        MnuStrip.Font = new Font("Arial", 12);

        string selectCommandText = "select Main_menu,Manu_Par_value,Status from MenuParent where Status='Y'";
        SqlDataAdapter adapter = new SqlDataAdapter(selectCommandText, con);
        DataTable dataTable = new DataTable();

        con.Open();

        adapter.Fill(dataTable);

        foreach (DataRow row in dataTable.Rows)
        {
            this.MnuStripItem = new ToolStripMenuItem(row["Main_menu"].ToString());
            this.SubMenu(this.MnuStripItem, row["Manu_Par_value"].ToString());
            MnuStrip.Items.Add(this.MnuStripItem);
        }

        base.MainMenuStrip = MnuStrip;
        con.Close();
}

//submenu loads
public void SubMenu(ToolStripMenuItem mnu, string submenu)
{
        //SqlDataAdapter adapter = new SqlDataAdapter("SELECT FRM_NAME FROM vw_pagerole WHERE MENUPARVAL='" + submenu + "' and status = 1 and UID = '" + Username + "' ", con);
        SqlDataAdapter adapter = new SqlDataAdapter("select  mp.Main_menu, msm.Form_code, msm.form_Name, r.u_name from MenuParent mp ,MenuSubmenue msm, Rights r where mp.Manu_Par_value = msm.Manu_Par_value and mp.Manu_Par_value = r.Manu_Par_value and msm.menue_submenue_code  = r.menue_submenue_code and r.Rights_invisible = 1 and mp.Main_menu='" + mnu + "'  and r.u_name = '" + Username + "' ", con);
        DataTable dataTable = new DataTable();
        adapter.Fill(dataTable);

        foreach (DataRow row in dataTable.Rows)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(row["form_Name"].ToString(), null, new EventHandler(ChildClick));
            mnu.DropDownItems.Add(item);
        }
}

//on child click
string eventclick;  //event name stored in this string
private void ChildClick(object sender, EventArgs e)
{
       string query = ("select Form_code from MenuSubmenue where form_Name='" + sender.ToString() + "'");
       SqlCommand cmdd = new SqlCommand(query, con);    con.Open();

       var dbr = cmdd.ExecuteReader();

       while (dbr.Read())
       {
           eventclick = (string)dbr["Form_code"];
           label1.Text = eventclick;
       }
       con.Close();    
}

我想调用字符串类型变量名eventclick

中的此事件

1 个答案:

答案 0 :(得分:0)

您可以使用reflection按名称调用方法。 问题既没有指定定义要调用的方法的类,也没有指定此方法的签名(参数)。所以我假设该方法被定义为当前Form的实例方法,并且具有签名(object sender, EventArgs e),这对于事件处理程序是常见的。下面的代码然后执行名称在eventclick变量:

中定义的方法
//Get instance method of this Form by method's name
var method = this.GetType().GetMethod(
    eventclick,
    BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

// Prepare arguments for method to be called.
// Here I assume that method is standard event
// handler and has two arguments of type object
// and EventArgs
EventArgs eventArgs = null;//create new EventArgs() if required by called method
var arguments = new object[] { this, eventArgs };

//Invoke method with prepared arguments
method.Invoke(this, arguments);