C# 实验06—Windows应用编程(2)——2

以下三道实验题目最终效果,大家可运行“可执行文件”目录中各题。

7、设计如图6-7所示的窗体,当用户在ritchBox中单击鼠标右键时,弹出一个快捷菜单,单击某一城市,即可在富文本框ritchBox中显示该城市的旅游景点。

         代码如下:

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 text6
{
    public partial class Form7 : Form
    {
        public Form7()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Text = "快捷菜单的使用";
            
        }
        private void Form7_Load(object sender, EventArgs e)
        {
            
        }
        //ritchbox中的contextmenustrip 属性 要置为 contextmenustrip1
        private void richTextBox1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                this.contextMenuStrip1.Show(MousePosition);
            }
        }

        private void 北京ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "故宫,天坛";
        }

        private void 南京ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "中统府,夫子庙";
        }

        private void 西安ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "华山,大雁塔,华清池";
        }

        private void 青岛ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "栈桥,崂山";
        }

        private void 上海ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = "外滩,田子坊,东方明,珠金融中心";
        }
    }
}

 

8、设计如图6-8a所示的窗体。单击“开始游戏”按钮,随机给出一个[0,9]之间的整数。然后让你猜是什么数字。你可以随便猜一个数字,游戏会提示大小,从而缩小结果范围。经过几次猜测与提示后,最终猜中答案。

游戏设计思路:

(1)窗体打开时,文本框只读,即禁止在文本框标输入任何内容,且焦点在“开始游戏”按钮上。如图6-8a所示。

(2)点击“开始游戏”按钮,则①取消文本框只读;②但在文本框中只能输入0~9十种数字;③生成一个0~99的随机整数作为目标数,供游戏者猜。如图6-8b所示。

(3)游戏者在文本框标输入数字。若没猜中,给出大小提示,可以继续输入数字。如图6-8c所示。

(4)若猜中,也给出提示,清空文本框,且重新设置文本框为只读,如图6-8c所示。

(5)游戏者可以点击“结束”按钮结束程序,也可以点击“开始游戏”按钮开始新的一轮游戏,此时随机产生目标数字被重新生成。

 

 

图6-8b 文本框中输入数字

        

代码如下:

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 text6
{
    public partial class Form8 : Form
    {
        int num;
        string str;
        int b = 0;
        public Form8()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }
        private void Form8_Load(object sender, EventArgs e)
        {
            this.label1.Text = "输入0~99之间整数:";
            this.Text = "猜数字游戏";
            this.button1.Text = "开始游戏";
            this.button2.Text = "结束";
            this.textBox1.ReadOnly = true;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.ReadOnly = false;
            Random random = new Random();
            num = random.Next(0, 99);
        }
        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!(Char.IsNumber(e.KeyChar)))
            {
                e.Handled = true;
            }
            if (e.KeyChar == (char)13)
            {
                str = textBox1.Text;
                b = int.Parse(str);
                if (b > num)
                {
                    DialogResult d = MessageBox.Show("大啦!", "", MessageBoxButtons.OK);
                }
                else if (b < num)
                {
                    DialogResult d = MessageBox.Show("小啦!", "", MessageBoxButtons.OK);
                }
                else
                {
                    DialogResult d = MessageBox.Show("猜中啦!", "", MessageBoxButtons.OK);
                    this.textBox1.Text = null;
                    this.textBox1.ReadOnly = true;
                }
            }
        }
    }
}

9、本实验要求设计5个窗体。分别为:一个登录窗体(frmLogin)、一个MDI主窗体(frmMain)和三个子窗体(frm子窗体1、frm子窗体2、frmHelp)。

设计思路:

(1)先创建5个窗体,按上面提示修改每个窗体的名称。

  (2)按图6-9提示,修改Program.cs文件中Program类。

 

 

(3)登录窗体如图6-10所示。具体要求:标题为“登录窗体”、大小不可调;启动后自动位居屏幕中央;没有最小化、最大化、关闭按钮。

