第八周作业

28 篇文章 1 订阅

1、理解窗体的文件含义及组织结构(如:Form1.cs、Form1.Designer.cs、Form1.resx),控件的属性、方法和事件。

2、完全用代码的方式在Form1.cs文件中创建一个文本标签对象Label1,用代码设置Label1的Parent、Location、Name、Text、AutoSize等属性,运行程序时显示在Form1窗体中(此例子在设计期的设计窗体中是看不到Label1标签的,想一想,Why?)。

//Form1.cs文件

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 Demo_HelloWorld_ByCode
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            Label Label1 = new Label();
            Label1.Parent = this;
            Label1.Name = "Label1";
            Label1.Text = "Hello,World!";
            Label1.Location = new Point(20, 30);
            Label1.AutoSize = true;

            InitializeComponent();
        }
    }
}


3、改进上例程序,依旧用代码的方式创建Label1文本标签,并让Label1能在设计期的设计窗体中显示,既把创建标签对象的代码移植到Form1.Designer.cs文件中。

//Form1.Designer.cs文件

namespace Demo_HelloWorld_ByCode_In_Designer
{
    partial class Form1
    {
        /// <summary>
        /// 必需的设计器变量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的资源。
        /// </summary>
        /// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗体设计器生成的代码

        /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Label1 = new System.Windows.Forms.Label();
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // Label1
            // 
            this.Label1.AutoSize = true;
            this.Label1.Location = new System.Drawing.Point(12, 25);//左上角的位置坐标Point(X,Y);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(77, 12);//对象的大小Size(Width,Height);
            this.Label1.TabIndex = 0;
            this.Label1.Text = "Hello,World!";
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(149, 161);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(252, 23);
            this.button1.TabIndex = 1;
            this.button1.Text = "设置HelloWorld所在的标签居中显示";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(580, 262);
            this.Controls.Add(this.button1);
            this.Controls.Add(this.Label1);
            this.Name = "Form1";
            this.Text = "用手写代码的方式创建一个显示Hello,World的文本标签,设计窗口中可见";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label Label1;
        private System.Windows.Forms.Button button1;
    }
}


4、改进上例程序,增加一个按钮计算并实现Label1标签在Form1窗口中水平方向居中显示。

        private void button1_Click(object sender, EventArgs e)
        {
            int ww = this.Width;//窗口的宽度
            int w1 = Label1.Width;//标签Label1的宽度
            int x = (ww - w1) / 2;//计算Label1.X的值
            Label1.Location = new Point(x,Label1.Location.Y);
        }

5、设计一个WinForm应用程序,演示如何用代码设置控件的前景与背景颜色。

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 Demo_Color
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.BackColor = System.Drawing.Color.LawnGreen;
            //label1.BackColor = System.Drawing.Color.FromArgb(124, 252, 0);//与上一条命令等价,效果是一样的。
        }

        private void button2_Click(object sender, EventArgs e)
        {
            label1.ForeColor = Color.FromArgb(255,0,0);
            //label1.ForeColor = System.Drawing.Color.Red;//与上一条命令等价,效果是一样的。
        }
    }
}

6、设计一个WinForm应用程序,演示Checxbox控件的用法,以及如何使用用代码设置控件的字体属性。

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 Demo_Font
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string fontName = textBox2.Text;
            float fontSize = Convert.ToSingle(textBox1.Text);
            //label1.Font = new Font(textBox2.Text, fontSize);//此语句不会保留字体的Style属性
            label1.Font = new Font(textBox2.Text, fontSize,label1.Font.Style);//保留label1.Font.Style属性
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            /*
             * ================方法一 BEGIN =======================*
            FontStyle fs = (FontStyle.Bold | FontStyle.Italic | FontStyle.Underline);//初始化时加上三种效果

            if (!checkBox1.Checked)
                fs -= FontStyle.Bold;//去除加粗效果
            if (!checkBox2.Checked)
                fs -= FontStyle.Italic;//去除倾斜效果
            if (!checkBox3.Checked)
                fs -= FontStyle.Underline;//去除下划线效果
            label1.Font = new Font(label1.Font, fs);
             * 
             *=================方法一 END =========================* 
            */


            /*
             * ================方法二 BEGIN =======================*
            */
            if (checkBox1.Checked)
                label1.Font = new Font(label1.Font,label1.Font.Style | FontStyle.Bold);//加粗效果
            else
                label1.Font = new Font(label1.Font, label1.Font.Style & ~FontStyle.Bold);//取消加粗

            if (checkBox2.Checked)
                label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Italic);//倾斜效果
            else
                label1.Font = new Font(label1.Font, label1.Font.Style & ~FontStyle.Italic);//取消倾斜

            if (checkBox3.Checked)
                label1.Font = new Font(label1.Font, label1.Font.Style | FontStyle.Underline);//下划线效果
            else
                label1.Font = new Font(label1.Font, label1.Font.Style & ~FontStyle.Underline);//取消下划线
            /*
             *=================方法二 END =========================* 
            */
        }
    }
}

7、演示DEMO源代码在github上的仓库地址:

https://github.com/xieyunc/csharp_teach.git

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值