Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/291.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 以编程方式添加菜单_C#_Winforms - Fatal编程技术网

C# 以编程方式添加菜单

C# 以编程方式添加菜单,c#,winforms,C#,Winforms,我一直在用Java编写大量代码,发现创建控制台或表单应用程序都非常容易。事实上,只需添加一个表单并显示该表单。可以这么说,就像饼干一样简单。但现在我有一个大项目要在Visual C中完成,我还没有太多地使用它。我想,我不确定我是否正确,但控制台应用程序是C#就是这样,控制台应用程序不能接受任何鼠标操作事件。我希望能够从表单内部向C#添加控件,就像在Java中一样。添加按钮或添加菜单。但在C#中有几个打开的文件,如properties、assemblyinfo.cs、form1.cs等。下面的代码

我一直在用Java编写大量代码,发现创建控制台或表单应用程序都非常容易。事实上,只需添加一个表单并显示该表单。可以这么说,就像饼干一样简单。但现在我有一个大项目要在Visual C中完成,我还没有太多地使用它。我想,我不确定我是否正确,但控制台应用程序是C#就是这样,控制台应用程序不能接受任何鼠标操作事件。我希望能够从表单内部向C#添加控件,就像在Java中一样。添加按钮或添加菜单。但在C#中有几个打开的文件,如properties、assemblyinfo.cs、form1.cs等。下面的代码在form designer.cs中

那么,从编程的角度而不是从设计可视化的角度来添加组件的最佳方法是什么呢

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;
    }
}
命名空间WindowsFormsTestMenuApplication
{
部分类Form1
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
/// 
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域Windows窗体设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.menuStrip1=new System.Windows.Forms.MenuStrip();
this.fileToolStripMenuItem=new System.Windows.Forms.ToolStripMenuItem();
this.openToolStripMenuItem=新系统.Windows.Forms.ToolStripMenuItem();
this.options ToolStripMenuItem=新建System.Windows.Forms.ToolStripMenuItem();
this.aToolStripMenuItem=new System.Windows.Forms.ToolStripMenuItem();
this.bToolStripMenuItem=new System.Windows.Forms.ToolStripMenuItem();
this.cToolStripMenuItem=新建System.Windows.Forms.ToolStripMenuItem();
this.menuStrip1.SuspendLayout();
这个.SuspendLayout();
// 
//菜单第1条
// 
this.menuStrip1.Items.AddRange(新System.Windows.Forms.ToolStripItem[]{
此.fileToolStripMenuItem,
此.openToolStripMenuItem,
此选项为.optionToolStripMenuItem});
this.menuStrip1.Location=新系统.Drawing.Point(0,0);
this.menuStrip1.Name=“menuStrip1”;
this.menuStrip1.Size=新系统.图纸.尺寸(284,24);
this.menuStrip1.TabIndex=0;
this.menuStrip1.Text=“menuStrip1”;
// 
//fileToolStripMenuItem
// 
this.fileToolStripMenuItem.DropDownItems.AddRange(新的System.Windows.Forms.ToolStripItem[]{
这个.aToolStripMenuItem,
这个。B工具条菜单,
这个.cToolStripMenuItem});
this.fileToolStripMenuItem.Name=“fileToolStripMenuItem”;
this.fileToolStripMenuItem.Size=新系统.Drawing.Size(37,20);
this.fileToolStripMenuItem.Text=“File”;
// 
//openToolStripMenuItem
// 
this.openToolStripMenuItem.Name=“openToolStripMenuItem”;
this.openToolStripMenuItem.Size=新系统.Drawing.Size(48,20);
this.openToolStripMenuItem.Text=“打开”;
// 
//选项ToolStripMenuItem
// 
this.optionsToolStripMenuItem.Name=“optionsToolStripMenuItem”;
this.options ToolStripMenuItem.Size=新系统图尺寸(61,20);
this.Options ToolStripMenuItem.Text=“Options”;
// 
//aToolStripMenuItem
// 
this.aToolStripMenuItem.Name=“aToolStripMenuItem”;
this.aToolStripMenuItem.Size=新系统图尺寸(152,22);
this.aToolStripMenuItem.Text=“A”;
// 
//bToolStripMenuItem
// 
this.bToolStripMenuItem.Name=“bToolStripMenuItem”;
this.bToolStripMenuItem.Size=新系统.Drawing.Size(152,22);
this.bToolStripMenuItem.Text=“B”;
// 
//cToolStripMenuItem
// 
this.cToolStripMenuItem.Name=“cToolStripMenuItem”;
this.cToolStripMenuItem.Size=新系统图纸尺寸(152,22);
this.cToolStripMenuItem.Text=“C”;
// 
//表格1
// 
此.AutoScaleDimensions=新系统.Drawing.SizeF(6F,13F);
this.AutoScaleMode=System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize=新系统.Drawing.Size(284262);
this.Controls.Add(this.menuStrip1);
this.main menustrip=this.menuStrip1;
this.Name=“Form1”;
this.Text=“Form1”;
this.menuStrip1.resumeloayout(false);
此.menuStrip1.PerformLayout();
此选项为.resume布局(false);
这个。执行布局();
}
#端区
private System.Windows.Forms.MenuStrip menuStrip1;
private System.Windows.Forms.ToolStripMenuItem文件ToolStripMenuItem;
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选项ToolStripMenuItem;
}
}

WinForms控件是常规对象,就像AWT或Swing一样

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