Empty/blank 在 C# 空项目 (.Net) 中形成 window
Empty/blank form window in C# Empty Project (.Net)
我需要一些帮助,因为我不知道我的新项目有什么问题!
我在 VS 2019 中启动了一个新的空项目 (.NET),添加了相同的引用来创建表单并在其上绘制不同的控件,如按钮、标签等。 . 一切对我来说都很好,但我没有得到 warning/error。当我 运行 我的项目时,它显示了我预期的表格,但它是空的...表格上没有按钮或标签,只有白色的空白 window [见图]。
我不熟悉空项目,所以可能我错过了一些东西..我试图输入 "InitializeComponent()" 到 class 构造函数但是我收到错误 “错误 CS0103 当前上下文中不存在名称 'InitializeComponent'”.. 我不使用设计器,因为这是一个空项目而且它不是附带它(我不需要它!),所以我想我不需要这个方法?
这是帮助定位问题的代码和图片。可能只是某个地方的菜鸟错误,但我无法弄清楚。
请帮忙!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileManager
{
class EntryPoint
{
static void Main(string[] args)
{
Manager manager = new Manager();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace FileManager
{
public class Manager
{
public Form WindowMain { get; set; }
public ListView ViewArea { get; set; }
public ImageList IconList { get; set; }
public Button Back_btn { get; set; }
public TextBox Path_tbx { get; set; }
public Label FileNameTxt_lbl { get; set; }
public Manager()
{
//InitializeComponent(); << Drop Error CS0103
WindowMain = new Form
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.Sizable,
Size = new Size(960, 540),
Text = "FileManager"
};
ViewArea = new ListView
{
SmallImageList = IconList,
LargeImageList = IconList,
View = View.LargeIcon
};
IconList = new ImageList
{
ImageSize = new Size(48, 48),
ColorDepth = ColorDepth.Depth32Bit
};
Back_btn = new Button
{
Location = new Point(100, 100),
Text = "Back"
};
FileNameTxt_lbl = new Label
{
Text = "File Name:"
};
Application.Run(WindowMain);
}
}
}
我建议您创建一个 Windows 表单应用程序而不是空项目。通过示例学习会更容易。
InitializeComponent 是必需的方法。请检查带有注释的自动生成的文件:
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.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
然后 InitializeComponent 必须在构造函数中调用,如
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
我找到了我的问题的解决方案!在我的窗体上显示控件非常简单...我应该将我的每个控件(按钮等)添加到我的窗体对象 (WindowMain) 的控件集合中,如此简单!
这是我的固定代码:
namespace FileManager
{
public class Manager
{
public Form WindowMain { get; set; }
public ListView ViewArea { get; set; }
public ImageList IconList { get; set; }
public Button Back_btn { get; set; }
public TextBox Path_tbx { get; set; }
public Label FileNameTxt_lbl { get; set; }
public Manager()
{
WindowMain = new Form
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.Sizable,
Size = new Size(960, 540),
Text = "FileManager"
};
ViewArea = new ListView
{
SmallImageList = IconList,
LargeImageList = IconList,
View = View.LargeIcon
};
IconList = new ImageList
{
ImageSize = new Size(48, 48),
ColorDepth = ColorDepth.Depth32Bit
};
Back_btn = new Button
{
Location = new Point(100, 100),
Text = "Back"
};
FileNameTxt_lbl = new Label
{
Text = "File Name:"
};
// These lines need to fix:
WindowMain.Controls.Add(ViewArea);
WindowMain.Controls.Add(Back_btn);
WindowMain.Controls.Add(FileNameTxt_lbl);
Application.Run(WindowMain);
}
}
}
我需要一些帮助,因为我不知道我的新项目有什么问题!
我在 VS 2019 中启动了一个新的空项目 (.NET),添加了相同的引用来创建表单并在其上绘制不同的控件,如按钮、标签等。 . 一切对我来说都很好,但我没有得到 warning/error。当我 运行 我的项目时,它显示了我预期的表格,但它是空的...表格上没有按钮或标签,只有白色的空白 window [见图]。
我不熟悉空项目,所以可能我错过了一些东西..我试图输入 "InitializeComponent()" 到 class 构造函数但是我收到错误 “错误 CS0103 当前上下文中不存在名称 'InitializeComponent'”.. 我不使用设计器,因为这是一个空项目而且它不是附带它(我不需要它!),所以我想我不需要这个方法?
这是帮助定位问题的代码和图片。可能只是某个地方的菜鸟错误,但我无法弄清楚。 请帮忙!
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace FileManager
{
class EntryPoint
{
static void Main(string[] args)
{
Manager manager = new Manager();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
namespace FileManager
{
public class Manager
{
public Form WindowMain { get; set; }
public ListView ViewArea { get; set; }
public ImageList IconList { get; set; }
public Button Back_btn { get; set; }
public TextBox Path_tbx { get; set; }
public Label FileNameTxt_lbl { get; set; }
public Manager()
{
//InitializeComponent(); << Drop Error CS0103
WindowMain = new Form
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.Sizable,
Size = new Size(960, 540),
Text = "FileManager"
};
ViewArea = new ListView
{
SmallImageList = IconList,
LargeImageList = IconList,
View = View.LargeIcon
};
IconList = new ImageList
{
ImageSize = new Size(48, 48),
ColorDepth = ColorDepth.Depth32Bit
};
Back_btn = new Button
{
Location = new Point(100, 100),
Text = "Back"
};
FileNameTxt_lbl = new Label
{
Text = "File Name:"
};
Application.Run(WindowMain);
}
}
}
我建议您创建一个 Windows 表单应用程序而不是空项目。通过示例学习会更容易。
InitializeComponent 是必需的方法。请检查带有注释的自动生成的文件:
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.components = new System.ComponentModel.Container();
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Text = "Form1";
}
#endregion
}
然后 InitializeComponent 必须在构造函数中调用,如
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
我找到了我的问题的解决方案!在我的窗体上显示控件非常简单...我应该将我的每个控件(按钮等)添加到我的窗体对象 (WindowMain) 的控件集合中,如此简单!
这是我的固定代码:
namespace FileManager
{
public class Manager
{
public Form WindowMain { get; set; }
public ListView ViewArea { get; set; }
public ImageList IconList { get; set; }
public Button Back_btn { get; set; }
public TextBox Path_tbx { get; set; }
public Label FileNameTxt_lbl { get; set; }
public Manager()
{
WindowMain = new Form
{
StartPosition = FormStartPosition.CenterScreen,
FormBorderStyle = FormBorderStyle.Sizable,
Size = new Size(960, 540),
Text = "FileManager"
};
ViewArea = new ListView
{
SmallImageList = IconList,
LargeImageList = IconList,
View = View.LargeIcon
};
IconList = new ImageList
{
ImageSize = new Size(48, 48),
ColorDepth = ColorDepth.Depth32Bit
};
Back_btn = new Button
{
Location = new Point(100, 100),
Text = "Back"
};
FileNameTxt_lbl = new Label
{
Text = "File Name:"
};
// These lines need to fix:
WindowMain.Controls.Add(ViewArea);
WindowMain.Controls.Add(Back_btn);
WindowMain.Controls.Add(FileNameTxt_lbl);
Application.Run(WindowMain);
}
}
}