山东大学C#实验: 实验4 Windows应用程序开发基础

实验4-1

创建窗体与菜单练习

新建一个Windows应用程序,在工具箱里拖曳MenuStrip菜单组件,添加到当前窗口进行编辑,form2为新的窗体,以编程方式实现上述菜单结构,设计页面如下:

Form1代码如下:

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

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 保存ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 退出ToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void 编辑ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 帮助ToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void 打开form2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Form2 f2 = new Form2();
            f2.ShowDialog();
        }
    }
}

Form2代码:

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

        private void Form2_Load(object sender, EventArgs e)
        {
            MenuStrip menu = new MenuStrip();
            ToolStripMenuItem item1 = new ToolStripMenuItem("文件");
            ToolStripMenuItem item2 = new ToolStripMenuItem("编辑");
            ToolStripMenuItem item3 = new ToolStripMenuItem("帮助");
            menu.Items.AddRange(new ToolStripItem[] { item1, item2, item3 });
            ToolStripMenuItem item4 = new ToolStripMenuItem("打开");
            ToolStripMenuItem item5 = new ToolStripMenuItem("保存");
            ToolStripMenuItem item6 = new ToolStripMenuItem("退出");
            item1.DropDownItems.AddRange(new ToolStripItem[] { item4, item5, item6 });
            this.Controls.Add(menu);
        }
    }
}

实验结果:

Form1中点击退出,关闭程序

点击打开form2

实验4-2

命令按钮、单选按钮和复选框等窗体控件练习

A.实验代码:

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton1.Checked == true)
                this.BackColor = Color.Red;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton2.Checked == true)
                this.BackColor = Color.Green;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton3.Checked == true)
                this.BackColor = Color.Blue;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            this.textBox1.Text = textBox1.Text + checkBox1.Text + "、";
        }

        private void checkBox2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox2.Checked == true)
                this.textBox1.Text = textBox1.Text + checkBox2.Text + "、";
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox3.Checked == true)
                this.textBox1.Text = textBox1.Text + checkBox3.Text + "、";
        }

        private void checkBox4_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox4.Checked == true)
                this.textBox1.Text = textBox1.Text + checkBox4.Text + "、";
        }

        private void checkBox5_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox5.Checked == true)
                this.textBox1.Text = textBox1.Text + checkBox5.Text + "、";
        }

        private void checkBox6_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox6.Checked == true)
                this.textBox1.Text = textBox1.Text + checkBox6.Text + "、";
        }
    }
}

实验结果:

B.只留下一个GroupBox,添加确定按钮,在按下确定按钮之后,显示你喜欢的颜色

设计页面:

实验代码:

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

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox2_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox3_Enter(object sender, EventArgs e)
        {

        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton1.Checked == true)
                this.BackColor = Color.Red;
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton2.Checked == true)
                this.BackColor = Color.Green;
        }

        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            if (this.radioButton3.Checked == true)
                this.BackColor = Color.Blue;
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


        private void button2_Click(object sender, EventArgs e)
        {
            if (this.checkBox1.Checked == true)
            { this.textBox1.Text = textBox1.Text + checkBox1.Text + "、"; }
            if (this.checkBox2.Checked == true)
            { this.textBox1.Text = textBox1.Text + checkBox2.Text + "、"; }
            if (this.checkBox3.Checked == true)
            { this.textBox1.Text = textBox1.Text + checkBox3.Text + "、"; }
            if (this.checkBox4.Checked == true)
            { this.textBox1.Text = textBox1.Text + checkBox4.Text + "、"; }
            if (this.checkBox5.Checked == true)
            { this.textBox1.Text = textBox1.Text + checkBox5.Text + "、"; }
            if (this.checkBox6.Checked == true)
            { this.textBox1.Text = textBox1.Text + checkBox6.Text + "、"; }
        }
    }
}

实验结果:

点击确定按钮显示喜欢的颜色

实验4-3

标签控件,文本框控件,列表框控件和组合框控件等窗体控件练习

新建窗体应用,添加四个label:书名,出版社,单价,光盘

单价的comboBox的属性DropDownStyle选为DropDownList,控制其选择有或无。

给出版社的comboBox加几条选择,属性DropDownStyle选为DropDown,用户可以自己填写出版社,也可以下拉选择

实验代码:

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

        private void comboBox_Publishing_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void button_Add_Click(object sender, EventArgs e)
        {
            listBox1.Items.Add(" 书名:" + textBox_Bookname.Text + ", 出版社:" + comboBox_Publishing.Text + ", 单价:" + textBox_Price.Text + ", " + comboBox_Disk.Text + "光盘");
            textBox_Bookname.Text = "";
            textBox_Price.Text = "";
        }

        private void button_Remove_Click(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex != -1)
            {
                listBox1.Items.Remove(this.listBox1.SelectedItem);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox_Disk.Items.Add("有");
            this.comboBox_Disk.Items.Add("无");
            comboBox_Publishing.Items.Add("山东大学出版社");
            comboBox_Publishing.Items.Add("清华大学出版社");
            comboBox_Publishing.Items.Add("电子科技出版社");
            comboBox_Publishing.Items.Add("中华出版社");
            comboBox_Publishing.Items.Add("机械工业出版社");

        }
    }
}

实验结果:

© 版权声明
THE END
喜欢就支持以下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容