在登录窗体中,若输入用户名为“u1”,且密码为“123”(密码显示为*),则通过登录。销毁登录窗体,同时打开如图6-11所示的主窗体。

否则,给出错误提示,如“用户名不能空”、“用户名错误”、“密码错误”等提示,如图6-12所示。

 

(4)主窗体界面如图6-11所示。主窗体中包含菜单(menuStrip1)、工具条、状态栏三个控件。提示:需要设置主窗体的IsMdiContainer属性,使之成为MDI窗体。

 

 

 

(5)当用户单击“打开窗体1”菜单项,或者单击“打开窗体1”工具按钮后,即可打开子窗体1,且该子窗体为主窗体子窗体。如图6-13中黄色窗体所示。

(6)当用户单击“打开窗体2”菜单项,或者单击“打开窗体2”工具按钮后,即可打开子窗体2,且该子窗体为主窗体子窗体。如图6-13蓝色窗体所示。

 

(6)当用户单击“垂直排列”菜单项,蓝色窗体和黄色窗体排列效果如图6-14所示。

 

(7)当用户单击“帮助”菜单项,蓝色窗体和黄色窗体随即关闭,同时帮助窗体打开,如图6-15所示。

注意:打开帮助窗体时,会自动关闭其他子窗体。此功能就是要求每次只打开一个子窗体。

 

(8)当用户单击“退出”菜单项,或者单击“结束程序”工具按钮后,弹出如图6-16所示的消息框。若单击“是”,则程序结束,若单击“否”,只是关闭消息框。

 

 (9)注意:状态条中显示登录时用户名、文件菜单中分隔线、“退出”菜单项的快捷键和图标、三个工具按钮的提示和图标。

看着最后一题这么长的描述,头秃。这确实是一道复杂题,题目描述就这么多了,直接上代码!

 1、program.cs代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace text6
{
    static class Program
    {
        public static string name { set; get; }
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        public static string getUser { get; set; }
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            frmLogin Loginfrm = new frmLogin();
            Loginfrm.ShowDialog();
            if (Loginfrm.DialogResult == DialogResult.OK)
            {
                Application.Run(new frmMain());
            }
        }
    }
}

