0

I have been writing a lot of code in Java and find that it is really easy to create either a console or form application. In fact, all one has to do is add a form and display that form. Simple as a cookie, so to speak. But now I have a big project coming up in Visual C# and I have not used it all that much. I think, I am not sure if I am right, but a console application is C# is just that, a console application which cannot accept any mouse action events. I want to be able to add controls to C# from inside of the form, just like in Java. Add a button, or add a menu. But in C# there are several files that open, properties, assemblyinfo.cs, form1.cs, etc. The code below is in form designer.cs.

So where is the best way to add components from the programming point of view and not the design visual stand point of view?

namespace WindowsFormsTestMenuApplication
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.menuStrip1 = new System.Windows.Forms.MenuStrip();
            this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.openToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.aToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.bToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.cToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
            this.menuStrip1.SuspendLayout();
            this.SuspendLayout();
            // 
            // menuStrip1
            // 
            this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.fileToolStripMenuItem,
            this.openToolStripMenuItem,
            this.optionsToolStripMenuItem});
            this.menuStrip1.Location = new System.Drawing.Point(0, 0);
            this.menuStrip1.Name = "menuStrip1";
            this.menuStrip1.Size = new System.Drawing.Size(284, 24);
            this.menuStrip1.TabIndex = 0;
            this.menuStrip1.Text = "menuStrip1";
            // 
            // fileToolStripMenuItem
            // 
            this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.aToolStripMenuItem,
            this.bToolStripMenuItem,
            this.cToolStripMenuItem});            
            this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
            this.fileToolStripMenuItem.Size = new System.Drawing.Size(37, 20);
            this.fileToolStripMenuItem.Text = "File";
            // 
            // openToolStripMenuItem
            // 
            this.openToolStripMenuItem.Name = "openToolStripMenuItem";
            this.openToolStripMenuItem.Size = new System.Drawing.Size(48, 20);
            this.openToolStripMenuItem.Text = "Open";
            // 
            // optionsToolStripMenuItem
            // 
            this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem";
            this.optionsToolStripMenuItem.Size = new System.Drawing.Size(61, 20);
            this.optionsToolStripMenuItem.Text = "Options";
            // 
            // aToolStripMenuItem
            // 
            this.aToolStripMenuItem.Name = "aToolStripMenuItem";
            this.aToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.aToolStripMenuItem.Text = "A";
            // 
            // bToolStripMenuItem
            // 
            this.bToolStripMenuItem.Name = "bToolStripMenuItem";
            this.bToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.bToolStripMenuItem.Text = "B";
            // 
            // cToolStripMenuItem
            // 
            this.cToolStripMenuItem.Name = "cToolStripMenuItem";
            this.cToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
            this.cToolStripMenuItem.Text = "C";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(284, 262);
            this.Controls.Add(this.menuStrip1);
            this.MainMenuStrip = this.menuStrip1;
            this.Name = "Form1";
            this.Text = "Form1";
            this.menuStrip1.ResumeLayout(false);
            this.menuStrip1.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.MenuStrip menuStrip1;
        private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem aToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem bToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem cToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem openToolStripMenuItem;
        private System.Windows.Forms.ToolStripMenuItem optionsToolStripMenuItem;
    }
}
3
  • Start by reading the files and understanding the generated code. Commented Dec 13, 2013 at 15:03
  • How woudl I go about adding another menu item in the above code. I tried copying aToolStringMenuItem and pasting it at the end, and then added in the new part here also this.cToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); but I still recieve and error. I know that it is easier to add in the menu item from the designer but I am just trying to figure out some things in C# by programming it programatically. Commented Dec 13, 2013 at 16:27
  • What error do you get? Commented Dec 13, 2013 at 16:29

3 Answers 3

2

Sounds somehow like a general design/approach question to me. Why don't you use WPF over WinForms? Gives you a quite handy designer in Visual Studio. You can comfortably build your GUI in WYSIWYG-style. Supports data binding, which - once familiar with - is pretty convenient. However, later you can still add controls etc. at runtime, see Breems answer. Note: If you use WPF, you need to use the Windows's dispatcher if you want to add controls from another thread than the Windows's one.

Sign up to request clarification or add additional context in comments.

Comments

1

Are you trying to add controls at runtime? If so, as SLaks stated, you will simply need to create a new instance of the control and add it to your form.

 // Add a menustrip to the form.
 MenuStrip menuStrip = new MenuStrip();
 menuStrip.Dock = DockStyle.Top;
 this.Controls.Add(menuStrip);

Otherwise, why not utilize the visual designer to add controls to your form?

1 Comment

I was trying to add a button to the form when I click on a menu item. I was just trying to do it programmatically because it is a better learning way than using the design editor all of the time. I am trying to brush up on my programming skills in the C# or C++ world and it makes it more difficult to do it programmatically like build a console application. I agree that the designer is much more useful for fast development.
1

WinForms controls are regular objects, just like AWT or Swing.

You can create a new ToolStripItem(), set its properties, then add it to a DropDownItems collection.

2 Comments

Hate to ask but what is the DropDownItems collection. I this something that C# useds to add items to the form. I am at work right now so looking at visual studio in depth is a bit difficult. Can you show a code example. Can you create forms and add menu items in a console application?
@DougHauf: Yes. The only difference between a console application and a WinForms application is that a WinForms application doesn't have a console. You pasted complete sample code in your question; read it!

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.