C# .net窗体实战7:单选钮、复选钮的使用

界面:

功能:在上面的textbox中是由汉字的,然后点击下面的字体、字号还有字型,上面的字就能够根据要求变化。

代码:

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 E
{
    public partial class Form1 : Form
    {
        string myFontName; 
        float myFontSize;
        FontStyle myFontStyle; //字体样式 FontStyle是方法
        Font myFont;
        public Form1()
        {
            InitializeComponent();
            textBox1.Text="测绘系";
            // \r: return,意思是回车,模拟打字机动作,从头开始(不换新行)
            // \n: new line,意思是开始一个新行。
            SetupFont();
        }
        private void SetupFont()
        {
            if (rdoFontNameSong.Checked==true) myFontName = "宋体";
            if (rdoFontNameHua.Checked) myFontName = "华文琥珀";
            if (rdoFontNameKai.Checked) myFontName = "楷体";

            if (rdoFontSize5.Checked) myFontSize = 9;
            if (rdoFontSize3.Checked) myFontSize = 15.75f;
            if (rdoFontSize1.Checked) myFontSize = 26.25f;

            //复选框需考虑所有可能的组合
            if (chkFontBold.Checked && chkFontItalic.Checked && chkFontUnderline.Checked)
                myFontStyle = FontStyle.Bold | FontStyle.Italic | FontStyle.Underline; //按位或
            else if (chkFontBold.Checked && chkFontItalic.Checked)
                myFontStyle = FontStyle.Bold | FontStyle.Italic;
            else if (chkFontBold.Checked && chkFontUnderline.Checked)
                myFontStyle = FontStyle.Bold | FontStyle.Underline;
            else if (chkFontItalic.Checked && chkFontUnderline.Checked)
                myFontStyle = FontStyle.Italic | FontStyle.Underline;
            else if (chkFontBold.Checked)
                myFontStyle = FontStyle.Bold;
            else if (chkFontItalic.Checked)
                myFontStyle = FontStyle.Italic;
            else if (chkFontUnderline.Checked)
                myFontStyle = FontStyle.Underline;
            else
                myFontStyle = FontStyle.Regular;

            //根据选定的字体名、字体大小、字体样式,创建一种新字体
            myFont = new Font(myFontName, myFontSize, myFontStyle);
            textBox1.Font = myFont;
        }

        private void rdoFontNameSong_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void rdoFontNameHua_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void rdoFontNameKai_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void rdoFontSize5_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void rdoFontSize3_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void rdoFontSize1_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void chkFontBold_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void chkFontItalic_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }

        private void chkFontUnderline_CheckedChanged(object sender, EventArgs e)
        {
            SetupFont();
        }
    }
}

实现起来和理解起来都比较简单,因为重点就是点击不同的选择之后发生的变化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值