以c#形式组织的工作空间

时间:2011-06-06 08:00:37

标签: c# forms transition

这是我第一次使用C#表单,我不知道我在做什么是正确的。 下面是我在C#中的表单工作区,你可以看到那里有很多东西而且很乱...... enter image description here

我使用this.BackgroundImage = image;更改窗体的背景图像,我只需要在需要时打开每个控件的可见属性,这样当我运行应用程序时它看起来很不错(虽然我仍然没有完成它,但很难以一种形式工作乱) enter image description here

无论如何,我可以以一种有组织的方式工作,就像我可以使用很多表单而不是一个,只需将它们互连,就像在powerpoint中你可以有很多幻灯片,只需使用超链接指向其他幻灯片... 请帮忙... 这是我的代码(对于noob编码风格很抱歉)

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;
using System.Data.OleDb;
using Microsoft.VisualBasic;

namespace PProj1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            backfromaboutandhow.Visible = false;
            nextbutton.Visible = false;
            backfromreserve.Visible = false;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            finish.Visible = false;
            backtoreserve.Visible = false;
            N1.Visible = false;
            N2.Visible = false;
            N3.Visible = false;
            N4.Visible = false;
            N5.Visible = false;
            N6.Visible = false;
            N7.Visible = false;
            N8.Visible = false;
            S1.Visible = false;
            S2.Visible = false;
            S3.Visible = false;
            S4.Visible = false;
            S5.Visible = false;
            S6.Visible = false;
            S7.Visible = false;
            S8.Visible = false;
            E1.Visible = false;
            E2.Visible = false;
            E3.Visible = false;
            E4.Visible = false;
            W1.Visible = false;
            W2.Visible = false;
            W3.Visible = false;
            NW1.Visible = false;
            NW2.Visible = false;
            NE1.Visible = false;
            NE2.Visible = false;
            SW.Visible = false;
            SE.Visible = false;
        }

        OleDbConnection con;
        OleDbCommand cmd;
        OleDbDataAdapter adapter;
        DataSet ds;

        private void pictureBox1_MouseHover(object sender, EventArgs e)
        {
            aboutoldtrafford.Location = new Point(16, 9);
        }

        private void pictureBox1_MouseLeave(object sender, EventArgs e)
        {
            aboutoldtrafford.Location = new Point(9, 9);
        }

        private void pictureBox2_MouseHover(object sender, EventArgs e)
        {
            howtogetthere.Location = new Point(16, 62);
        }

        private void pictureBox2_MouseLeave(object sender, EventArgs e)
        {
            howtogetthere.Location = new Point(9, 62);
        }

        private void pictureBox3_MouseHover(object sender, EventArgs e)
        {
            reserveaticket.Location = new Point(16, 113);
        }

        private void pictureBox3_MouseLeave(object sender, EventArgs e)
        {
            reserveaticket.Location = new Point(9, 113);
        }

        private void pictureBox1_MouseClick(object sender, MouseEventArgs e)
        {
            Image image = Properties.Resources.about;
            this.BackgroundImage = image;
            aboutoldtrafford.Visible = false;
            howtogetthere.Visible = false;
            reserveaticket.Visible = false;
            backfromaboutandhow.Visible = true;
        }

        private void pictureBox4_MouseHover(object sender, EventArgs e)
        {
            backfromaboutandhow.Location = new Point(566, 511);
        }

        private void pictureBox4_MouseLeave(object sender, EventArgs e)
        {
            backfromaboutandhow.Location = new Point(559, 511);
        }

        private void pictureBox4_MouseClick(object sender, MouseEventArgs e)
        {
            backfromaboutandhow.Visible = false;
            Image image = Properties.Resources.oldtraffordwelcome1;
            this.BackgroundImage = image;
            aboutoldtrafford.Visible = true;
            howtogetthere.Visible = true;
            reserveaticket.Visible = true;

        }

        private void pictureBox2_MouseClick(object sender, MouseEventArgs e)
        {
            Image image = Properties.Resources.howto;
            this.BackgroundImage = image;
            aboutoldtrafford.Visible = false;
            howtogetthere.Visible = false;
            reserveaticket.Visible = false;
            backfromaboutandhow.Visible = true;
        }

        private void pictureBox3_MouseClick(object sender, MouseEventArgs e)
        {
            Image image = Properties.Resources.reg1;
            this.BackgroundImage = image;
            aboutoldtrafford.Visible = false;
            howtogetthere.Visible = false;
            reserveaticket.Visible = false;
            backfromaboutandhow.Visible = false;
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;
            textBox4.Visible = true;
            label1.Visible = true;
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            nextbutton.Visible = true;
            backfromreserve.Visible = true;
        }

        private void pictureBox5_MouseHover(object sender, EventArgs e)
        {
            nextbutton.Location = new Point(545, 463);
        }

        private void pictureBox5_MouseLeave(object sender, EventArgs e)
        {
            nextbutton.Location = new Point(539, 463);
        }

        private void pictureBox6_MouseHover(object sender, EventArgs e)
        {
            backfromreserve.Location = new Point(30, 463);
        }

        private void pictureBox6_MouseLeave(object sender, EventArgs e)
        {
            backfromreserve.Location = new Point(36, 463);
        }

        private void pictureBox6_MouseClick(object sender, MouseEventArgs e)
        {
            nextbutton.Visible = false;
            backfromreserve.Visible = false;
            Image image = Properties.Resources.oldtraffordwelcome1;
            this.BackgroundImage = image;
            aboutoldtrafford.Visible = true;
            howtogetthere.Visible = true;
            reserveaticket.Visible = true;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            textBox4.Visible = false;
            label1.Visible = false;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            label5.Visible = false;
            finish.Visible = false;
            backtoreserve.Visible = false;

        }

        private void pictureBox5_MouseClick(object sender, MouseEventArgs e)
        {
            if (textBox1.Text != "" && textBox2.Text != "" && textBox3.Text != "" && textBox4.Text != "")
            {
                Image image = Properties.Resources.reg21;
                this.BackgroundImage = image;

                textBox1.Visible = false;
                textBox2.Visible = false;
                textBox3.Visible = false;
                textBox4.Visible = false;
                label1.Visible = false;
                label2.Visible = false;
                label3.Visible = false;
                label4.Visible = false;
                label5.Visible = false;
                nextbutton.Visible = false;
                backfromreserve.Visible = false;
                finish.Visible = true;
                backtoreserve.Visible = true;
                N1.Visible = true;
                N2.Visible = true;
                N3.Visible = true;
                N4.Visible = true;
                N5.Visible = true;
                N6.Visible = true;
                N7.Visible = true;
                N8.Visible = true;
                S1.Visible = true;
                S2.Visible = true;
                S3.Visible = true;
                S4.Visible = true;
                S5.Visible = true;
                S6.Visible = true;
                S7.Visible = true;
                S8.Visible = true;
                E1.Visible = true;
                E2.Visible = true;
                E3.Visible = true;
                E4.Visible = true;
                W1.Visible = true;
                W2.Visible = true;
                W3.Visible = true;
                NW1.Visible = true;
                NW2.Visible = true;
                NE1.Visible = true;
                NE2.Visible = true;
                SW.Visible = true;
                SE.Visible = true;

            }
            else
            {
                label5.Visible = true;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            con = new OleDbConnection(@" provider=Microsoft.ace.Oledb.12.0; data source=C:\OldTrafford.accdb; Persist Security Info=False");
            loaddata();
        }
        void loaddata()
        {
            adapter = new OleDbDataAdapter("select * from oldtraff", con);
            ds = new DataSet(); //student-> table name in stud.accdb file
            adapter.Fill(ds, "oldtraff");
            ds.Tables[0].Constraints.Add("pk_ID", ds.Tables[0].Columns[0], true);//creating primary key for Tables[0] in dataset
            //dataGridView1.DataSource = ds.Tables[0];
        }

        private void finish_MouseHover(object sender, EventArgs e)
        {
            finish.Location = new Point(545, 553);
        }

        private void finish_MouseLeave(object sender, EventArgs e)
        {
            finish.Location = new Point(539, 553);
        }

        private void backtoreserve_MouseHover(object sender, EventArgs e)
        {
            backtoreserve.Location = new Point(30, 553);
        }

        private void backtoreserve_MouseLeave(object sender, EventArgs e)
        {
            backtoreserve.Location = new Point(36, 553);
        }

        private void backtoreserve_MouseClick(object sender, MouseEventArgs e)
        {
            Image image = Properties.Resources.reg1;
            this.BackgroundImage = image;
            aboutoldtrafford.Visible = false;
            howtogetthere.Visible = false;
            reserveaticket.Visible = false;
            backfromaboutandhow.Visible = false;
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;
            textBox4.Visible = true;
            label1.Visible = true;
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            nextbutton.Visible = true;
            backfromreserve.Visible = true;
            finish.Visible = false;
            backtoreserve.Visible = false;
            N1.Visible = false;
            N2.Visible = false;
            N3.Visible = false;
            N4.Visible = false;
            N5.Visible = false;
            N6.Visible = false;
            N7.Visible = false;
            N8.Visible = false;
            S1.Visible = false;
            S2.Visible = false;
            S3.Visible = false;
            S4.Visible = false;
            S5.Visible = false;
            S6.Visible = false;
            S7.Visible = false;
            S8.Visible = false;
            E1.Visible = false;
            E2.Visible = false;
            E3.Visible = false;
            E4.Visible = false;
            W1.Visible = false;
            W2.Visible = false;
            W3.Visible = false;
            NW1.Visible = false;
            NW2.Visible = false;
            NE1.Visible = false;
            NE2.Visible = false;
            SW.Visible = false;
            SE.Visible = false;
        }
    }
}

3 个答案:

答案 0 :(得分:2)

您应该考虑使用“用户控件”来分隔您的屏幕。这些控件中的每一个都应该管理它自己与用户的交互。如果你需要让主表知道这些控件之一发生了什么,你可以使用一个事件(就像任何其他类型的控件一样)。

对于您已经显示的示例,您将有5个控件(每个屏幕一个),并且每个控件上可能有一些事件指示用户按下了“下一步”按钮(或其他)。

主控件应该更改在任何给定时间显示哪个控件......并且通过代码而不是GUI设计器执行此操作可能是最简单的。

编辑:

这个链接超级老了,但是它会让你知道我的意思 - 并且让我免于制作一堆屏幕截图并将它们粘贴在这里:

http://msdn.microsoft.com/en-us/library/aa302342.aspx

http://knol.google.com/k/creating-custom-controls-with-c-net#

答案 1 :(得分:1)

我认为你可以像你的申请一样wizard。另外,正如其他人在答案中提到的那样,使用用户控件来“分组”控件以便轻松管理它们。

答案 2 :(得分:0)

您可以为每个人进行一次用户控制,只需隐藏/显示该用户控件。