您的位置:首页 > 编程语言 > C#

C#—Windows应用基础

2016-04-29 16:43 561 查看

最基本的加法器

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!");
}
}
}


运行结果:

左边为设计原图,右边为运行后的图

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C#