Windows应用编程(1)_7

本文介绍了一个使用C#和Windows Forms设计的窗体实例,实现了复选框、单选按钮控制标签字体样式,以及水平滚动条调整字体大小的功能。通过具体代码展示了如何响应窗体加载、复选框状态改变、单选按钮选择和滚动条滚动等事件。

题目要求:

设计如图所示的窗体,实现下面要求的效果。

(1)复选框中文字在左边;

(2)最下部为水平滚动条。水平滚动条最小值为4、最大值为72,且在窗体Load事件中通过代码设置;

(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.Threading.Tasks;
using System.Windows.Forms;

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

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Text = "单选和复选框实验";
            this.label1.Text = "烟台大学";
            this.groupBox1.Text = "字体样式";
            this.groupBox2.Text = "字体";
            this.checkBox1.Text = "斜体";
            this.checkBox2.Text = "下划线";
            this.checkBox3.Text = "粗体";
            this.radioButton1.Text = "黑体";
            this.radioButton2.Text = "楷体";
            this.hScrollBar1.Minimum = 4;
            this.hScrollBar1.Maximum = 72;

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                label1.Font = new Font(label1.Text, label1.Font.Size, label1.Font.Style | FontStyle.Italic);
            }
            else
            {
                label1.Font = new Font(label1.Text, label1.Font.Size, label1.Font.Style & ~FontStyle.Italic);
            }
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox2.Checked)
            {
                label1.Font = new Font(label1.Text, label1.Font.Size, label1.Font.Style | FontStyle.Underline);
            }
            else
            {
                label1.Font = new Font(label1.Text, label1.Font.Size, label1.Font.Style & ~FontStyle.Underline);
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                label1.Font = new Font(label1.Text, label1.Font.Size, label1.Font.Style | FontStyle.Bold);
            }
            else
            {
                label1.Font = new Font(label1.Text, label1.Font.Size, label1.Font.Style & ~FontStyle.Bold);
            }
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton1.Checked)
            {
                label1.Font = new Font("黑体", label1.Font.Size,label1.Font.Style);
            }
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton2.Checked)
            {
                label1.Font = new Font("楷体", 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);
        }

    }
}

自动生成的Designer文件:

