C#—Windows应用基础
来源:互联网 发布:ecmall微信商城源码 编辑:程序博客网 时间:2024/06/05 00:35
最基本的加法器
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 WindowsFormsApplication2{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0) { MessageBox.Show("请输入数!"); return; } try { double x = double.Parse(textBox1.Text); double y = double.Parse(textBox2.Text); textBox3.Text = (x + y).ToString(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } }}
运行结果:
鼠标事件
/* *窗体上有一个图片框pictureBox1和一个命令按钮button1. *(1)当在窗体上右击鼠标时,弹出“你好!”的信息; *(2)当在button1上右击,显示“Hello!”的信息; *(3)当鼠标指向图片框时,图片框显示一幅图片。当鼠标离开图片框时图片框内的图片消失。 */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 WindowsFormsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_MouseClick(object sender, MouseEventArgs e) //点击鼠标时发生 { if (e.Button == MouseButtons.Right) MessageBox.Show("您好!"); } private void button1_MouseDown(object sender, MouseEventArgs e) //鼠标位于控件上并按下鼠标键时发生 { if (e.Button == MouseButtons.Right) MessageBox.Show("Hello!"); } private void pictureBox1_MouseEnter(object sender, EventArgs e) //鼠标进入控件的可见部分时发生 { //使图片适应窗体 pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage; pictureBox1.Image = Image.FromFile(Application.StartupPath + @"\g.png"); } private void pictureBox1_MouseLeave(object sender, EventArgs e) //鼠标离开控件的可见部分时发生 { pictureBox1.Image = null; } }}
运行结果:
当右击按钮时
当鼠标移动到图片区域时
键盘事件
KeyPress:用户按下一个ASCII键时触发;
KeyDown:用户按下键盘上任意键时被触发;
KeyUp:用户释放键盘上任意键时被触发。
KeyPressEventArgs参数值
属性:
KeyChar:返回用户按下的键所对应的字符
Handled:获取或设置一个值,该值指示是否处理过KeyPress事件
/* * 键盘事件应用。 * (1)捕获是否按下F1键; * (2)捕获是否同时按下Ctrl+C组合键; * (3)窗体上的文本框只接受0~9的数字,在文本框中不允许进行粘贴操作。*/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 WindowsFormsApplication2{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.F1) //只有Alt、Ctrl和Shift三个功能键表示成e.Alt、e.Shift和e.Control,其他功能键及其他ASCII码键均表示成Keys的一系列枚举值 { //如:Keys.C MessageBox.Show("你按下了F1键"); } if (e.Control && e.KeyCode == Keys.C) MessageBox.Show("你同时按下了Ctrl+C键"); } private void Form1_Load(object sender, EventArgs e) { this.KeyPreview = true; //窗体优先接收键盘事件 } private void textBox1_KeyPress(object sender, KeyPressEventArgs e) { if (e.KeyChar < '0' || e.KeyChar > '9') e.Handled = true; //将其设为true,以取消KeyPress事件//只要运行到此处,就退出,按键无效 } private void textBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) Clipboard.Clear(); //如果按下了Ctrl+C组合键,则清空粘贴板 } }}
运行结果:
按下F1键时
按下Ctrl+C组合键时
动态添加一个按钮
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 WindowsFormsApplication3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { Button bt = new Button(); bt.Text = "动态添加按钮"; //设置控件的标题 bt.Location = new Point(100, 100); //设置控件的位置 bt.Size = new Size(100, 50); //设置控件的大小 this.Controls.Add(bt); //把控件添加到窗体中 bt.Click+=new EventHandler(bt_Click); //使用委托,添加事件 } void bt_Click(object sender, EventArgs e) //编写事件方法 { MessageBox.Show("Click me!"); } }}
运行结果:
左边为设计原图,右边为运行后的图
0 0
- C#—Windows应用基础
- C#—Windows应用基础2
- C#—Windows应用基础3
- C#Windows应用—加法计数器
- C#TreeView基础应用
- C语言基础应用
- C语言基础—函数的理解及应用
- Windows编程基础一:Windows应用的基本概念
- windows应用程序设计 C语言提高篇
- C++Builder 生成64位Windows应用
- C/C++语言Windows编程基础 - 概述
- C/C++语言Windows编程基础 - 概述
- C基础:队列性质应用广度搜索
- C基础:堆栈性质应用深度搜索
- C语言基础 break continue语句应用
- Windows Phone 8移动应用开发工具盘点——基础篇
- C指针原理(96)-C基础综合应用
- Windows SDK编程(Delphi版) 之 应用基础,楔子
- 奇异值分解(SVD)原理详解及推导 (转)
- 远程控制的完善—遇到问题与解决方案
- 1006
- webkit移动开发笔记:html5中video与audio标签之无法自动播放的audio元素
- 搜索算法10之1017
- C#—Windows应用基础
- android+html5 上传图片兼容4.0 5.0 6.0
- MYSQL 的SQL 语句用法大全
- DC-DC变换器
- 顺序表的基本操作
- window下eclipse配制hadoop插件 (四)
- 李白打酒
- Opera安装详解
- Java抽象类与接口的区别