Note that there are some explanatory texts on larger screens.

plurals
  1. POcommunication between winform usercontrols to add tab to tabcontrol
    text
    copied!<p>I want to add a TabPage after clicking a button. The difficulty for me is that I got a UserControl with the button, and the tabpage is a usercontrol which I want to add to an existing UserControl with the TabControl.</p> <p>here is the Usercontrol for the Tabs which I want to add:</p> <pre><code>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; } } } </code></pre> <p>This is the Usercontrol where I got the TabControl</p> <pre><code>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 &lt;= 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) { } } } </code></pre> <p>and this is the UserControl where I got the button to add the tabPage:</p> <pre><code>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&lt;string&gt; groupItems = new List&lt;string&gt;(); 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 &gt; -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) { } } } </code></pre> <p>With btnAddWorker I want to add the tabPage.</p> <p>How can I use the button_click event to add the tabPage to the UserControl wit the TabControl?</p> <p>thanks for all helpful answers</p> <p>greetz</p> <p>Tobi </p> <p>EDIT:</p> <p>Hi, it doesn't run till now. I need a little hint.</p> <p>here is the delegate in UserControl A:</p> <pre><code>public delegate void tabWork(TabControl tabsWork); </code></pre> <p>here I got the button and the addTab-method in UserControl A:</p> <pre><code>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); } </code></pre> <p>and here I got the addTab-method in UserControl B:</p> <pre><code>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()); } </code></pre> <p>but the addTab in UserControl B is never called.</p> <p>how can I solve this problem?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload