我正在嘗試在我的 Winforms 表單中更改多個 DateTimePicker 組件的 CustomFormat。但是,當我創建 foreach 回圈時,我沒有選擇更改 CustomFormat。這是代碼:
foreach (Control ctrl in this.Controls)
{
if(ctrl is DateTimePicker)
{
ctrl.CustomFormat = "yyyy-MM-dd";
}
}
我收到錯誤訊息:“'Control' 不包含'CustomFormat' 的定義,并且找不到接受'Control' 型別的第一個引數的可訪問擴展方法'CustomFormat'(您是否缺少 using 指令或程式集參考? )”。
我正在使用 VS 2022 和 .NET CORE 6(最新穩定版本)。
對此的任何幫助都將受到歡迎。
uj5u.com熱心網友回復:
那是因為您仍在使用型別為 Control 的變數“ctrl” - 而不是 DateTimePicker
foreach (Control ctrl in this.Controls)
{
if(ctrl is DateTimePicker dtp)
{
dtp.CustomFormat = "yyyy-MM-dd";
}
}
應該作業得很好。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/406570.html
標籤:
