当前位置:网站首页>C # application interface development foundation - form control
C # application interface development foundation - form control
2022-06-28 19:41:00 【DXB2021】
From forms
establish WinForm Program
establish WinForm Procedure steps
open Visual Studio, Click on “ Create a new project ”, Using shortcut keys Ctrl+Shift+N It can be opened quickly “ New projects ” Dialog box .

Select... From the pull down menu “C#”、“Windows”、“ desktop ” in , Select in the list box “Windows Form application (.NET Framework)”.

Click on “ next step ”.
Click on “ establish ”.

In Solution Explorer , double-click Program.cs file , Will jump to Windows Console application interface , stay “ edit ” In the window is an automatically generated WinForm Program .

The code is as follows :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
internal static class Program
{
/// <summary>
/// The main entry point for the application .
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}Click again “ start-up ” Key , A blank form will pop up .

The essence of creating a form
The essence of a form is to use System.Windows.Forms.Form Class or a derived class of this class .
Right click “ forms ” after , choice “ Look at the code ” command , It will jump to Form1.cs file .


The code is as follows :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
}
}
Add and delete forms
Right-click the project name WindowsFormsApp1, Choose... From the shortcut menu that pops up “ add to ”-“Windows forms ” perhaps “ add to ”-“ New item ” command .

Last in “ Add a new item ” Select... From the Christmas box “ forms (Windows forms )”.

Delete form , Just in the solution manager , Select the name of the form to delete , Right click , Select from the pop-up shortcut menu “ Delete ” Command is enough .

Properties of the form
1、“ attribute ” panel
open “ attribute ” The panel has the following three methods :
(1)
Right click command

(2)
“ View ” command

(3)
The lower right corner

2、C# WinForm Form basic properties
(1) Attribute values in window styles , Can be used to change the icon 、 Maximize window transparency .
| Property value | explain |
| Icon | Change icon style ( Top left Icon ) |
| MaximizeBox:true; | Display the maximize button in the upper right corner |
| MinimizeBox:true; | Display the minimize button in the upper right corner |
| ShowInco:true; | Display the small icon in the upper left corner |
| ShowInTaskbar:ture; | The form is displayed in the taskbar |
| TopMost:ture; | The window is displayed at the top |
| Opacity:100% | Whole window transparency |
(2) Attribute values in the layout , It can be used to change the size of the form and the display position of the form after starting the program .
| Property value | explain |
| AutoScroll:true/false; | If the control exceeds the window, whether to automatically display the scroll bar |
| AutoSize:true/false; | Whether the scope of the window will exceed the size of the control |
| MaximumSize:0,0; | The maximum size a window can be dragged |
| MinimumSize:0,0; | The smallest size a window can drag |
| Size:300,300; | The default size when the window is opened |
| StartPosition:centerScreen; | The default desktop location when the window is opened , In the middle |
| WindowState:Maximized; | The default open window is maximized . |
(3) Attribute value of appearance , It is mainly used to change the form color 、 Font, title, etc .
| Property value | explain |
| Font: Song style ,9pt; | You can change the font size , The larger the font, the larger the control |
| Text; | Input text |
| TextAlign; | Text location |
| FormBorderStyle:FixedSingle; | The window cannot be dragged |
| FormBorderStyle:None; | Hide the border of the window |
| DropDownStyle:DropDownList; | Make the drop-down box unable to enter text |
3、 Set form properties
The icon of the form is system Default icon .

Change icon , stay “ attribute ” The palette , choice Icon character

Color of the form And background , adopt BackgroundImage Property to set . choice “ attribute ” In the panel BackgroundImage attribute .

Common events for forms
The so-called event , It means what's going to happen , It can be simply understood as the user's operation , It is triggered by an object . All events of the form , Can be in “ attribute ” Panel to view .
1、 Add event
Add an event to the form , Just select the event to be added in the event panel ,Load Double click in the following space , The corresponding event will be generated automatically .
(1) When the form is loaded , A form loading event will be triggered Load.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(" Form loading completed ");
}
}
}
(2) When you click the form , Trigger Click( single click ) event .

Click double-click .

The code is as follows :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(" Form loading completed ");
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show(" Click form ");
}
}
}

(3) When you close the form , Trigger FormClosing( close ) event

The code is as follows :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
MessageBox.Show(" Form loading completed ");
}
private void Form1_Click(object sender, EventArgs e)
{
MessageBox.Show(" Click form ");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show(" Are you sure you want to close the form ?");
}
}
}

2、 Delete event
Do not understand , Don't master .
Display and hide of forms
Form identifier .Show()
Form identifier .Hide()
MDI forms
Single document interface (SDI)
Multi document interface (MDI)
MDI The concept of form
MDI forms (Multiple-Document Interface, Multi document interface ) Used to display multiple documents at the same time .
Used in projects MDI On the window , Usually a MDI Window form as parent form , A parent form can contain multiple child forms in its workspace .
Set up MDI forms
1、MDI Container form
Set a form as a window form , There are two ways :
1、 On the window “ attribute ” The palette , take IsMidContainer Property is set to True that will do .

False:
True:

2、 On the window Load Add the following statement to the event

