加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

C#窗体练习

(2011-10-15 19:15:50)
分类: c#

1.在窗体中显示一行字符,加入标题为“红色”和“黑色”的两个按钮,分别单击两个按钮,可以改变显示字符的颜色。字符为红色时,“红色”的按钮不能使用;  字符为黑色时,“黑色”的按钮不能使用。(提示:修改按钮的属性Enabled为false使其不能使用)
解:http://s13/middle/82f2fc284af54af8d33ec&690

红色按钮的Click事件处理代码:

    button2.Enabled = true;

    label1.ForeColor = Color.Red;

    button1.Enabled = false;

黑色按钮的Click事件处理代码:

   button1.Enabled = true;

   label1.ForeColor = Color.Black;

   button2.Enabled = false;

  

2.将上题改为用按扭修改字体的大小,按钮的标题分别为“大字”和“小字”(参见3.9节)。

解:将“红色”和“黑色”的两个按钮的Text属性分别改为“大字”和“小字”

大字按钮的Click事件处理代码:

      button2.Enabled = true;

      label1.Font = new Font(label1.Font.Name, label1.Font.Size+2);

      button1.Enabled = false;

 小字按钮的Click事件处理代码:

     button1.Enabled = true;

      label1.Font = new Font(label1.Font.Name, label1.Font.Size-2);

      button2.Enabled = false;

 http://s3/middle/82f2fc284af54df706492&690

3.加一文本框控件和一按钮,单击按钮将文本框控件输入的内容用标签控件显示。

解:加一按钮,Text属性分别改为“显示”,其Click事件处理代码如下:

 label1.Text = textBox1.Text;

 

http://s15/middle/82f2fc284af5507010fee&690


4.修改上题,使文本框控件输入内容和标签控件显示的内容同步。

解:在textBox1的TextChanged事件处理函数中增加代码:

label1.Text = textBox1.Text;

 

http://s15/middle/82f2fc284af552a84111e&690

5.增加文本框和按钮控件到窗体,单击按钮将文本框控件中选中内容的字符数和选中内容的第1个字符的索引号用标签控件显示。

解:

⑴textBox1的SelectionLength属性可以得到选中内容的字符数,

  SelectionStart属性可以得到选中内容的第1个字符的索引号。

⑵新增加按钮的Click事件处理代码如下:

label1.Text = textBox1.SelectionLength.ToString()+","+textBox1.SelectionStart.ToString();

 http://s11/middle/82f2fc284af573249a9aa&690

6.用控件RadioButton选择性别,把选择的结果用Label控件显示出来。

解:

⑴窗体上增加radioButton1和 radioButton2,它们的Text属性分别为“男”和“女”;

⑵在两个按钮的CheckedChanged事件处理函数中分别增加代码:

 label1.Text = radioButton1.Text;

 label1.Text = radioButton2.Text;

 http://s10/middle/82f2fc284af57575ce0b9&690


7.用控件ComboBox修改标签控件字体的大小。(提示:用属性Item在下拉列表中输入字号)

解:

⑴在控件ComboBox的属性Item中输入数字字符,例如1,2,3….20;

⑵在ComboBox的SelectedIndexChanged事件处理函数中分别增加代码:

label1.Font = new Font(label1.Font.Name, Convert.ToSingle(comboBox1.Text));

http://s13/middle/82f2fc284af58141017dc&690 

8.放置ListBox控件到窗体中,属性Name=listBoxl。列表框中有3项,分别为:苹果、梨子、香蕉。允许多选。标签控件同步显示ListBox控件所做的选择。
解:

⑴在控件listBoxl的SelectionMode设置为MultiSimple,属性Item中输入苹果、梨子、香蕉。

ps:multiSimple 允许选择多个的对象; MultiExtend 允许使用 Shift 和 Ctrl 键,进行多项选择。

⑵在 listBoxl的SelectedIndexChanged事件处理函数中分别增加代码:

label1.Text = "所选择的是:";

foreach( int i in listBox1.SelectedIndices )

 label1.Text += listBox1.Items[i].ToString() + " ,";

http://s1/middle/82f2fc284af58634b9690&690 


