(2)单击“确定”按钮,可将textBox中的文字添加到label2中; (3)label2中的文字可字体大小可切换三次(小、中、大),初始状态下字体为“小”,则“缩小字体”按钮不可用,每次单击“增大字体”按钮,可使增大字体一号,当字体增大为“大”时,“增大字体”按钮不可用,每次单击“缩小字体”按钮,可使字体缩小一号;
(4)“变换字体颜色”按钮,可使label2中的字体颜色随机改变。 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 Test3 {
public partial class Form1 : Form {
public int Add = 0; //增大字体标志:0代表小,1代表中,2代表大 public Form1() {
this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 InitializeComponent(); }
//确定按钮
private void button1_Click(object sender, EventArgs e) {
if (textBox1.Text == \ {
MessageBox.Show(\请输入文字!\提示信息\MessageBoxIcon.Warning);
return; }
if (Add == 0) {
button3.Enabled = false; //设置缩小字体不可用 }
richTextBox1.Text = textBox1.Text; //显示文字 textBox1.Text = \清空文字
textBox1.Focus(); //定位输入焦点 }
//增大字体按钮
private void button2_Click(object sender, EventArgs e) {
richTextBox1.SelectAll(); //全选文字
//richTextBox1.Select(0, 0); //取消全选文字 if (richTextBox1.Text == \ {
MessageBox.Show(\当前文本框无文字\\提示信息\MessageBoxButtons.OK, MessageBoxIcon.Warning);
return; }
richTextBox1.SelectionFont = new Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size + 1, richTextBox1.SelectionFont.Style);
Add++; if (Add !=0) {
button3.Enabled = true; //设置缩小字体可用 }
if (Add == 2) {
button2.Enabled = false; //设置增大字体不可用 } }
//缩小字体按钮
private void button3_Click(object sender, EventArgs e) {
richTextBox1.SelectAll(); //全选文字 richTextBox1.SelectionFont = Font(richTextBox1.SelectionFont.FontFamily, richTextBox1.SelectionFont.Size richTextBox1.SelectionFont.Style);
Add--;
if (Add == 0) {
button3.Enabled = false; //设置缩小字体不可用 }
if (Add < 2) {
button2.Enabled = true; //设置增大字体可用 } }
//变换字体颜色按钮
private void button4_Click(object sender, EventArgs e) {
richTextBox1.SelectAll(); //全选文字 //richTextBox1.SelectionColor = Color.Red;
//设置字体颜色:GetRandomColor()方法是为了获取随机颜色 richTextBox1.SelectionColor = GetRandomColor();
new -1,
}
//加载窗口
private void Form1_Load(object sender, EventArgs e) { }
//获取随机颜色
public Color GetRandomColor() {
Random RandomNum_First = new Random((int)DateTime.Now.Ticks); // C#的随机数
System.Threading.Thread.Sleep(RandomNum_First.Next(50));
Random RandomNum_Sencond = new Random((int)DateTime.Now.Ticks); // 为了在白色背景上显示,尽量生成深色 int int_Red = RandomNum_First.Next(256);
int int_Green = RandomNum_Sencond.Next(256);
int int_Blue = (int_Red + int_Green > 400) ? 0 : 400 - int_Red - int_Green; int_Blue = (int_Blue > 255) ? 255 : int_Blue;
return Color.FromArgb(int_Red, int_Green, int_Blue); } } }
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 WindowsFormsApplication3 {
public partial class Form1 : Form {
public Form1() {
InitializeComponent(); }
private void label1_Click(object sender, EventArgs e) { }
private void button1_Click(object sender, EventArgs e) {
textBox2.Text = textBox1.Text; }
public inti = 1;
private void button2_Click(object sender, EventArgs e) {
if (i< 3) {
textBox2.Font = new Font(\宋体\i++;
} }
private void button3_Click(object sender, EventArgs e) {
if (i> 1) {
textBox2.Font = new Font(\宋体\i--;
} }
private void button4_Click(object sender, EventArgs e) {
textBox2.ForeColor = Color.Red; } } }
4.试编写Windows应用程序,完成下列要求:
(1)Form1窗体设计界面如下:
(2)窗体左侧为一个靠左停靠的panel,其中包含一个label控件; (3)初试状态时,“水平移动”选中,当用户单击“开始移动”按钮时,label在panel中水平从左向右移动,单击“暂停移动”按钮时,label停在原位置不动;
(4)在label移动过程中,若用户切换移动方式,则弹出对话框,提示先暂停移动;在label暂停移动时,用户切换移动方式,label在原位置以新的移动方式进行移动。
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 Test4 {
public partial class Form1 : Form {
int y; //标志位 public Form1() {
this.StartPosition = FormStartPosition.CenterScreen; //窗口居中显示 y = 1;
InitializeComponent(); }
//开始移动按钮
private void button1_Click(object sender, EventArgs e) {
timer1.Enabled = true; //设置定时器可用
timer1.Interval = 200; //设置定时器间隔为500毫秒
}
//暂停移动按钮
private void button2_Click(object sender, EventArgs e) {
timer1.Enabled = false; //设置定时为不可用 }
//加载窗口
private void Form1_Load(object sender, EventArgs e) {
radioButton1.Enabled = true; }
//定时器
private void timer1_Tick(object sender, EventArgs e) {
//水平移动
if (radioButton1.Checked) {
//注意:laber1.Right属性不可改变,只能读。所以改变laber1的位置只能用top和left属性
label1.Left = label1.Left + 10*y ; if (label1.Right>panel1.Size.Width) {