1

单击按钮后,我想添加一个 TabPage。对我来说,困难在于我得到了一个带有按钮的 UserControl,而标签页是一个用户控件,我想用 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 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 的用户控件

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)
        {

        }
    }
}

这是用户控件,我在其中获得了添加 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。

我怎么解决这个问题?

4

1 回答 1

0

您应该创建一个事件,指示其他用户控件创建/添加另一个选项卡,该选项卡传递带有您希望在其他用户控件中可用的信息的对象

于 2012-07-10T09:05:55.307 回答