C#之窗体应用程序设计:窗体、控件的使用
实验内容:在主窗口上放置一ListBox和两个Button,Button1和Button2;新建Window窗体Form2,在Form2上放置一button;要求主窗体居中显示。点击主窗体上的button1,,弹出窗体Form2,实现点击Form2窗体上的按钮时,在Form1的ListBox控件里面添加任意字符串; 在Form2窗体上添加合适的控件,要求输入一十进制数,输出这个数的八进制和二进制;
步骤:
- 打开vs,新建WPF文件,打开工具箱,拖动需要的控件至FORM1,并且调整大小和位置。
- 通过右侧的属性修改控件的一些属性,例如: Name,Text,外观,位置,颜色等等
【Name】:获取或设置控件的名称;
【Text】:获取或设置与此控件相关联的文本;
…
- 一般控制台应用程序都从main()主程序开始切入,那么Windows窗体的程序从哪开始切入呢?
我们在解决方案资源管理器solution explorer中来查看program.cs文件,打开后可以发现下面这段程序代码:
而这里面的main()主程序下调用了application静态类的方法,来执行windows窗体应用程序,其实application类下的EnableVisualStyles()函数所实现的功能是提供可视化效果,而它下面的run()函数则是实现窗体的运行。
那么,用户与windows程序界面如何进行交互呢?
当然是通过事件驱动event driven来产生事件event,而这些事件包含单击或者双击和移动鼠标等,然而,我们对事件进行触发大部分用的是鼠标的click事件。
通过控件都有它默认的事件处理程序,当我们在窗体上进行双击时则进入窗体的加载事件form1_load(),双击按钮控件则进入button1_click()事件,不同的控件可使用这种方法来进入到代码编辑区域。
Windows窗口程序中消息循环message loop是通过使用application类提供的静态方法来启用和停止的,而application类下的run()方法和exit()方法就是用来启用和停止消息循环的方法。
- 双击控件进入编辑代码;
Form1.cs
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 Cshape窗体应用程序设计实验一
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void Btn_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();//对Form2实例化
this.Hide();//对用户隐藏控件Form1
form2.ShowDialog();//将Form2显示为模式对话框
this.Dispose();//将Form1释放掉,Form1只是隐藏了但是在后台一直在运行,所以必须释放掉
}
private void add_Click(object sender, EventArgs e)//自定义添加选项
{
if (textBox1.Text != String.Empty)
{
listBox1.Items.Add(textBox1.Text);
//listBox1.Text = textBox1.Text;
}
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{//将listbox中索引项内容输入到textbox1中
if (listBox1.SelectedIndex >= 0 && listBox1.SelectedIndex < listBox1.Items.Count)
textBox1.Text = Convert.ToString(listBox1.Items[listBox1.SelectedIndex]);
}
private void delete_Click(object sender, EventArgs e)
{//实现删除listbox索引项内容操作
if (listBox1.SelectedIndex >= 0 && listBox1.SelectedIndex < listBox1.Items.Count)
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
}
}
注释:
- 当用户单击按钮时,系统将调用Click事件处理程序,在Click事件的代码块中进行编辑可以完成对单击按钮事件的响应。
- 如何实现多个窗体之间的跳转:
其中Show();或ShowDialog(); 都可以
区别在于,Show()是无模式,ShowDialog()是有模式
有模式的意思是,在Form2打开的情况下无法激活Form1.
private void Btn_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();//对Form2实例化
this.Hide();//对用户隐藏控件Form1
form2.ShowDialog();//将Form2显示为模式对话框
this.Dispose();//将Form1释放掉
}
-
如何新建其他窗体
项目——>添加窗体 -
如果想要自定义添加到列表当中选项,可以点击添加,添加进去选项
private void add_Click(object sender, EventArgs e)
{
if (textBox1.Text != String.Empty)
{
listBox1.Items.Add(textBox1.Text);
//listBox1.Text = textBox1.Text;
}
}
对Form2进行上述操作步骤 :得到如下窗口视图
注释:
- 如何使控件居中:
1、通用型:重写OnResize(EventArgs e)方法,通过计算,重新定位控件的位置。(优点:准确,通用,即使窗体改变也能使用;缺点:麻烦并且只有在运行的时候才能看到效果)
2、万能型:直接在属性界面计算出居中坐标,设置为控件的Location属性值。并设置锚点Anchor为Top,钉住控件顶部位置。(优点:准确,通用,能够实时预览;缺点:一个控件算一次,算完还得写上去,不吐血?好,万一窗体大小改变了,万一有很多个控件呢?)
以上两种方法都是通过计算确定位置,一个是代码去确定的,一个是人工计算并填写,所以不止能设置居中,什么居左,居右,居上,居下,都是可以的。前提是,很烦。
3、Label控件:将label的AutoSize属性设置为false,把label控件拉满或设置Dock属性为fill,填满父容器,再设置TextAlgin为MiddleCenter(居中)即可。(优点:动动鼠标就ok,实时预览;缺点:只适用于不设置背景的label控件,label设置背景就和按钮一样了,而且控件多了互相覆盖,洒得到处都是)
属性说明:
AutoSize(false):取消自动调整大小,这样控件就不会随窗体的改变而改变大小,位置等,并能够自定义控件的区域,位置等。
TextAlgin(MiddleCenter):文本对齐方式为居中。label的尺寸一般总是刚刚包围住所有文字,而且又是透明的,所以,正常情况下该属性没什么效果,只有当控件的区域比文字区域大了很多时,文字才明显不对头了。这时,就需要设置对齐方式。类似于对齐子控件,内容:
private void Form2_Load(object sender, EventArgs e)
{
base.OnResize(e);
int x = (int)(0.5 * (this.Width - button1.Width));
int y = button1.Location.Y;
button1.Location = new System.Drawing.Point(x, y);
}
进入Form2代码编辑:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace Cshape窗体应用程序设计实验一
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = new Form1();
this.Hide();
form1.ShowDialog();
this.Dispose();
}
private void Form2_Load(object sender, EventArgs e)
{
base.OnResize(e);
int x = (int)(0.5 * (this.Width - button1.Width));
int y = button1.Location.Y;
button1.Location = new System.Drawing.Point(x, y);
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
}
private void label1_Click(object sender, EventArgs e)
{
}
private void tb1_TextChanged(object sender, EventArgs e)
{
if (tb1.Text == "")
{
tb2.Text = "";
tb3.Text = "";
}else{
try
{
tb2.Text = Convert.ToString(Convert.ToInt32(tb1.Text), 8);
tb3.Text = Convert.ToString(Convert.ToInt32(tb1.Text), 2);
}catch(Exception ex){
MessageBox.Show(ex.Message + "\n请重新输入!", "输入有误!");
return;
}
}
}
}
}
注释:
- 在一个TextBox中输入数字,其它TextBox的内容也要同步变化,所以为每个TextBox创建TextChanged事件,只要该TextBox的内容发生变化就会触发该事件。(双击事件类型即可自动创建)
- 控件属性:通过上述步骤中属性调整编译器会自动修改好,不需要自己动手敲。
namespace Cshape窗体应用程序设计实验一
{
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.But = new System.Windows.Forms.Button();
this.add = new System.Windows.Forms.Button();
this.listBox1 = new System.Windows.Forms.ListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.delete = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// But
//
this.But.Location = new System.Drawing.Point(395, 41);
this.But.Name = "But";
this.But.Size = new System.Drawing.Size(388, 50);
this.But.TabIndex = 0;
this.But.Text = "打开窗口二";
this.But.UseVisualStyleBackColor = true;
this.But.Click += new System.EventHandler(this.Btn_Click);
//
// add
//
this.add.Location = new System.Drawing.Point(395, 315);
this.add.Name = "add";
this.add.Size = new System.Drawing.Size(181, 50);
this.add.TabIndex = 1;
this.add.Text = "添加";
this.add.UseVisualStyleBackColor = true;
this.add.Click += new System.EventHandler(this.add_Click);
//
// listBox1
//
this.listBox1.FormattingEnabled = true;
this.listBox1.ItemHeight = 20;
this.listBox1.Location = new System.Drawing.Point(0, 41);
this.listBox1.Name = "listBox1";
this.listBox1.Size = new System.Drawing.Size(389, 324);
this.listBox1.TabIndex = 2;
this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(395, 199);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(388, 108);
this.textBox1.TabIndex = 3;
//
// delete
//
this.delete.Location = new System.Drawing.Point(602, 315);
this.delete.Name = "delete";
this.delete.Size = new System.Drawing.Size(181, 50);
this.delete.TabIndex = 4;
this.delete.Text = "删除";
this.delete.UseVisualStyleBackColor = true;
this.delete.Click += new System.EventHandler(this.delete_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 20F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.delete);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.listBox1);
this.Controls.Add(this.add);
this.Controls.Add(this.But);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button But;
private System.Windows.Forms.Button add;
private System.Windows.Forms.ListBox listBox1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button delete;
}
}