namespace WindowsFormsApplication5_7
{
    partial class Form1
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.checkBox3 = new System.Windows.Forms.CheckBox();
            this.checkBox2 = new System.Windows.Forms.CheckBox();
            this.checkBox1 = new System.Windows.Forms.CheckBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.radioButton2 = new System.Windows.Forms.RadioButton();
            this.radioButton1 = new System.Windows.Forms.RadioButton();
            this.hScrollBar1 = new System.Windows.Forms.HScrollBar();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(43, 28);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(62, 18);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // groupBox1
            // 
            this.groupBox1.Controls.Add(this.checkBox3);
            this.groupBox1.Controls.Add(this.checkBox2);
            this.groupBox1.Controls.Add(this.checkBox1);
            this.groupBox1.Location = new System.Drawing.Point(40, 162);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(384, 173);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "groupBox1";
            // 
            // checkBox3
            // 
            this.checkBox3.AutoSize = true;
            this.checkBox3.Location = new System.Drawing.Point(278, 76);
            this.checkBox3.Name = "checkBox3";
            this.checkBox3.Size = new System.Drawing.Size(115, 22);
            this.checkBox3.TabIndex = 2;
            this.checkBox3.Text = "checkBox3";
            this.checkBox3.UseVisualStyleBackColor = true;
            this.checkBox3.CheckedChanged += new System.EventHandler(this.checkBox3_CheckedChanged);
            // 
            // checkBox2
            // 
            this.checkBox2.AutoSize = true;
            this.checkBox2.Location = new System.Drawing.Point(146, 76);
            this.checkBox2.Name = "checkBox2";
            this.checkBox2.Size = new System.Drawing.Size(115, 22);
            this.checkBox2.TabIndex = 1;
            this.checkBox2.Text = "checkBox2";
            this.checkBox2.UseVisualStyleBackColor = true;
            this.checkBox2.CheckedChanged += new System.EventHandler(this.checkBox2_CheckedChanged);
            // 
            // checkBox1
            // 
            this.checkBox1.AutoSize = true;
            this.checkBox1.Location = new System.Drawing.Point(6, 76);
            this.checkBox1.Name = "checkBox1";
            this.checkBox1.Size = new System.Drawing.Size(115, 22);
            this.checkBox1.TabIndex = 0;
            this.checkBox1.Text = "checkBox1";
            this.checkBox1.UseVisualStyleBackColor = true;
            this.checkBox1.CheckedChanged += new System.EventHandler(this.checkBox1_CheckedChanged);
            // 
            // groupBox2
            // 
            this.groupBox2.Controls.Add(this.radioButton2);
            this.groupBox2.Controls.Add(this.radioButton1);
            this.groupBox2.Location = new System.Drawing.Point(508, 162);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(322, 173);
            this.groupBox2.TabIndex = 2;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "groupBox2";
            // 
            // radioButton2
            // 
            this.radioButton2.AutoSize = true;
            this.radioButton2.Location = new System.Drawing.Point(184, 76);
            this.radioButton2.Name = "radioButton2";
            this.radioButton2.Size = new System.Drawing.Size(141, 22);
            this.radioButton2.TabIndex = 1;
            this.radioButton2.TabStop = true;
            this.radioButton2.Text = "radioButton2";
            this.radioButton2.UseVisualStyleBackColor = true;
            this.radioButton2.CheckedChanged += new System.EventHandler(this.radioButton2_CheckedChanged);
            // 
            // radioButton1
            // 
            this.radioButton1.AutoSize = true;
            this.radioButton1.Location = new System.Drawing.Point(37, 76);
            this.radioButton1.Name = "radioButton1";
            this.radioButton1.Size = new System.Drawing.Size(141, 22);
            this.radioButton1.TabIndex = 0;
            this.radioButton1.TabStop = true;
            this.radioButton1.Text = "radioButton1";
            this.radioButton1.UseVisualStyleBackColor = true;
            this.radioButton1.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
            // 
            // hScrollBar1
            // 
            this.hScrollBar1.Location = new System.Drawing.Point(40, 363);
            this.hScrollBar1.Name = "hScrollBar1";
            this.hScrollBar1.Size = new System.Drawing.Size(790, 40);
            this.hScrollBar1.TabIndex = 3;
            this.hScrollBar1.Scroll += new System.Windows.Forms.ScrollEventHandler(this.hScrollBar1_Scroll);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(878, 427);
            this.Controls.Add(this.hScrollBar1);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox2.PerformLayout();
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.CheckBox checkBox3;
        private System.Windows.Forms.CheckBox checkBox2;
        private System.Windows.Forms.CheckBox checkBox1;
        private System.Windows.Forms.GroupBox groupBox2;
        private System.Windows.Forms.RadioButton radioButton2;
        private System.Windows.Forms.RadioButton radioButton1;
        private System.Windows.Forms.HScrollBar hScrollBar1;
    }
}

 

