C#:02窗体介绍

mac2024-02-23  58

 

02 Button按钮 - I_love_U

namespace I_love_you { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //-------------------------------------------------------------------------------- // 当鼠标进入按钮的可见部分的时候,给按钮一个新的坐标 private void button2_MouseEnter(object sender, EventArgs e) { //给按钮一个新坐标 //这个按钮的最大宽度就是窗体宽度减去按钮的宽度 //int x = this.Width - button1.Width; this.width是固定的 int x = this.ClientSize.Width - button2.Width; int y = this.ClientSize.Height - button2.Height; Random r = new Random();//要给按钮一个随机的坐标 //光标移动到Location位置出现提示,如果用Point(x,y)坐标就写死了 button2.Location = new Point(x,y); //r.Next(0, x),最大值为x-1 button2.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1)); } //-------------------------------------------------------------------------------- private void button1_Click(object sender, EventArgs e) { MessageBox.Show("爱你too"); this.Close();//关闭主窗体 } } }

03 Timer控件 - 跑马灯

namespace _03Timer { public partial class Form1 : Form { public Form1() { InitializeComponent(); } //-------------------------------------------------------------------------------- private void timer1_Tick(object sender, EventArgs e) { //Substring(1),从下标1到最后字符的子串 label1.Text=label1.Text.Substring(1) + label1.Text.Substring(0, 1); } //-------------------------------------------------------------------------------- // 每隔1秒钟将时间赋值给label2 private void timer2_Tick(object sender, EventArgs e) { // DateTime.Now.ToString();获取当前时间 label2.Text = DateTime.Now.ToString(); //先做下面Form1_Load,再做音乐 if(DateTime.Now.Hour==21&&DateTime.Now.Minute==36&&DateTime.Now.Second==10) { //播放音乐 ALT+SHIFT+F10 ,自动添加using引用命名空间 SoundPlayer sp = new SoundPlayer(); sp.SoundLocation = @"d:\12 边疆泉水清又纯.WAV"; sp.Play(); } } //-------------------------------------------------------------------------------- // 当窗体加载的时候,将当前系统时间赋值给lable2 private void Form1_Load(object sender, EventArgs e) { label2.Text = DateTime.Now.ToString(); } } }

04 TextBox控件- 简单记事本

   

大家可以对控件的name属性对控件改名

namespace _04简单记事本 { public partial class Form1 : Form { //-------------------------------------------------------------------------------- public Form1() { InitializeComponent(); button3.Visible = false; button4.Visible = false; textBox3.Visible = false; } //-------------------------------------------------------------------------------- //登录按钮 private void button1_Click(object sender, EventArgs e) { if (textBox1.Text == "admin" && textBox2.Text == "admin") { MessageBox.Show("登录成功!"); button1.Visible = false; button2.Visible = false; textBox1.Visible = false; textBox2.Visible = false; label1.Visible = false; label2.Visible = false; button3.Visible = true; button4.Visible = true; textBox3.Visible = true; } else { MessageBox.Show("登录失败"); //清空文本框 textBox1.Clear(); textBox2.Clear(); //让用户名文本框获得焦点 textBox1.Focus(); } } //-------------------------------------------------------------------------------- //重置按钮 private void button2_Click(object sender, EventArgs e) { //清空文本框 textBox1.Clear(); textBox2.Clear(); //让用户名文本框获得焦点 textBox1.Focus(); } //-------------------------------------------------------------------------------- // 自动换行按钮 private void button3_Click(object sender, EventArgs e) { //判断当前是否为自动换行,如果是,取消自动换行,如果不是,改成取消自动换行 if(button3.Text=="自动换行") { textBox3.WordWrap = true; button3.Text = "取消自动换行"; } else if(button3.Text == "取消自动换行") { textBox3.WordWrap = false; button3.Text = "自动换行"; } } //-------------------------------------------------------------------------------- //保存按钮 //using System.IO; private void button4_Click(object sender, EventArgs e) { using (FileStream fsWrite = new FileStream(@"C:\Users\cmy\Desktop\123.txt", FileMode.OpenOrCreate, FileAccess.Write)) { string str = textBox3.Text.Trim(); byte[] buffer = System.Text.Encoding.Default.GetBytes(str); fsWrite.Write(buffer, 0, buffer.Length); } MessageBox.Show("保存成功"); } } }

 

最新回复(0)