我一直在用Java编写大量代码,发现创建控制台或表单应用程序非常容易。事实上,所有人要做的就是添加一个表单并显示该表单。简单就像一个cookie,可以这么说。但是现在我有一个很大的项目出现在Visual C#中,我还没有那么多使用它。我想,我不确定我是否正确,但控制台应用程序是C#就是这样一个控制台应用程序,它不能接受任何鼠标动作事件。我希望能够从表单内部向C#添加控件,就像在Java中一样。添加按钮或添加菜单。但是在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;
}
}
答案 0 :(得分:2)
听起来像是一般的设计/方法问题。 为什么不在WinForms上使用WPF?为您提供Visual Studio中非常方便的设计师。您可以以WYSIWYG风格轻松构建GUI。支持数据绑定,一旦熟悉,非常方便。 但是,稍后您仍然可以在运行时添加控件等,请参阅Breems的答案。 注意:如果您使用WPF,如果要从除Windows之外的其他线程添加控件,则需要使用Windows的调度程序。
答案 1 :(得分:1)
WinForms控件是常规对象,就像AWT或Swing一样。
您可以创建new ToolStripItem()
,设置其属性,然后将其添加到DropDownItems
集合。
答案 2 :(得分:1)
您是否尝试在运行时添加控件?如果是这样,正如SLaks所说,您只需创建一个新的控件实例并将其添加到表单中。
// Add a menustrip to the form.
MenuStrip menuStrip = new MenuStrip();
menuStrip.Dock = DockStyle.Top;
this.Controls.Add(menuStrip);
否则,为什么不利用可视化设计器向表单添加控件?