/*
* (1)限制文本输入。该实例限制输入到TextBox控件中的内容只能是A~Z这26个大写英文字母,并且输入时可以使用Backspace修正,禁止在文本框里执行粘贴操作。
* (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 WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if ((e.KeyChar < 'A' || e.KeyChar > 'Z') & e.KeyChar != 8) //Backspace的ASCII为8
e.Handled = true; //按键无效
}
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.V & e.Control)
Clipboard.Clear();
}
private void Form1_Load(object sender, EventArgs e)
{
label1.Text = "烟台大学";
label1.AutoSize = true;
label1.Font = new Font("宋体", 48, FontStyle.Regular);
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font("楷体_GB2312", label1.Font.Size, label1.Font.Style);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font("黑体", label1.Font.Size, label1.Font.Style);
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox1.Checked ? label1.Font.Style | FontStyle.Italic : label1.Font.Style ^ (FontStyle.Italic));
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox2.Checked ? label1.Font.Style | FontStyle.Underline : label1.Font.Style ^ (FontStyle.Underline));
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
运行结果:
/*
* 要求:
* (1)复选框中文字在左边;
* (2)最下部为水平滚动条,水平滚动条最大值为72,最小值为4;
* (3)单击任何复选框,标签上文字样式都发生变化;
* (4)单击任何单选按钮,标签上文字字体都发生变化;
* (5)拖动水平滚动条,标签上文字大小发生变化。
*/
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_Load(object sender, EventArgs e)
{
label1.Text = "这是一个标签控件";
checkBox1.Text = "斜体";
checkBox2.Text = "下划线";
checkBox3.Text = "粗体";
radioButton1.Text = "黑体";
radioButton2.Text = "楷体";
groupBox1.Text = "字体样式";
groupBox2.Text = "字体";
hScrollBar1.Minimum = 4;
hScrollBar1.Maximum = 72;
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox1.Checked ? label1.Font.Style | FontStyle.Italic : label1.Font.Style ^ (FontStyle.Italic));
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox2.Checked ? label1.Font.Style | FontStyle.Underline : label1.Font.Style ^ (FontStyle.Underline));
}
private void checkBox3_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font(label1.Font.Name, label1.Font.Size, checkBox3.Checked ? label1.Font.Style | FontStyle.Bold : label1.Font.Style ^ (FontStyle.Bold));
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font("黑体", label1.Font.Size, label1.Font.Style);
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
label1.Font = new Font("楷体_GB2312", label1.Font.Size, label1.Font.Style);
}
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
label1.Font = new Font(label1.Font.Name, hScrollBar1.Value, label1.Font.Style);
}
}
}
运行结果: