感謝stackoverflow,我能夠為我的專案動態創建標簽頁。我遇到的問題是如何更新我在 TabPanel TabPages 中的 UserControl 中的內容。
這是我當前創建選項卡的方式(沒有創建一個默認選項卡,因為它永遠不會改變所有其他選項卡都是在 frm_load 時創建的)
private void TechnicianInfo()
{
var techSheet = smartSheet.SheetResources.GetSheet(
0000000000000000, // all 0's for security purposes
null,
null,
null,
null,
null,
null,
null);
// API identifies columns by ID, but it's more convenient to refer to column names
Dictionary<string, long> technicianMap = new Dictionary<string, long>();
// Build the column map for easier reference
foreach (Column column in techSheet.Columns)
technicianMap.Add(column.Title, (long)column.Id);
// Let's loop through the rows and add in information
foreach (Row row in techSheet.Rows)
{
Cell techName = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Name"]);
Cell techCertified = row.Cells.First(cell => cell.ColumnId == technicianMap["Certified Radios"]);
Cell techLevel = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Level"]);
Cell techEmail = row.Cells.First(cell => cell.ColumnId == technicianMap["Email"]);
// Dyanmiclly Create Tab Pages with correct info on them
UserControl newTechnicianControl = new TechnicianControl
{
TechnicianLevel = techLevel.DisplayValue,
CertifiedRadios = techCertified.DisplayValue,
EmailAddress = techEmail.DisplayValue
};
TabPage techPage = new TabPage();
techPage.Controls.Add(newTechnicianControl);
techPage.Text = techName.DisplayValue.ToString();
techPage.Name = "tech_" techName.DisplayValue.ToLower();
tcRepairs.TabPages.Add(techPage);
}
}
在創建程序中,我可以在定義 UserControl 時訪問它的屬性。我有一個計時器,可以每小時從智能表中提取一次更新資訊(請注意,我現在沒有提取我需要的所有資訊。我想在提取所有資訊之前先弄清楚我的問題)。在計時器滴答事件中,我想呼叫一個更新函式,該函式將更新每個 TabPage 中 UserControls 的標簽。
到目前為止,我已經為 UserControl 設定了三個屬性。
public string TechnicianLevel
{
get
{
return this.lblTechLevel.Text;
}
set
{
this.lblTechLevel.Text = value;
}
}
public string CertifiedRadios
{
get
{
return this.lblCertifiedRadios.Text;
}
set
{
this.lblCertifiedRadios.Text = value;
}
}
public string EmailAddress
{
get
{
return this.lblEmail.Text;
}
set
{
this.lblEmail.Text = value;
}
}
我能想到的唯一方法是洗掉除第一個 TabPage 之外的所有內容,然后重新添加它們。我覺得這種方法可行,但我覺得應該有更好的方法來更新 TabPage 中的 UserControl。
uj5u.com熱心網友回復:
感謝@Jimi 和他們回圈遍歷TabPages. 下面現在正在更新UserControl TechnicianControl標簽。非常感謝!
private void UpdateTechnicianInfo()
{
// API identifies columns by ID, but it's more convenient to refer to column names
Dictionary<string, long> columnMap = new Dictionary<string, long>();
var techSheet = smartSheet.SheetResources.GetSheet(
2589985581885316,
null,
null,
null,
null,
null,
null,
null);
// API identifies columns by ID, but it's more convenient to refer to column names
Dictionary<string, long> technicianMap = new Dictionary<string, long>();
// Build the column map for easier reference
foreach (Column column in techSheet.Columns)
technicianMap.Add(column.Title, (long)column.Id);
// Lets loop through the rows and add in information
foreach (Row row in techSheet.Rows)
{
Cell techName = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Name"]);
Cell techCertified = row.Cells.First(cell => cell.ColumnId == technicianMap["Certified Radios"]);
Cell techLevel = row.Cells.First(cell => cell.ColumnId == technicianMap["Technician Level"]);
Cell techEmail = row.Cells.First(cell => cell.ColumnId == technicianMap["Email"]);
foreach (TabPage page in tcRepairs.TabPages)
{
TechnicianControl technicianControl2 = page.Controls.OfType<TechnicianControl>().FirstOrDefault();
if (technicianControl2 != null && && page.Name == "tech_" techName.DisplayValue.ToString())
{
technicianControl2.TechnicianLevel = techName.DisplayValue.ToString();
}
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/497119.html
上一篇:使按鈕與另一個按鈕動態保持距離