using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Data.OleDb; namespace DormMIS { /// <summary> /// Form1 的摘要说明。 /// </summary> public class Login : System.Windows.Forms.Form { private System.Windows.Forms.Button btClose;//取消按钮 private System.Windows.Forms.Button btAdd; private System.Windows.Forms.TextBox password; private System.Windows.Forms.TextBox name; private System.Windows.Forms.Label label3; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label1; /// <summary> /// 必需的设计器变量。 /// </summary> private System.ComponentModel.Container components = null; private OleDbConnection oleConnection1 = null; public Login() { // // Windows 窗体设计器支持所必需的 // InitializeComponent(); this.oleConnection1=new OleDbConnection(DormMIS.database.dbConnection.connection); // // TODO: 在 InitializeComponent 调用后添加任何构造函数代码 // } /// <summary> /// 清理所有正在使用的资源。 /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows 窗体设计器生成的代码 /// <summary> /// 设计器支持所需的方法 - 不要使用代码编辑器修改 /// 此方法的内容。 /// </summary> private void InitializeComponent() { System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Login)); this.btClose = new System.Windows.Forms.Button(); this.btAdd = new System.Windows.Forms.Button(); this.password = new System.Windows.Forms.TextBox(); this.name = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.label1 = new System.Windows.Forms.Label(); this.SuspendLayout(); // // btClose // this.btClose.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btClose.ForeColor = System.Drawing.Color.Black; this.btClose.Location = new System.Drawing.Point(168, 156); this.btClose.Name = "btClose"; this.btClose.Size = new System.Drawing.Size(75, 23); this.btClose.TabIndex = 20; this.btClose.Text = "取消"; this.btClose.Click += new System.EventHandler(this.btClose_Click); // // btAdd // this.btAdd.FlatStyle = System.Windows.Forms.FlatStyle.Popup; this.btAdd.ForeColor = System.Drawing.Color.Black; this.btAdd.Location = new System.Drawing.Point(48, 156); this.btAdd.Name = "btAdd"; this.btAdd.Size = new System.Drawing.Size(75, 23); this.btAdd.TabIndex = 19; this.btAdd.Text = "确定"; this.btAdd.Click += new System.EventHandler(this.btAdd_Click); // // password // this.password.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.password.Location = new System.Drawing.Point(128, 116); this.password.Name = "password"; this.password.PasswordChar = '*'; this.password.Size = new System.Drawing.Size(100, 21); this.password.TabIndex = 18; this.password.Text = "admin"; // // name // this.name.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; this.name.Location = new System.Drawing.Point(128, 76); this.name.Name = "name"; this.name.Size = new System.Drawing.Size(100, 21); this.name.TabIndex = 17; this.name.Text = "admin"; this.name.TextChanged += new System.EventHandler(this.name_TextChanged); // // label3 // this.label3.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label3.ForeColor = System.Drawing.Color.Black; this.label3.Location = new System.Drawing.Point(64, 116); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(56, 23); this.label3.TabIndex = 16; this.label3.Text = "密 码"; // // label2 // this.label2.Font = new System.Drawing.Font("宋体", 10.5F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label2.ForeColor = System.Drawing.Color.Black; this.label2.Location = new System.Drawing.Point(64, 76); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(56, 23); this.label2.TabIndex = 15; this.label2.Text = "用户名"; // // label1 // this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 18F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))); this.label1.ForeColor = System.Drawing.Color.Black; this.label1.Location = new System.Drawing.Point(40, 24); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(228, 28); this.label1.TabIndex = 14; this.label1.Text = "宿舍管理信息系统"; // // Login // this.AutoScaleBaseSize = new System.Drawing.Size(6, 14); this.BackColor = System.Drawing.Color.Ivory; this.ClientSize = new System.Drawing.Size(304, 210); this.Controls.Add(this.btClose); this.Controls.Add(this.btAdd); this.Controls.Add(this.password); this.Controls.Add(this.name); this.Controls.Add(this.label3); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "Login"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "登录"; this.Load += new System.EventHandler(this.Login_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion /// <summary> /// 应用程序的主入口点。 /// </summary> [STAThread] static void Main() { Application.Run(new Login()); } private void btAdd_Click(object sender, System.EventArgs e) { if(name.Text.Trim()==""||password.Text.Trim()=="") MessageBox.Show("请输入用户名和密码","提示"); else { oleConnection1.Open(); OleDbCommand cmd=new OleDbCommand("",oleConnection1); string sql="select * from userinfo where UName='"+name.Text.Trim()+"' and PWD='"+password.Text.Trim()+"'"; cmd.CommandText=sql; if (null!=cmd.ExecuteScalar()) { //隐藏登录窗口 this.Visible=false; //创建并打开主界面 Main main=new Main(); main.Tag=this.FindForm(); OleDbDataReader dr; cmd.CommandText=sql; dr=cmd.ExecuteReader(); dr.Read(); main.statusBarPanel2.Text=name.Text.Trim(); main.ShowDialog(); } else MessageBox.Show("用户名或密码错误","警告"); } oleConnection1.Close(); } private void btClose_Click(object sender, System.EventArgs e) { this.Close(); } private void name_TextChanged(object sender, EventArgs e) { } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值