案例集锦(简单的计算器,简单的闹钟,告白神器,简单的对话框)

本文介绍了使用C#创建对话框和计算器的详细步骤,包括如何通过按钮触发消息发送到消息框,以及如何利用下拉菜单选择不同的数学运算,并在文本框中显示结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对话框案例

两个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;
        }
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值