18 Feb 2014

Append a MenuStrip to an MDI Parent Window (Windows Forms) c# (or) mdi child menu in wpf

In this post, I explained how to bind data to menu strip in MDI PARENT FORM. multiple-document interface (MDI) child window can be different from the MDI parent window. For example, the MDI parent might be a spreadsheet, and the MDI child might be a chart, I have used stored procedures in this article.  I have binded the menu strip based on the role of the user.




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Telerik.WinControls;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace Company
{
    public partial class MainForm : Telerik.WinControls.UI.RadForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        public DataSet getMenu()
        {
            return SqlHelper.ExecuteDataset(Program.Con, CommandType.StoredProcedure, "Storedprocedute_Menu");
        }

        public DataSet getWorkMenu(string role, int menu)
        {
            SqlParameter[] cmdpar = new SqlParameter[2];
            cmdparam[0] = new SqlParameter("@p_menu", SqlDbType.Int);
            cmdparam[0].Value = menuid;
            cmdparam[1] = new SqlParameter("@p_role", SqlDbType.Int);
            cmdparam[1].Value = roleId;
            return SqlHelper.ExecuteDataset(Program.Con, CommandType.StoredProcedure, "StoredProcedure", cmdpar);
        }

        private void SelectedChildMenu_OnClick(object sender, System.EventArgs e)
        {
            if (((ActiveMdiChild != null))) ActiveMdiChild.Close();
            cmbNames.SelectedValue = sender.ToString();
            string formName = cmbNames.Text;
            string[] frms = formName.Split('&');

            object oForm = new object();
            Program.frmName = frms[0];
            Program.mode = frms[1];
            string projName = "Company";
            Type tForm = System.Reflection.Assembly.GetExecutingAssembly().GetType(projName + "." + frms[0]);

            if (((tForm != null)))
            {
                oForm = Activator.CreateInstance(tForm);
                ((Form)oForm).MdiParent = this;
                ((Form)oForm).Show();
            }
        }

        public void MainForm_Load(object sender, EventArgs e)
        {
            ToolStripMenuItem childItem = default(ToolStripMenuItem);
            int cnt = 0;
            DataSet menuData = null;
            menuData = getMenu();
            cmbFuncNames.DataSource = menuData.Tables[1];
            cmbFuncNames.DisplayMember = "progam";
            cmbFuncNames.ValueMember = "work";
            for (int j = 0; j <= menuData.Tables[0].Rows.Count - 1; j++)
            {
                int id = Convert.ToInt32(menuData.Tables[0].Rows[j]["MID"]);
                ToolStripMenuItem parItem = new ToolStripMenuItem();
                parItem.Name = id.ToString();
                string menuName = menuData.Tables[0].Rows[j]["MNAME"].ToString();
                parItem.Text = menuName;                 MenuStrip2.Items.Add(pItem);
                DataSet childDs = new DataSet();
                childDs = getWorkMenu(Program.userrole, id);
                ContextMenuStrip cms = new ContextMenuStrip();
                for (int i = 0; i <= childDs.Tables[0].Rows.Count - 1; i++)
                {
                    childItem = new ToolStripMenuItem();
                    childItem.ForeColor = System.Drawing.Color.Black;
                    childItem.Name = childDs.Tables[0].Rows[i]["WorkID"].ToString();
                    childItem.Text = childDs.Tables[0].Rows[i]["WorkNAME"].ToString();
                    childItem.Tag = childDs.Tables[0].Rows[i]["PROG_NAME"].ToString();
                    pItem.DropDownItems.Add(childItem);

                    cms.Items.Add(childItem.Text, null, new System.EventHandler(SelectedChildMenu_OnClick));
                }
                ToolStripMenuItem tsi = (ToolStripMenuItem)parItem;
                tsi.DropDown = cms;
            }
        }

        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }

        private void MenuStrip2_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "close")
            {
                Application.Exit();
            }
        }
    }
}


No comments:

Post a Comment