winform usercontrols之间的通信,以将tab添加到tabcontrol

时间:2012-07-10 08:57:24

标签: c# user-controls tabcontrol dynamically-generated

我想在点击按钮后添加一个TabPage。我遇到的困难是我有一个带有按钮的UserControl,而tabpage是一个usercontrol,我想用TabControl添加到现有的UserControl。

这是我要添加的标签的Usercontrol:

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

namespace Demo
{
    public partial class TabsTravel : UserControl
    {
        private static DateTime ArrivalStart;
        public static DateTime arrivalStart
        {
            get { return ArrivalStart; }
            set { ArrivalStart = value; }

        }

        private static DateTime ArrivalEnd;
        public static DateTime arrivalEnd
        {
            get { return ArrivalEnd; }
            set { ArrivalEnd = value; }

        }

        private static DateTime DepartureStart;
        public static DateTime departureStart
        {
            get { return DepartureStart; }
            set { DepartureStart = value; }

        }

        private static DateTime DepartureEnd;
        public static DateTime departureEnd
        {
            get { return DepartureEnd; }
            set { DepartureEnd = value; }

        }

        public TabsTravel()
        {
            InitializeComponent();
            LoadSubsidiaryXML();
            dtpArrivalStart.Format = DateTimePickerFormat.Custom;
            dtpArrivalStart.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpArrivalEnd.Format = DateTimePickerFormat.Custom;
            dtpArrivalEnd.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpDepartureStart.Format = DateTimePickerFormat.Custom;
            dtpDepartureStart.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpDepartureEnd.Format = DateTimePickerFormat.Custom;
            dtpDepartureEnd.CustomFormat = "ddd dd MMM yyyy hh:mm";
            comboSubsidiaryTravel.SelectedIndexChanged += new EventHandler(comboSubsidiaryTravel_SelectedIndexChanged);
        }

        private void LoadSubsidiaryXML()
        {
            XmlDocument subsidiary = new XmlDocument();
            subsidiary.Load("Subsidiaries.xml");

            XmlNodeList Subname = subsidiary.SelectNodes("subsidiaries/type/name");
            foreach (XmlNode name in Subname)
            {
                comboSubsidiaryTravel.Items.Add(name.InnerText);
            }
        }

        private void LoadWorkerXML(string xmlType)
        {
            comboWorkerType.Items.Clear();
            XmlDocument workerType = new XmlDocument();
            workerType.Load(xmlType);

            XmlNodeList worker = workerType.SelectNodes("worker/type/name");
            foreach (XmlNode name in worker)
            {
                comboWorkerType.Items.Add(name.InnerText);
            }
        }

         //Select XML which have to be populated to comboWorkerType
        private void chooseWorker()
        {
            string xmlType = "";
            string subsidiary = "";
            subsidiary = comboSubsidiaryTravel.Text;
            switch (subsidiary)
            {
                case "GH":
                    xmlType = "GHworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GP":
                    xmlType = "GPworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GN":
                    xmlType = "GNworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GT":
                    xmlType = "GTworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;
                case "GS":
                    xmlType = "GSworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GK":
                    xmlType = "GKworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GBH":
                    xmlType = "GBHworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GAS":
                    xmlType = "GASworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "Others":
                    comboWorkerType.Items.Clear();
                    break;
                default:
                    break;
            }
        }

        private void comboSubsidiaryTravel_SelectedIndexChanged(object sender, EventArgs e)
        {
            chooseWorker();
        }

        private void dtpArrivalStart_ValueChanged(object sender, EventArgs e)
        {
            ArrivalStart = dtpArrivalStart.Value;
            dtpArrivalEnd.MinDate = dtpArrivalStart.Value;
        }

        private void dtpArrivalEnd_ValueChanged(object sender, EventArgs e)
        {
            ArrivalEnd = dtpArrivalEnd.Value;
            dtpDepartureStart.MinDate = dtpArrivalEnd.Value;
        }

        private void dtpDepartureStart_ValueChanged(object sender, EventArgs e)
        {
            DepartureStart = dtpDepartureStart.Value;
            dtpDepartureEnd.MinDate = dtpDepartureStart.Value;
        }

        private void dtpDepartureEnd_ValueChanged(object sender, EventArgs e)
        {
            DepartureEnd = dtpDepartureEnd.Value;
        }
    }
}

这是我获得TabControl的Usercontrol

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MBG.SimpleWizard;
using System.Globalization;

namespace Demo
{
    public partial class Page3 : UserControl, IWizardPage
    {
        int c = 1;
        private static DateTime WorkStart;
        public DateTime workStart 
        {
            get { return WorkStart; }
            set { WorkStart = value; }
        }

        private static DateTime WorkEnd;
        public DateTime workEnd 
        {
            get { return WorkEnd; }
            set { WorkEnd = value; }
        }

        public Page3()
        {
            InitializeComponent();
            dtpWorkStart.Format = DateTimePickerFormat.Custom;
            dtpWorkStart.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpWorkEnd.Format = DateTimePickerFormat.Custom;
            dtpWorkEnd.CustomFormat = "ddd dd MMM yyyy hh:mm";
        }

        #region IWizardPage Members

        public UserControl Content
        {
            get { return this; }
        }

        public new void Load()
        {

        }

        public void Save()
        {
        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public bool IsBusy
        {
            get { throw new NotImplementedException(); }
        }

        public bool PageValid
        {
            get { return true; }
        }

        public string ValidationMessage
        {
            get { throw new NotImplementedException(); }
        }

        #endregion

        private void dtpWorkStart_ValueChanged(object sender, EventArgs e)
        {
            workStart = dtpWorkStart.Value;
            dtpWorkStart.MinDate = dtpWorkEnd.Value;
        }

        private void dtpWorkEnd_ValueChanged(object sender, EventArgs e)
        {
            workEnd = dtpWorkEnd.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            dataGridView1.DataSource = GetTable();

            if (c == 1)
            {
                DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();
                dataGridView1.Columns.Add(chk);
                chk.HeaderText = "Holiday";
                chk.Name = "Holiday";
            }
            c++;

        }

        public static DataTable GetTable() 
        {
            DataTable table = new DataTable();
            DateTime test = new DateTime();
            DateTime test2 = new DateTime();
            CultureInfo culture = CultureInfo.CurrentCulture;
            int week = 0;

            test = WorkStart;
            table.Columns.Add("Calendarweek", typeof(int));
            table.Columns.Add("Day", typeof(string));
            table.Columns.Add("Date", typeof(DateTime));
            table.Columns.Add("Start work", typeof(string));
            table.Columns.Add("End work", typeof(string));
            table.Columns.Add("Total", typeof(TimeSpan));

            while (test <= WorkEnd)
            {
                test2 = test;
                test2 = test2.AddHours(10);
                if (test.DayOfWeek != DayOfWeek.Sunday)
                {
                    week = culture.Calendar.GetWeekOfYear(test, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
                    table.Rows.Add(week, test.DayOfWeek.ToString(), test.ToString("dd MMM"), test.ToString("hh:mm tt", CultureInfo.InvariantCulture), test2.ToString("hh:mm tt", CultureInfo.InvariantCulture), test2 - test);

                }
                test = test.AddDays(1);
                //table.Rows.Add();

            }
            //Zugriff auf Zelle zuerst zeile dann spalte
            //string testString = table.Rows[3][2].ToString();


            table.RowChanged += new DataRowChangeEventHandler(table_RowChanged);

            //MessageBox.Show(testString);

            return table;

        }


        static DataRow getRow(DataTable table)
        {
            DataRow row = table.NewRow();

            return row;

        }

        static void table_RowChanged(object sender, DataRowChangeEventArgs e)
        {
            //e.Row.AcceptChanges();
            //throw new StackOverflowException();
            //throw new NotImplementedException();
        }

        private void button2_Click(object sender, EventArgs e)
        {

        }
    }
}

这是UserControl,我在那里获得了添加tabPage的按钮:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MBG.SimpleWizard;
using System.Xml;
using System.Xml.Linq;

namespace Demo
{
    public partial class Page2 : UserControl, IWizardPage
    {
        private static DateTime ArrivalStart;
        public DateTime arrivalStart 
        {
            get { return ArrivalStart; }
            set { ArrivalStart = value; }

        }

