Solved

i can't display menu on windows form , why ?

Posted on 2011-03-07
5
365 Views
Last Modified: 2012-05-11
i can't display menu on windows form , why ?

please refer the below code ...

private void Welcome_Load(object sender, EventArgs e)
        {
            //Dynamically Create Menu
            MenuStrip objMenuStrip = new MenuStrip();

            ToolStripMenuItem menuItem = new ToolStripMenuItem("File");
            // Create Main menu and child menus in a ditcionary object as key pair values.

            Dictionary<string, string[]> displayMenus = new Dictionary<string, string[]>();

            // Define the child menus based on the each main menu on the basis of key.

            displayMenus.Add("File", new string[] { "New", "Open", "Exit" });

            displayMenus.Add("Edit", new string[] { "Copy", "Cut", "Paste" });

            displayMenus.Add("Help", new string[] { "AboutMe", "ContactUs", "Help" });

            this.Controls.Add(objMenuStrip);
}

Open in new window


what can i do now ??
0
Comment
Question by:Parth48
  • 3
5 Comments
 
LVL 22

Expert Comment

by:p_davis
ID: 35056256
give it a location or set the dock property
0
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 500 total points
ID: 35057134
Don't you have to set the mainmenustrip property of the form?

this.MainMenuStrip == objMenuStrip;
0
 

Author Comment

by:Parth48
ID: 35065931
i have set the mainmenustrip property of the form , but still can't display menu on form ...

please refer the below code ....

this.MainMenuStrip = objMenuStrip;

what can i do now ??
0
 

Author Comment

by:Parth48
ID: 35066132
please refer the below code .....

 private void Welcome_Load(object sender, EventArgs e)
        {
            Form objform = this.FindForm();

            //Dynamically Create Menu
            MenuStrip menuItems = new MenuStrip();
           
            //Get Form Properties
            
            objGeneral.GetFormProperties(objform);

            objform.LocationChanged += new EventHandler(this.Welcome_LocationChanged);
            objform.Resize += new EventHandler(this.Welcome_Resize  );

            //attach flash object to the windows form
            //GetFlashObject();

            //create lable Dynamically
            //Label lblTitle = new Label();
            //lblTitle.Text = "WELCOME";
            //lblTitle.BackColor = Color.FromKnownColor(KnownColor.Transparent);
            //lblTitle.Font = new Font("Microsoft Sans Serif", 14, FontStyle.Bold, GraphicsUnit.Point);
            //lblTitle.Location = new Point(200, 130);
            //lblTitle.Size = new Size(150, 25);
            //this.Controls.Add(lblTitle);
            
            //create Icon Dynamically
            PictureBox ApplicationIcon = new PictureBox();
            int length = Application.ExecutablePath.Length;
            ApplicationIcon.ImageLocation = Application.ExecutablePath.Substring(0, length - 22).ToString() + "Icons\\" + "Application_Icon.ico";
            ApplicationIcon.Size = new Size(128, 128);
            ApplicationIcon.Location = new Point(180, 150);
            this.Controls.Add(ApplicationIcon);

            //Create Button Dynamically
            Button btnLogin = new Button();
            btnLogin.Text = "Click To Login";
            btnLogin.TextAlign = ContentAlignment.MiddleCenter;
            btnLogin.Location = new Point(300, 350);
            btnLogin.Font = new Font("Microsoft Sans Serif", 10, FontStyle.Bold, GraphicsUnit.Point);
            btnLogin.Size = new Size(160, 25);
            btnLogin.Click += new EventHandler(this.btnLogin_Click);
            this.Controls.Add(btnLogin);
        }

        private List<ToolStripMenuItem> CreateMenu(Dictionary<string, string[]> displayMenus)
        {

            // Declare ToolStripMenuItem object.

            List<ToolStripMenuItem> menuItems = new List<ToolStripMenuItem>();

            // Loop through all main menus.

            foreach (KeyValuePair<string, string[]> menu in displayMenus)
            {

                // Take a ToolStripMenuItem to add the menu item.

                ToolStripMenuItem menuItem = new ToolStripMenuItem(menu.Key);

                // Set a name to the menu.

                menuItem.Name = menu.Key;

                // Create child menu items for a menu item.

                this.CreateChildMenus(menuItem, menu.Value);

                switch (menu.Key)
                {

                    case "File":

                    case "Edit":

                        // This is by default.

                        // menuItem.Alignment = ToolStripItemAlignment.Left;

                        break;

                    case "Help":

                        menuItem.Alignment = ToolStripItemAlignment.Right;

                        break;

                }

                // Add each menu item to the menu strip item.

                menuItems.Add(menuItem);

            }

            return menuItems;

        }

        private void CreateChildMenus(ToolStripMenuItem parentMenuToAddChildMenus, string[] childMenus)
        {

            // Loop through all child menus.

            foreach (string childMenu in childMenus)
            {

                // Take a ToolStripMenuItem to add the menu item.

                ToolStripMenuItem childMenuItem = new ToolStripMenuItem(childMenu);

                // Set a name to the menu.

                childMenuItem.Name = childMenu;

                // Hnadle the event for the menu created.

                childMenuItem.Click += new EventHandler(ChildMenu_Click);

                // Add each child menu to its parent menu item.

                parentMenuToAddChildMenus.DropDown.Items.Add(childMenuItem);

            }

        }

            private void ChildMenu_Click(object sender, EventArgs e)

            {

                ToolStripMenuItem sourceMenuItem = (ToolStripMenuItem)sender;

                string selectedMenu = string.Empty;

                // Selected menu item

                switch (sourceMenuItem.Name)

                {

                case "New":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "Open":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "Exit":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "Edit":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "Copy":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "Paste":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "AboutMe":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "ContactUs":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                case "Help":

                // Required statements here.

                selectedMenu = sourceMenuItem.Text;

                break;

                }

                if (!string.IsNullOrEmpty(selectedMenu))

                {

                MessageBox.Show(string.Concat(

                selectedMenu,

                " feature is under development "));

                }

            }

Open in new window


anything wrong in the above code ??
0
 

Author Comment

by:Parth48
ID: 35066134
i think i missing something in Page_load Event ???

but i can't find it ??
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
C# Reverse int in fast ways 6 28
Form design in vb.net 7 19
C# Error - Add Failed 12 49
Red error squiggly on vb.net 7 12
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now