2、frmMain代码:

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

        private void frmMain_Load(object sender, EventArgs e)
        {
            this.Text = "主窗体";
            this.IsMdiContainer = true;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            MenuStrip menuStrip1 = new MenuStrip();
            toolStripMenuItem1.Text = "文件(&F)";
            toolStripMenuItem2.Text = "窗体操作(&O)";
            toolStripMenuItem3.Text = "帮助(&H)";
            toolStripMenuItem4.Text = "打开窗体1";
            toolStripMenuItem5.Text = "打开窗体2";
            toolStripMenuItem6.Text = "退出";
            toolStripMenuItem6.Image = Image.FromFile("key.jpg");
            toolStripMenuItem7.Text = "垂直排列";
            toolStripMenuItem8.Text = "水平排列";
            toolStripButton1.Image = Image.FromFile("p1.png");
            toolStripButton2.Image = Image.FromFile("p2.jpg");
            toolStripButton3.Image = Image.FromFile("Close.png");
            toolStripStatusLabel1.Text = Program.name;
            toolStripButton1.Text = "打开窗体1";
            toolStripButton2.Text = "打开窗体2";
            toolStripButton3.Text = "结束程序";
            this.KeyPreview = true;
        }

        private void toolStripMenuItem4_Click(object sender, EventArgs e)
        {
            frm子窗体1 frm1 = new frm子窗体1();
            frm1.MdiParent = this;
            frm1.Show();
        }

        private void toolStripMenuItem5_Click(object sender, EventArgs e)
        {
            frm子窗体2 frm2 = new frm子窗体2();
            frm2.MdiParent = this;
            frm2.Show();
        }

        private void toolStripMenuItem6_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("确认退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }

        private void toolStripMenuItem7_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileVertical);
        }

        private void toolStripMenuItem8_Click(object sender, EventArgs e)
        {
            this.LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void toolStripMenuItem3_Click(object sender, EventArgs e)
        {
            frmHelp frmhelp = new frmHelp();
            frmhelp.MdiParent = this;
            frmhelp.WindowState = FormWindowState.Maximized;
            frmhelp.Show();
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            frm子窗体1 frm1 = new frm子窗体1();
            frm1.MdiParent = this;
            //frm1.StartPosition = FormStartPosition.CenterScreen;
            frm1.Show();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            frm子窗体2 frm2 = new frm子窗体2();
            frm2.MdiParent = this;
            //frm2.StartPosition = FormStartPosition.CenterScreen;
            frm2.Show();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            //DialogResult dialogResult = MessageBox.Show("确认退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
            if (MessageBox.Show("确认退出吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
            {
                Application.Exit();
            }
        }
    }
}

3、frmLogin代码:

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 text6
{
    public partial class frmLogin : Form
    {
        
        public frmLogin()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
            this.MinimizeBox = false;
            this.MaximizeBox = false;
            this.ControlBox = false;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
        }

        private void frmLogin_Load(object sender, EventArgs e)
        {
            this.Text = "登录窗体";
            this.label1.Text = "用户名:";
            this.label2.Text = "密 码:";
            this.button1.Text = "确定";
            this.button2.Text = "取消";
            this.textBox2.PasswordChar = '*';
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //判断是否为空
            //if (string.IsNullOrEmpty(textBox1.Text.Trim())){
            //    DialogResult dt = MessageBox.Show("用户名不能为空!", "", MessageBoxButtons.OK);
            //}
            if (textBox1.TextLength == 0)
            {
                DialogResult dt=MessageBox.Show("用户名不能为空!", "", MessageBoxButtons.OK);
            }
            else if (textBox1.TextLength != 0 && textBox1.Text != "u1")
            {
                DialogResult dt = MessageBox.Show("用户名错误!", "", MessageBoxButtons.OK);
            }
            else if (textBox1.TextLength != 0 && textBox1.Text == "u1"&&textBox2.TextLength == 0)
            {
                DialogResult dt = MessageBox.Show("密码不能为空!", "", MessageBoxButtons.OK);
            }
            else if (textBox1.TextLength != 0 && textBox1.Text == "u1"&&textBox2.TextLength != 0 && textBox2.Text != "123")
            {
                DialogResult dt = MessageBox.Show("密码错误!", "", MessageBoxButtons.OK);
            }
            else
            {
                this.DialogResult = DialogResult.OK;
            }
            Program.name = textBox1.Text;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
    }
}

4、frmHelp代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace text6
{
    public partial class frmHelp : Form
    {
        public frmHelp()
        {
            InitializeComponent();
        }

        private void frmHelp_Load(object sender, EventArgs e)
        {
            this.Text = "帮助窗体";
            button1.Text = "关闭窗体";
            richTextBox1.Multiline = true;
            richTextBox1.ScrollBars = RichTextBoxScrollBars.Vertical;
            string str = Application.StartupPath + @"/123.rtf";
            richTextBox1.LoadFile(str, RichTextBoxStreamType.RichText);
        }

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

5、frm子窗体1代码:

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 text6
{
    public partial class frm子窗体1 : Form
    {
        public frm子窗体1()
        {
            InitializeComponent();
        }

        private void frm子窗体1_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.Yellow;
            //this.Location = new Point(100, 100);
            this.Text = "frm子窗体1";
        }
    }
}

6、frm子窗体2代码:

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 text6
{
    public partial class frm子窗体2 : Form
    {
        public frm子窗体2()
        {
            InitializeComponent();
        }

        private void frm子窗体2_Load(object sender, EventArgs e)
        {
            this.BackColor = Color.Blue;
            //this.Location = new Point(100, 100);
            this.Text = "frm子窗体2";
        }
    }
}

 

C# 实验06—Windows应用编程(2)的实验总结完了,终于收工啦!希望总结的对你有所帮助!<微笑>

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值