        private static DateTime ArrivalEnd;
        public DateTime arrivalEnd
        {
            get { return ArrivalEnd; }
            set { ArrivalEnd = value; }

        }

        private static DateTime DepartureStart;
        public DateTime departureStart
        {
            get { return DepartureStart; }
            set { DepartureStart = value; }

        }

        private static DateTime DepartureEnd;
        public DateTime departureEnd
        {
            get { return DepartureEnd; }
            set { DepartureEnd = value; }

        }

        private List<string> groupItems = new List<string>();

        public Page2()
        {
            InitializeComponent();
            LoadSubsidiaryXML();
            dtpArrivalStart.Format = DateTimePickerFormat.Custom;
            dtpArrivalStart.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpArrivalEnd.Format = DateTimePickerFormat.Custom;
            dtpArrivalEnd.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpDepartureStart.Format = DateTimePickerFormat.Custom;
            dtpDepartureStart.CustomFormat = "ddd dd MMM yyyy hh:mm";
            dtpDepartureEnd.Format = DateTimePickerFormat.Custom;
            dtpDepartureEnd.CustomFormat = "ddd dd MMM yyyy hh:mm";

            groupItems.Add("Customer");
            groupItems.Add("Intercompany");

            comboWorkerType.DrawMode = DrawMode.OwnerDrawFixed;
        }



        private void LoadSubsidiaryXML()
        {
            XmlDocument subsidiary = new XmlDocument();
            subsidiary.Load("Subsidiaries.xml");

            XmlNodeList Subname = subsidiary.SelectNodes("subsidiaries/type/name");
            foreach (XmlNode name in Subname)
            {
                comboSubsidiaryTravel.Items.Add(name.InnerText);
            }
        }

        private void LoadWorkerXML(string xmlType)
        {
            comboWorkerType.Items.Clear();
            XmlDocument workerType = new XmlDocument();
            workerType.Load(xmlType);

            XmlNodeList worker = workerType.SelectNodes("worker/type/name");
            foreach (XmlNode name in worker)
            {
                //if (name.InnerText == "Customer" || name.InnerText == "Intercompany")
                //{
                //    comboWorkerType.Font = new Font(comboWorkerType.Font, FontStyle.Bold);
                //}


                comboWorkerType.Items.Add(name.InnerText);
            }
        }

        //private void comboWorkerType_DrawItem(object sender, DrawItemEventArgs e) 
        //{
        //    e.DrawBackground();

        //    if (e.Index > -1)
        //    {
        //        string drawText = comboWorkerType.Items[e.Index].ToString();
        //        if (groupItems.Contains(drawText))
        //        {
        //            using (Font font = new Font(comboWorkerType.Font, FontStyle.Bold))
        //                e.Graphics.DrawString(drawText, font, Brushes.Black, e.Bounds);
        //        }
        //        else
        //            e.Graphics.DrawString(drawText, comboWorkerType.Font, Brushes.Black, new Rectangle(16, e.Bounds.Top, e.Bounds.Width - 16, e.Bounds.Height));

        //        e.DrawFocusRectangle();
        //    }


        //}


        #region IWizardPage Members

        public UserControl Content
        {
            get { return this; }
        }

        public new void Load()
        {

        }

        public void Save()
        {
        }

        public void Cancel()
        {
            throw new NotImplementedException();
        }

        public bool IsBusy
        {
            get { throw new NotImplementedException(); }
        }

        public bool PageValid
        {
            get { return true; }
        }

        public string ValidationMessage
        {
            get { throw new NotImplementedException(); }
        }

