実行時にタブを動的に追加したい。
次のようなタブ用の独自のクラスを作成しました。
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;
}
}
}
ボタン付きの新しいタブを追加しています:
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());
}
ボタンでタブを追加し、別のボタンでタブを削除するとうまくいきますが、いくつかの問題があります:
- omboWorkerType は、comboSubsidiaryTravel で何が選択されているかによって異なります。しかし、動的に追加されたすべてのタブで、comboSubsidiaryTravel でアイテムを選択した場合、XML はomboWorkerType にロードされません。
- 動的に追加されたコントロールとその値 (datetimepicker など) にアクセスするにはどうすればよいですか?
挨拶
トビ
編集:
これは、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());
}
}
}