对话框案例
两个textbox,一个button按钮
最大的是消息框(textbox2),小的是发送框(textbox1)
form窗体的属性AcceptButton ,后面给值button1,实现效果为发送框按键回车则发送内容进入信息框。
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 聊天窗口
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Width = this.Width;
textBox2.Width = this.Width;
textBox2.ReadOnly = true;
textBox2.BackColor = Color.White;
textBox2.Font = new Font("微软雅黑",10);
}
private void button1_Click(object sender, EventArgs e)
{
Random r = new Random();
int i = r.Next(8);
string name = "";
if (i==0)
{
name = "易烊千玺";
}
if (i == 1)
{
name = "胡歌";
}
if (i == 2)
{
name = "李木子";
}
if (i == 3)
{
name = "润玉";
}
if (i == 4)
{
name = "公主";
}
if (i == 5)
{
name = "大祭司";
}
if (i == 6)
{
name = "公子";
}
if (i == 7)
{
name = "殿下";
}
if (textBox1.Text!="")
{
string str = name + "(" + DateTime.Now + ")" + "\r\n" +textBox1.Text+"\r\n";
textBox2.Text = textBox2.Text + str;
//信息框中内容过多时,自动滚动显示最新消息
textBox2.SelectionStart = textBox2.Text.Length-1;
textBox2.ScrollToCaret();
textBox1.Text = "";
}
}
}
}
闹钟案例
四个label,三个textbox,两个button,一个timer
第一个label用来显示系统当前时间
其他三个 表示时分秒
三个textbox中分别输入闹钟时间
timer定时器
timer属性设置如图(1000ms=1s)
添加事件 Tick(timer1_Tick)
第一个button表示确定闹钟
第二个button表示停止音乐
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;
using System.Media;
namespace 闹钟
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
}
int h, m,s;
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToString();
if (h == DateTime.Now.Hour)
{
if (m == DateTime.Now.Minute)
{
if (s==DateTime.Now.Second) {
SoundPlayer sound = new SoundPlayer();
sound.SoundLocation = "music/shengpizi.wav";
sound.Play();
}
}
}
}
private void button1_Click(object sender, EventArgs e)
{
h = int.Parse(textBox1.Text);
m = int.Parse(textBox2.Text);
s = int.Parse(textBox3.Text);
}
private void button2_Click(object sender, EventArgs e)
{
SoundPlayer sound = new SoundPlayer();
sound.SoundLocation = "music/shengpizi.wav";
sound.Stop();
}
}
}
简单的计算器
三个textbox,一个button按钮,一个comcobox下拉选择算法+,- ,*,/,%
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 计算器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//设置comboBox1只读
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
textBox3.ReadOnly = true;
comboBox1.SelectedIndex = 0;
}
private void button1_Click(object sender, EventArgs e)
{
//将comboBox中的文本内容(string)转换为int类型
int a = int.Parse(textBox1.Text);
int b = int.Parse(textBox2.Text);
//索引值从0开始
int sel = comboBox1.SelectedIndex;
int c=0 ;
if (sel==0) {
c = a + b;
}
if (sel==1) {
c = a - b;
}
if (sel==2) {
c = a * b;
}
if (sel ==3)
{
c = a / b;
}
if (sel == 4)
{
c = a % b;
}
textBox3.Text = c.ToString();
}
}
}
告白神器
一个label,三个button
两个button都是不喜欢
当鼠标放在一个 不喜欢按钮时,消失,随即显示另一个不喜欢按钮
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 喜欢还是不喜欢
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Left = Screen.PrimaryScreen.Bounds.Width / 2 - this.Width / 2;
this.Top = Screen.PrimaryScreen.Bounds.Height / 2 - this.Height / 2;
label1.Font = new Font("黑体-繁",30);
label1.ForeColor = Color.Red;
label1.BackColor = Color.Transparent;
label1.Left = this.Width/2 - label1.Width/2;
button2.Visible = false;
}
private void button3_MouseEnter(object sender, EventArgs e)
{
button3.Visible = false;
button2.Visible = true;
}
private void button2_MouseEnter(object sender, EventArgs e)
{
button2.Visible = false;
button3.Visible = true;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("其实,我也喜欢你!");
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
MessageBox.Show("不喜欢我,难受想哭");
}
private void Form1_SizeChanged(object sender, EventArgs e)
{
label1.Left = this.Width / 2 - label1.Width / 2;
}
}
}