The code is as follows :
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.IsMdiContainer = true;
}
}
}
2、 Set up MDI Subform
MDI A subform is a normal form . You only need to convert the MdiParent Property to a MDI Parent form , It is the child form of the parent form , The syntax is as follows :
Form instance name .MdiParent= Parent form object ;
Set up MDI Containers :

After modification :

from Form5 In the attribute , adopt Name Property to change to “MainForm”

After modification :

Text Property represents the form title , It is amended as follows “MDI forms ”

After modification :

New subform (Form1.cs、Form2.cs、Form3.cs、Form4.cs):

Code content :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form5
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
form1.MdiParent = this;
Form2 form2 = new Form2();
form2.Show();
form2.MdiParent = this;
Form3 form3 = new Form3();
form3.Show();
form3.MdiParent = this;
Form4 form4 = new Form4();
form4.Show();
form4.MdiParent = this;
}
}
}After completion :

The operation results are as follows :

array MDI forms
The syntax is as follows :
public void LayoutMdi(MdiLayout value)
value yes MdiLayout One of the enumeration values of , Used to define MDI Layout of subform .
| Members of the enumeration | explain |
| Cascade | Cascade arrangement MDI Subform |
| TileHorizontal | Tile horizontally MDI Subform |
| TileVertical | Tile vertically MDI Subform |
Create a new one Windows Forms application , stay Name Property to name the form unique identifier MainForm, And in Text Properties of the , Name the title of the main form “MDI forms ”.
Select from the menu bar “ View ” command , Re selection “ hold-all ” command , The toolbox will pop up .

Select... In the toolbox MenuStrip Control , A row of menu bars will appear in the form , And then enter “ New form ”、“ Cascade arrangement ”、“ Tile horizontally ”、“ Tile vertically ”、“ close ”.



next , then IsMdiContainer Property is set to True.
Create a subform , And named it “ChildForm”, Next, you can double-click the menu item , Add events .

The code is as follows :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Form5
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private void MainForm_Load(object sender, EventArgs e)
{
Form1 form1 = new Form1();
form1.Show();
form1.MdiParent = this;
Form2 form2 = new Form2();
form2.Show();
form2.MdiParent = this;
Form3 form3 = new Form3();
form3.Show();
form3.MdiParent = this;
Form4 form4 = new Form4();
form4.Show();
form4.MdiParent = this;
}
private void New form ToolStripMenuItem_Click(object sender, EventArgs e)
{
ChildForm mychildForm=new ChildForm();
mychildForm.Show();
mychildForm.MdiParent = this;
}
private void Cascade arrangement ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.Cascade);
}
private void Tile horizontally ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileHorizontal);
}
private void Tile vertically ToolStripMenuItem_Click(object sender, EventArgs e)
{
LayoutMdi(MdiLayout.TileVertical);
}
private void close ToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();
}
}
}
The operation results are as follows :

Click new form :
Cascade arrangement :

Tile horizontally :

Tile vertically :

File control
Select class control
Group class control
menu bar 、 Toolbar and status bar controls
边栏推荐
- Paper 3 vscode & texlive & sumatrapdf create a perfect tool for writing papers
- 大火的虚拟人在哪些产业开始发力?
- 论文3 VScode&texlive&SumatraPDF打造完美书写论文工具
- PY SQL可以获取到表结构吗?
- Grafana draws the trend chart
- Ffmpeg usage in video compression processing
- Markdown绘图mermaid实用教程
- easypoi
- Upward and downward transformation
- MDM data analysis function description
猜你喜欢

视差js特效js轮播图插件

pd. Difference between before and after cut interval parameter setting

From design delivery to development, it is easy and efficient!

Upward and downward transformation

春风动力携手华为打造智慧园区标杆,未来工厂创新迈上新台阶

i人事HR系统上架企业微信ISV,增强企微在服务连锁零售等行业深度应用

智能计算系统3 Plugin 集成开发的demo

道路千万条,为什么这家创新存储公司会选这条?

智能计算系统1 环境搭建

团体程序设计天梯赛练习题-持续更新中
随机推荐
严重性 代码 说明 项目 文件 行 禁止显示状态 错误 C1047 对象或库文件“.lib”是使用与其他对象(如“x64\Release\main.obj”)不同的
使用点云构建不规则三角网TIN
H5 sunflower operation
PY SQL可以获取到表结构吗?
Win11如何给系统盘瘦身?Win11系统盘瘦身方法
道路千万条,为什么这家创新存储公司会选这条?
Ffmpeg usage in video compression processing
redisTemplate
SQL Server2019 新建 SQL Server身份验证用户名 并登录
Grafana draws the trend chart
Machine learning notes temperature+softmax
Tencent tangdaosheng: facing the new world of digital and real integration, developers are the most important "architects"
Double contextual relationship network for polyp segmentation
Rigid error: could not extract PIDs from PS output PIDS: [], Procs: [“bad pid
Judge whether the string is empty
Design of secsha system
How does win11 slim down the system disk? Slimming method of win11 system disk
数字藏品,万字长文,你想知道的大部分问题都讲清楚了从业者必看
Redis 如何实现库存扣减操作?如何防止商品被超卖?
Can only one task be submitted by one table if the flinkcdc is submitted by flinksql? When there are thousands of watches