以编程方式添加菜单
本文关键字:菜单 添加 方式 编程 | 更新日期: 2024-09-25 12:06:14
我用Java写了很多代码,发现创建控制台或表单应用程序非常容易。事实上,你所要做的就是添加一个表单并显示该表单。可以说,这就像饼干一样简单。但现在我在Visual C#中有一个大项目要做,我并没有那么多地使用它。我想,我不确定我是否是对的,但控制台应用程序是C#,就是一个不能接受任何鼠标操作事件的控制台应用程序。我希望能够像在Java中一样,从表单内部向C#添加控件。添加按钮或添加菜单。但在C#中,有几个文件是打开的、属性、assemblyinfo.cs、form1.cs等。下面的代码是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;
}
}
对我来说,这听起来像是一个一般的设计/方法问题。为什么不在WinForms上使用WPF?在Visual Studio中为您提供了一个非常方便的设计器。您可以轻松地以所见即所得的样式构建GUI。支持数据绑定,这一点一旦熟悉就非常方便。不过,稍后您仍然可以在运行时添加控件等,请参阅Breems的回答。注意:如果您使用WPF,如果您想从另一个线程而不是Windows的线程添加控件,则需要使用Windows的调度程序。
您是否试图在运行时添加控件?如果是这样,正如SLaks所说,您只需要创建一个新的控件实例并将其添加到表单中
// Add a menustrip to the form.
MenuStrip menuStrip = new MenuStrip();
menuStrip.Dock = DockStyle.Top;
this.Controls.Add(menuStrip);
否则,为什么不使用可视化设计器将控件添加到表单中呢?
WinForms控件是常规对象,就像AWT或Swing一样。
您可以创建new ToolStripItem()
,设置其属性,然后将其添加到DropDownItems
集合中。