1. 程式人生 > >C#實現Windows應用高階控制元件&註冊視窗

C#實現Windows應用高階控制元件&註冊視窗

視窗顯示為
在這裡插入圖片描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

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

        private void ckInfo_CheckedChanged(object sender, EventArgs e)
        {
            if (ckInfo.Checked == true)
            {
                ckLinfo.Visible = true;

                CheckAll(ckLinfo);
            }
            else
            {
                ckLinfo.Visible = false;
                CheckAllEsce(ckLinfo);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            radWoman.Checked = false;
            radMan.Checked = false;
            //  VisbCheck(false);
        }

        private void ckShop_CheckedChanged(object sender, EventArgs e)
        {
            if (ckShop.Checked == true)
            {
                cklShop.Visible = true;
                CheckAll(cklShop);
            }
            else
            {
                cklShop.Visible = false;
                CheckAllEsce(cklShop);
            }// end block if 
        }

        private void ckSell_CheckedChanged(object sender, EventArgs e)
        {
            if (ckSell.Checked == true)
            {
                cklSell.Visible = true;
                CheckAll(cklSell);
            }
            else
            {
                cklSell.Visible = false;
                CheckAllEsce(cklSell);
            }// end block if 
        }

        private void ckMange_CheckedChanged(object sender, EventArgs e)
        {
            if (ckMange.Checked == true)
            {
                cklMange.Visible = true;
                CheckAll(cklMange);
            }
            else
            {
                cklMange.Visible = false;
                CheckAllEsce(cklMange);

            }// end block
        }
        //全部選中方法,引數傳控制元件名稱name 屬性值 
        public void CheckAll(object chckList)
        {
            if (chckList.GetType().ToString() == "System.Windows.Forms.CheckedListBox")
            {
                CheckedListBox ckl = (CheckedListBox)chckList;
                for (int i = 0; i < ckl.Items.Count; i++)
                { ckl.SetItemCheckState(i, CheckState.Checked); }
            }// end block if 
        }//end mehtod block 
        //全部取選中方法,引數傳控制元件名稱name 屬性值 
        public void CheckAllEsce(object chckList)
        {
            if (chckList.GetType().ToString() == "System.Windows.Forms.CheckedListBox")
            {
                CheckedListBox ckl = (CheckedListBox)chckList;
                for (int i = 0; i < ckl.Items.Count; i++)
                { ckl.SetItemCheckState(i, CheckState.Unchecked); }
            }// end block if 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("使用者姓名不能為空", "提示");
                    return;
                }
                if (textBox2.Text == "")
                {
                    MessageBox.Show("使用者密碼", "提示");
                    return;
                }
                if (radMan.Checked == false && radWoman.Checked == false)
                {
                    MessageBox.Show("請選擇使用者性別", "提示");
                    return;
                }
                if (ckInfo.Checked == false && ckMange.Checked == false && ckSell.Checked == false && ckShop.Checked == false)
                {
                    MessageBox.Show("請任選一項使用者許可權", "提示");
                    return;
                }// end 
                string strName = textBox1.Text.ToString();
                string strPassword = textBox2.Text;
                string strPhon = textBox3.Text;
                string srtEmail = textBox4.Text;
                string strAdress = textBox5.Text;
                string strSex;
                if (radWoman.Checked == true)
                {
                    strSex = "女";
                }
                else
                {
                    strSex = "男";
                }

                string strCkNabge = "庫存管理:" + "\n";
                string strCklsell = "銷售管理:" + "\n";
                string strCklShop = "進貨管理:" + "\n";

                string strCkl = "基本檔案:" + "\n";
                if (ckLinfo.Visible == true)
                {

                    for (int i = 0; i < ckLinfo.CheckedItems.Count; i++)
                    {

                        strCkl += ckLinfo.CheckedItems[i].ToString() + "\n";
                    }
                }// end block if 
                if (cklMange.Visible == true)
                {

                    for (int i = 0; i < cklMange.CheckedItems.Count; i++)
                    {
                        strCkNabge += cklMange.CheckedItems[i].ToString() + "\n";
                    }

                }// end block 
                if (cklSell.Visible == true)
                {

                    for (int i = 0; i < cklSell.CheckedItems.Count; i++)
                    {
                        strCklsell += cklSell.CheckedItems[i].ToString() + "\n";
                    }

                }// end block 
                if (cklShop.Visible == true)
                {
                    for (int i = 0; i < cklShop.CheckedItems.Count; i++)
                    {
                        strCklShop += cklShop.CheckedItems[i].ToString() + "\n";

                    }
                }// end blick if 
                MessageBox.Show("註冊資訊如下:" + "\n" + "姓名:" + strName + "\n" + "密碼:" + strPassword + "\n" + "電話:" + strPhon + "\n" + "郵箱:" + srtEmail + "\n" + "地址:" + strAdress + "\n" + "性別:" + strSex + "\n" + "使用者許可權如下:" + "\n" + strCkl + strCkNabge + strCklsell + strCklShop, "資訊確認");
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            radWoman.Checked = false;
            radMan.Checked = false;
            ckInfo.Checked = false;
            ckMange.Checked = false;
            ckSell.Checked = false;
            ckShop.Checked = false;
        }
    }
}

除錯效果為:
在這裡插入圖片描述
由於沒有連線資料庫,所以輸入的資訊不能在資料庫中看到,不過能實現簡單的頁面操作,除錯能成功。
後面學習資料訪問技術。。