        #endregion
        private void btnAddWorker_Click(object sender, EventArgs e)
        {
            string title = "worker " + (tabsTravel.TabCount + 1).ToString();

            TabPage travel = new TabPage();
            TabPage work = new TabPage();

            tabsTravel.TabPages.Add(travel);
            travel.Text = title;
            travel.BackColor = Color.White;
            travel.Controls.Add(new TabsTravel());



        }

        private void btnRemoveWorker_Click(object sender, EventArgs e)
        {
            tabsTravel.TabPages.Remove(tabsTravel.SelectedTab);
        }

        // Select XML which have to be populated to comboWorkerType
        private void chooseWorker() 
        {
            string xmlType = "";
            string subsidiary = "";
            subsidiary = comboSubsidiaryTravel.Text;
            switch (subsidiary)
            {   
                case "GH":
                    xmlType = "GHworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GP":
                    xmlType = "GPworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GN":
                    xmlType = "GNworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GT":
                    xmlType = "GTworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;
                case "GS":
                    xmlType = "GSworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GK":
                    xmlType = "GKworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GBH":
                    xmlType = "GBHworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "GAS":
                    xmlType = "GASworkerType.xml";
                    LoadWorkerXML(xmlType);
                    break;

                case "Others":
                    comboWorkerType.Items.Clear();
                    break;
                default:
                    break;
            }
        }

        private void comboSubsidiaryTravel_SelectedIndexChanged(object sender, EventArgs e)
        {
            chooseWorker();
        }

        private void dtpArrivalStart_ValueChanged(object sender, EventArgs e)
        {
            ArrivalStart = dtpArrivalStart.Value;
            dtpArrivalEnd.MinDate = dtpArrivalStart.Value;
        }

        private void dtpArrivalEnd_ValueChanged(object sender, EventArgs e)
        {
            ArrivalEnd = dtpArrivalEnd.Value;
            dtpDepartureStart.MinDate = dtpArrivalEnd.Value;
        }

        private void dtpDepartureStart_ValueChanged(object sender, EventArgs e)
        {
            DepartureStart = dtpDepartureStart.Value;
            dtpDepartureEnd.MinDate = dtpDepartureStart.Value;
        }

        private void dtpDepartureEnd_ValueChanged(object sender, EventArgs e)
        {
            DepartureEnd = dtpDepartureEnd.Value;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            worker test = new worker();
            //test.ArrivalTimeSpan();
            //test.DepartureTimeSpan();
            //test.WorkTimeSpan();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

    }
}

使用btnAddWorker我想添加tabPage。

如何使用button_click事件将tabPage添加到带有TabControl的UserControl?

感谢所有有用的答案

格尔茨

托比

编辑:

嗨,直到现在还没有运行。我需要一点提示。

这是UserControl A中的委托:

public delegate void tabWork(TabControl tabsWork);

这里我得到了UserControl A中的按钮和addTab方法:

private void btnAddWorker_Click(object sender, EventArgs e)
        {

            addTab(tabsTravel);

        }

        static void addTab(TabControl tabsTravel) 
        {

            string title = "worker " + (tabsTravel.TabCount + 1).ToString();

            TabPage travel = new TabPage();
            tabWork work;

            tabsTravel.TabPages.Add(travel);
            travel.Text = title;
            travel.BackColor = Color.White;
            travel.Controls.Add(new TabsTravel());

            work = new tabWork(Page3.addTab);



        }

在这里我得到了UserControl B中的addTab方法:

public static void addTab(TabControl tabsWork) 
        {
            TabsWork work = new TabsWork();
            TabPage Tabwork = new TabPage();
            string title = "worker " + (tabsWork.TabCount + 1).ToString();

            tabsWork.TabPages.Add(Tabwork);
            Tabwork.Text = title;
            Tabwork.BackColor = Color.White;
            Tabwork.Controls.Add(new TabsWork());

        }

但从不调用UserControl B中的addTab。

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您应该创建一个事件,通知其他用户控件创建/添加另一个传递沿着对象的选项卡,其中包含您希望在其他userControl中可用的信息