代码如下:
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 P6_6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.button1.Click += new EventHandler(button_Click);//关联事件处理方法
this.button2.Click += new EventHandler(button_Click);//关联事件处理方法
this.button3.Click += new EventHandler(button_Click);//关联事件处理方法
}
void button_Click(object sender,EventArgs e)
{
textBox1.Text = "您按下了" + ((Button)sender).Text;//在textBox1中输出button信息
}
}
}
代码如下:
using System;
namespace P6_1
{
delegate void DualFunction(double x, double y);//用关键字delegate关键字定义DualFunction委托
class Program
{
static void Main()
{
DualFunction fun1;//定义方法变量fun1
double a = 2.5, b = 2;
Console.Write("请选择函数(加0 减1 乘2 除3)");
int i = int.Parse(Console.ReadLine());
if (i == 0)
fun1 = new DualFunction(Add);//将Add封装到fun1
else if (i == 1)
fun1 = new DualFunction(Sub);//将Sub封装到fun1
else if (i == 2)
fun1 = new DualFunction(Mul);//将Mul封装到fun1
else
fun1 = new DualFunction(Div);//将Div封装到fun1
fun1(a, b);
}
static void Add(double x,double y)//定义Add方法
{
Console.WriteLine("{0} + {1} = {2}", x, y, x + y);
}
static void Sub(double x, double y)//定义Sub方法
{
Console.WriteLine("{0} - {1} = {2}", x, y, x - y);
}
static void Mul(double x, double y)//定义Mul方法
{
Console.WriteLine("{0} * {1} = {2}", x, y, x * y);
}
static void Div(double x, double y)//定义Div方法
{
Console.WriteLine("{0} / {1} = {2}", x, y, x / y);
}
}
}
输出:
请选择函数(加0 减1 乘2 除3)1
2.5 - 2 = 0.5
代码如下:
using System;
namespace P6_2
{
class Program
{
static void Main()
{
Student[] students = new Student[5];//定义Student类的数组
//为students数组赋初值
students[0] = new Student("王小红", 20, 1);
students[1] = new Student("周军", 23, 2);
students[2] = new Student("方小白", 21, 2);
students[3] = new Student("高强", 25, 3);
students[4] = new Student("王浩", 22, 3);
Student.CompareFunction compare;//定义方法变量compare
//选择需要的功能(即需要封装的方法)
Console.WriteLine("请选择排序方式:A姓名 B年龄 C年级");
char ch = Console.ReadKey().KeyChar;
if (ch == 'A' || ch == 'a')
compare = CompareName;
else if (ch == 'B' || ch == 'b')
compare = CompareAge;
else
compare = CompareGrade;
//进行排序
Student.SortAndPrint(students, compare);
}
static int CompareName(Student s1,Student s2)
{
return 0 - s1.Name.CompareTo(s2.Name);//比较姓名(字符串的比较)
}
static int CompareAge(Student s1, Student s2)
{
return s1.Age - s2.Age;//比较年龄
}
static int CompareGrade(Student s1, Student s2)
{
return s1.Grade - s2.Grade;//比较年级
}
}
public class Student
{
private string name;//定义私有成员name
public string Name//用Name属性封装私有成员name
{
get { return name; }
}
private int age;//定义私有成员age
public int Age//用Age属性封装私有成员age
{
get { return age; }
}
public int Grade { get; set; }//定义Grade
public Student(string name,int age,int grade)//构造函数进行初始化
{
this.name = name;
this.age = age;
this.Grade = grade;
}
public static void SortAndPrint(Student[] students, CompareFunction comparre)//按照传入的比较方法进行排序
{
//使用冒泡排序让成员从小到大排列
for (int i = students.Length - 1; i > 0; i--)
for (int j = 0; j < i; j++)
if (comparre(students[j], students[j + 1]) > 0)
{
//交换顺序使得成员从小到大排列
Student s = students[j];
students[j] = students[j + 1];
students[j + 1] = s;
}
//逐个输出数组成员
foreach (Student s in students)
Console.WriteLine(s);
}
//格式化输出形式
public override string ToString()
{
return string.Format("{0} {1}岁 {2}年级", name, age, Grade);
}
//用关键字delegate关键字定义CompareFunction委托
public delegate int CompareFunction(Student s1, Student s2);
}
}
输出:
请选择排序方式:A姓名 B年龄 C年级
A周军 23岁 2年级
王小红 20岁 1年级
王浩 22岁 3年级
高强 25岁 3年级
方小白 21岁 2年级
代码如下:
using System;
namespace P6_4
{
class Program
{
static void Main()
{
TrafficLight light = new TrafficLight();
Car car1 = new Car();
car1.Enter(light);//委托合并
Ambulance amb1 = new Ambulance();
amb1.Enter(light);//委托合并
light.ChangeColor();//绿灯变红灯
light.ChangeColor();//红灯变绿灯
amb1.Emergent = true;//救护车变为紧急状态
light.ChangeColor();//绿灯变红灯
}
}
//用关键字delegate关键字定义LightEvent委托
public delegate void LightEvent(bool color);
public class TrafficLight
{
private bool color = false;
public bool Color
{
get { return color; }
}
//发布事件OnColorChange
public event LightEvent OnColorChange;
//定义ChangeColor方法
public void ChangeColor()
{
color = !color;//改变灯光状态
Console.WriteLine(color ? "红灯亮" : "绿灯亮");
if (OnColorChange != null)
OnColorChange(color);//激发委托
}
}
public class Car
{
private bool bRun = true;//定义车辆行驶状况
public void Enter(TrafficLight light)//事件订阅
{
light.OnColorChange += LightColorChange;
}
public void Leave(TrafficLight light)//取消事件订阅
{
light.OnColorChange -= LightColorChange;
}
public virtual void LightColorChange(bool color)//虚拟事件处理方法
{
if(bRun && color)//如果正在行驶并且是红灯
{
bRun = false;
Console.WriteLine("{0}停车",this);
}
else if (!bRun && !color)//如果汽车没有启动并且是绿灯
{
bRun = true;
Console.WriteLine("{0}启动", this);
}
}
}
public class Ambulance : Car
{
private bool emergent = false;//定义救护车紧急情况
public bool Emergent
{
get { return emergent; }
set { emergent = value; }
}
public override void LightColorChange(bool color)
{
if (emergent)//如果是紧急情况直接行驶
Console.WriteLine("{0}紧急行驶", this);
else//否则按照交通信号灯行驶
base.LightColorChange(color);
}
}
}
输出:
红灯亮
P6_4.Car停车
P6_4.Ambulance停车
绿灯亮
P6_4.Car启动
P6_4.Ambulance启动
红灯亮
P6_4.Car停车
P6_4.Ambulance紧急行驶
代码如下:
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 P7_1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
LocationChanged += new EventHandler(Form1_LocationChanged);//事件订阅
SizeChanged += new EventHandler(Form1_SizeChanged);//事件订阅
}
//定义SizeChanged事件的处理方法
void Form1_SizeChanged(object sender,EventArgs e)
{
this.Text = "窗体尺寸:" + this.Size.ToString();
}
//定义LocationChanged事件的处理方法
void Form1_LocationChanged(object sender, EventArgs e)
{
this.Text = "窗体位置:" + this.Location.ToString();
}
}
}
代码如下:
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 P7_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;
}
//定义Form1_FormClosing方法
void Form1_Load(object sender,EventArgs e)
{
if (MessageBox.Show("要进入程序吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.Cancel)
this.Close();
else
this.FormClosing += Form1_FormClosing;
}
//定义Form1_Load方法
void Form1_FormClosing(object sender,FormClosingEventArgs e)
{
if (MessageBox.Show("确认要退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) == DialogResult.No)
e.Cancel = true;
}
}
}
代码如下:
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 P7_4
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.textBox1.KeyPress += textBox1_KeyPress;
}
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar >= 32 && e.KeyChar < 127)
label1.Text = e.KeyChar.ToString() + label1.Text;//将新输入的字符显示出来
if (e.KeyChar == '\b' && label1.Text.Length > 0)
label1.Text = label1.Text.Remove(0, 1);//如果是退格键,就将光标从0移动到1,并且把光标之前的元素都进行删除
}
}
}
代码如下:
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 P7_5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
//订阅事件
checkBox1.CheckedChanged += checkBox1_CheckedChanged;
radioButton1.CheckedChanged += radioButton_CheckedChanged;
radioButton2.CheckedChanged += radioButton_CheckedChanged;
radioButton3.CheckedChanged += radioButton_CheckedChanged;
}
//定义checkBox1_CheckedChanged方法
void checkBox1_CheckedChanged(object sender,EventArgs e)
{
//如果点击了,则button按键是三维风格
if (checkBox1.Checked) button1.FlatStyle = FlatStyle.Standard;
//反之是扁平风格
else button1.FlatStyle = FlatStyle.Flat;
}
void radioButton_CheckedChanged(object sender,EventArgs e)
{
//定义居中
if (radioButton1.Checked)
{
button1.TextAlign = ContentAlignment.MiddleCenter;
button1.ImageAlign = ContentAlignment.MiddleCenter;
}
//定义左图右文
else if (radioButton2.Checked)
{
button1.TextAlign = ContentAlignment.MiddleLeft;
button1.ImageAlign = ContentAlignment.MiddleRight;
}
//定义右图左文
else if (radioButton3.Checked)
{
button1.TextAlign = ContentAlignment.MiddleRight;
button1.ImageAlign = ContentAlignment.MiddleLeft;
}
}
}
}
代码如下:
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 P7_7
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.Load += Form1_Load;//课本少写了这一句
trackBar1.ValueChanged += new EventHandler(trackBar_ValueChanged);
trackBar2.ValueChanged += new EventHandler(trackBar_ValueChanged);
trackBar3.ValueChanged += new EventHandler(trackBar_ValueChanged);
}
//设置窗体初始背景色
private void Form1_Load(object sender,EventArgs e)
{
this.BackColor = Color.FromArgb(0, 0, 0);
this.Text = "窗体背景色:(0,0,0)";
}
//调整窗体背景色
void trackBar_ValueChanged(object sender,EventArgs e)
{
this.BackColor = Color.FromArgb(trackBar1.Value, trackBar2.Value, trackBar3.Value);
this.Text = string.Format("窗体背景色:({0},{1},{2})", this.BackColor.R, this.BackColor.G, this.BackColor.B);
}
}
}
输出:
代码如下:
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 P7_8
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
button1.Click += button1_Click;
}
//统计处理的控件数量
void button1_Click(object sender,EventArgs e)
{
int i = EmphAllCtrls(this);
MessageBox.Show(string.Format("共处理{0}个控件", i));
}
//加粗,改斜体
public int EmphAllCtrls(Control ctrl)
{
int i = 0;
foreach (Control c in ctrl.Controls)
{
//字体加粗,改斜体
c.Font = new Font(c.Font, FontStyle.Bold | FontStyle.Italic);
i++;
if (c.HasChildren)
i += EmphAllCtrls(c);
}
return i;
}
}
}
代码如下:
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 P7_10
{
public partial class Form1 : Form
{
public Form1()
{
//订阅事件
InitializeComponent();
menuItemWindowMiddle.Checked = true;
menuItemWindowBig.Click += menuItem_Click;
menuItemWindowMiddle.Click += menuItem_Click;
menuItemWindowSmall.Click += menuItem_Click;
}
//定义点击方法
void menuItem_Click(object sender,EventArgs e)
{
//定义变量存对象的菜单
ToolStripMenuItem item = (ToolStripMenuItem)sender;
if(item == menuItemWindowBig)
{
//定义大窗口
menuItemWindowBig.Checked = true;
menuItemWindowMiddle.Checked = menuItemWindowSmall.Checked = false;
this.Size = new Size(800, 450);
}
else if (item == menuItemWindowMiddle)
{
//定义中窗口
menuItemWindowMiddle.Checked = true;
menuItemWindowBig.Checked = menuItemWindowSmall.Checked = false;
this.Size = new Size(480, 270);
}
else if (item == menuItemWindowSmall)
{
//定义小窗口
menuItemWindowSmall.Checked = true;
menuItemWindowBig.Checked = menuItemWindowMiddle.Checked = false;
this.Size = new Size(240, 135);
}
}
}
}
.NET 类库提供了丰富的 Windows 窗体和控件类型,它们大都在 System. Windows.Forms 命名空间中定义。其中Form 类是所有 Windows 窗体的基类,而 Control 则是 Form 及其他所有控件类的共同基类。它们主要通过鼠标和键盘等事件来响应用户操作。在Visual Studio 开发环境中能够方便地设置控件的各种属性和事件处理方法。
输入文字时一定要切换成英文输入法、记得让修改控件名称与代码中的名称保持一致、查看窗体代码是双击控件会出现方法的定义,如果只删除方法程序会出现错误等。