9.放置ListBox、TextBox和3个Button控件到窗体中,Button控件属性Text分别为:增加、删除、清空。单击标题为“增加”的按钮,把textBox中输入的内容作为一个条目增加到listBoxl列表中;单击标题为“删除”的按钮,删除listBoxl列表中所选择的条目;单击标题为“清空”按钮,清除listBoxl列表中所有条目。

解:

⑴“增加”按钮的事件处理函数代码:

listBox1.Items.Add(textBox1.Text);

⑵“删除”按钮的事件处理函数代码:

listBox1.Items.RemoveAt(listBox1.SelectedIndex);

⑶“清空”按钮的事件处理函数代码:

   listBox1.Items.Clear();

 http://s16/middle/82f2fc284af59de5275bf&690

学习拓展:

1.判断 ListBox 是否有被选中的Item,就是使用SelectedIndex来判断,如果未选中任何一个Item则SelectedIndex就是-1;否则为Item的索引(0、1、2...)

2.获得焦点事件的应用(textBox1.Enter)

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label1.Text = "所选择的是:";

            foreach (int i in listBox1.SelectedIndices)

                label1.Text += listBox1.Items[i].ToString();

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "") { label1.Text = "无输入内容"; return; }
            listBox1.Items.Add(textBox1.Text); label1.Text = "已输入:" + textBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1) { label1.Text = "无选定内容"; return; }
            listBox1.Items.RemoveAt(listBox1.SelectedIndex); label1.Text = "已删除一个条目";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear(); label1.Text = "已清空";
            textBox1.Text = "请输入";
        }

        private void textBox1_GotFocus(object sender, EventArgs e)
        {
            label1.Text = "输入等待";
        }
 //textBox获得焦点事件,注意事件的注册。

//this.textBox1.Enter += new System.EventHandler(this.textBox1_Enter);
        private void textBox1_Enter(object sender, EventArgs e)
        {
           textBox1.Text = "";//清空
        }

    }
}

10.在窗体中显示字符,每隔1s字体变大些,变到一定尺寸后,每隔1s字体变小些,如此循环。增加一个按钮,可以启动和停止字符串字体大小变化。

解:

⑴在 Form1类中增加数据成员private  bool big = true; big = true表示字体正在变大,否则正在变小;

⑵增加timer控件,将timer1的interval属性设置为1000,Enable=true;

⑶在timer1的Tick事件处理函数中增加代码:

float size=label1.Font.Size;

int increment = 1;   //字体变化的增量

if (big)

  if (size + increment < 25)

   label1.Font = new Font(label1.Font.Name, size + increment);

  else

   big = false;

if(!big)

  if (size-increment>=9)

    label1.Font = new Font(label1.Font.Name, size- increment);

  else

  big = true;

(4)Button的Click事件代码

timer1.Enabled = ! timer1.Enabled;

http://s15/middle/82f2fc284af614abb90ee&690
  

⒒在窗体中显示字符,每隔1s字符移动一定距离,先右移,移到右边界,再左移,移到左边界,又一次右移,如此循环。

解:

⑴在 Form1类中增加数据成员:private bool MoveToRight=true;表示移动方向,true为右移,否则左移。

⑵在timer1的Tick事件处理函数中增加代码:

int x=label1.Location.X;

int y=label1.Location.Y;

int increment=2;  

if (MoveToRight)

   if ((x + label1.Size.Width) + increment>= this.ClientRectangle.Width)

       MoveToRight = false;

   else

       label1.Location = new Point(x+increment, y);

if (!MoveToRight)

   if ( x-increment<=0)

       MoveToRight=true;

   else

     label1.Location = new Point(x-increment, y);  

 http://s1/middle/82f2fc284af61bc94ded0&690
上升下降
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private bool up = true;
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            int x = label1.Location.X;
            int y = label1.Location.Y;
            int increment = 1;
            if (!up)//下降
                if ((y + label1.Size.Height) + increment <= this.ClientRectangle.Height)
                    label1.Location = new Point(x, y + increment);
                else
                    up = true;
            if (up)//上升
                if (y - increment >= 0)
                    label1.Location = new Point(x, y - increment);
                else
                    up = false;

        }
    }
}

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有