//1.添加两个按钮一个文本框如下图:当用户点击爱时弹出“我也爱你哟”,点击不爱时弹出“还是被你给点到了”并退出程序
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace Love_and_notLove {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void Lovebtn_Click(object sender, EventArgs e) {
MessageBox.Show(\我也爱你哟!\); this.Close(); }
//触发点击事件
private void NotLovebtn_Click(object sender, EventArgs e) {
MessageBox.Show(\还是被你给点到了\); //关闭主程序 this.Close(); }
//针对鼠标移动至按钮所属区域时触发事件
private void NotLovebtn_MouseEnter(object sender, EventArgs e) {
//声明一个变量用于存放按钮的X轴坐标
int x = this.ClientSize.Width-NotLovebtn.Width; //声明一个变量用于存放按钮的Y轴坐标
int y = this.ClientSize.Height-NotLovebtn.Height;
//声明一个随机数实例 Random r = new Random(); //声明按钮的移动范围
NotLovebtn.Location = new Point(r.Next(0, x + 1), r.Next(0, y + 1)); } } }
//2.在窗口中放置两个控件如下图:
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace Lable控件和TextBox控件 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void txtBox_TextChanged(object sender, EventArgs e) {
lblTxt.Text = txtBox.Text; //使lable控件的值等于textbox的值 } } }
//3.做一个跑马灯的练习控件如下图:在窗体中放入label和Timer两个控件并分别设置两个控件的属性 Label控件:Text:☆★ Timer控件:Enable:true
Tick:事件
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms;
namespace 跑马灯 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void timer1_Tick(object sender, EventArgs e) {
label1.Text = label1.Text.Substring(1) + label1.Text.Substring(0, 1); } } } //4.小闹钟
在窗体中放一个label和timer两个控件并修改其属性 Timer控件:Interval:1000
Enable:True
Label 控件:Name:labtime using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq;
using System.Text;
using System.Windows.Forms; using System.Media;
namespace 小闹钟 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void timer2_Tick(object sender, EventArgs e) {
labTime.Text = DateTime.Now.ToString();
if (DateTime.Now.Hour == 12 && DateTime.Now.Minute == 02 && DateTime.Now.Second == 00) {
SoundPlayer sp = new SoundPlayer();
sp.SoundLocation = @\; sp.Play(); } }
private void Form1_Load(object sender, EventArgs e) {
labTime.Text = DateTime.Now.ToString(); } } }
//5.需要输入用户名“admin”和密码“admin”才能登陆到使用介面的记事本程序: //在窗体中需要拖入2 个label控件4个button控件3个textBox控件并修改其相应属性 Label控件:label1:
Text: 用户名:
Label2: Text: 密码:
textBox控件:textBox2:
passwrodChar:*
textBox3: MultiLine(多行) WordWrap:False
Button控件:button1:
Name:btnLogin Text:登陆 Button2:
Name:btnReset Text:重置 Button3: Name:butWords Text:自动换行 Button4: Name:butSave Text:保存
using System;
using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text;
using System.Windows.Forms; using System.IO;
namespace 记事本程序 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
///
/// 程序运行时隐藏文本编程器 ///
///
private void Form1_Load(object sender, EventArgs e) {
//txtWords.WordWrap = false; btnWords.Visible = false; btnSave.Visible = false;
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库C#winform练习在线全文阅读。
相关推荐: