user1414157
user1414157

Reputation: 53

dynamically adding tabs

I want to add tabs dynamically during the runtime.

I made a own class for the tabs like this:

namespace Demo
{
    public partial class Tabs : 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 Tabs()
        {
            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";
        }

        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;
        }
    }
}

and I'm adding a new tab with a button:

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

            TabPage test = new TabPage();

            tabsTravel.TabPages.Add(test);
            test.Text = title;
            test.BackColor = Color.White;
            test.Controls.Add(new Tabs());

        }

Adding the tabs with the button and removing them with another button works fine, but now I got some Problems:

  1. the comboWorkerType is depending on what is selected in the comboSubsidiaryTravel. but in all dynamically added tabs, the XML isn't loaded to the comboWorkerType if I selected an item in comboSubsidiaryTravel.
  2. how can I access the dynamically added controls and their values like datetimepicker?

greetz

Tobi

EDIT:

this is the class where I'm using the values of the Datetimepicker:

namespace Demo
{
    class worker
    {
        public void ArrivalTimeSpan()
        {
            TimeSpan Arrival = new TimeSpan();

            Arrival = Page2.arrivalEnd - Page2.arrivalStart;
            System.Windows.Forms.MessageBox.Show(Arrival.ToString());
        }

        public void DepartureTimeSpan() 
        {
            TimeSpan Departure = new TimeSpan();

            Departure = Page2.departureEnd - Page2.departureStart;
            System.Windows.Forms.MessageBox.Show(Departure.ToString());
        }

        public void WorkTimeSpan() 
        {
            TimeSpan Work = new TimeSpan();

            Work = Page3.workEnd - Page3.workStart;
            System.Windows.Forms.MessageBox.Show(Work.ToString());
        }
}
}

Upvotes: 0

Views: 256

Answers (1)

Maarten
Maarten

Reputation: 22955

A1: I do see eventhandler methods, but I do not see a line of code that actually connects the event to the eventhandler. E.g. comboSubsidiaryTravel.SelectedIndexChanged += comboSubsidiaryTravel_SelectedIndexChanged

A2: You can't directly. You have to check the Controls collection property to find them. Unless you are 'inside' the user control, then you can access them directly.

One major fundamental error I see though is the following: your DateTime variables in your user control are static! I'm pretty sure that is not what you want, you want normally instanced properties. How else are you going to keep the values separate?

Upvotes: 1

Related Questions