2013-10-11 55 views
1

我正在修改一些使用DateTimePicker的自定義日曆控件的代碼。我已經設置了ShowCheckBox = true,並且我試圖強制它在用戶單擊複選框(通過更改日期的CustomFormat)時將日期的值更改爲null,該方式有效。我的問題是,它需要太多的點擊複選框來更改DateFormat。我的基本CalendarColumn來自http://msdn.microsoft.com/en-us/library/7tas5c80.aspx,但不包括複選框,並且不允許空值。DateTimePicker需要點擊多個複選框才能觸發OnValueChanged

OnValueChanged()代碼:

protected override void OnValueChanged(EventArgs eventargs) 
    { 
     // Notify the DataGridView that the contents of the cell 
     // have changed. 
     base.OnValueChanged(eventargs); 

     if (this.Checked) 
     { 
      this.Checked = true; 
      this.Format = DateTimePickerFormat.Short; 
     } else if (!this.Checked) 
     { 
      this.Format = DateTimePickerFormat.Custom; 
      this.CustomFormat = " "; 
     } 
    } 

上的複選框(當它的值被檢查)第一次單擊變灰的日期,但不火的OnValueChanged()方法,第二次點擊設置回「選中「,然後觸發事件,第3次點擊將CustomFormat設置爲」「,因此應該顯示空日期。

我已經做了一些研究,我認爲我的問題與第一次點擊獲得單元格焦點有關,但如果我將我的支票放在onGotFocus()中,單擊一下鼠標就會顯示/隱藏日期格式,但是當我在取消選中複選框後點擊另一個單元格時,仍將CustomFormat設置爲「」而不是DateTimePickerFormat.Short

有關類似問題的其他答案請參考http://www.mofeel.net/70-microsoft-public-dotnet-framework-windowsforms/8806.aspx,但我很困惑我將如何將其納入我的代碼。我沒有發佈全班,但如果有人認爲會有幫助,可以做嗎?

回答

1

一位同事爲我解決了這個問題。我會盡量解釋,盡我所能:

OnValueChanged()方法結束看起來像:

protected override void OnValueChanged(EventArgs eventargs) 
{ 
    valueChanged = true; 
    // Notify the DataGridView that the contents of the cell 
    // have changed. 

    this.EditingControlDataGridView.NotifyCurrentCellDirty(true); 
    base.OnValueChanged(eventargs); 

    isChecked(); 
} 

public bool isChecked() 
{ 
    bool isChecked = false; 
    if (this.Checked) 
    { 
     this.Checked = true; 
     this.Format = DateTimePickerFormat.Short; 
     isChecked = true; 
    } 
    else if (!this.Checked) 
    { 
     this.Format = DateTimePickerFormat.Custom; 
     this.CustomFormat = " "; 
     isChecked = false; 
    } 
    return isChecked; 
} 

他還補充這些方法:

protected override void OnClick(EventArgs e) 
{ 
    isChecked(); 
    base.OnClick(e); 
}  

public object EditingControlFormattedValue 
{ 
    get 
    { 
     if (!this.Checked) 
     { 
      return String.Empty;      
     } 
     else 
     { 
      if (this.Format == DateTimePickerFormat.Custom) 
      { 
       return this.Value.ToString(); 
      } 
      else 
      { 
       return this.Value.ToShortDateString(); 
      } 
     } 
    } 
    set 
    { 
     string newValue = value as string; 
     if (!String.IsNullOrEmpty(newValue)) 
     { 
      this.Value = DateTime.Parse(newValue); 
     } 
    } 
} 

InitializaEditingControl也被編輯爲:

public override void InitializeEditingControl(int rowIndex, object 
     initialFormattedValue, DataGridViewCellStyle dataGridViewCellStyle) 
{ 
    // Set the value of the editing control to the current cell value. 
    base.InitializeEditingControl(rowIndex, initialFormattedValue, 
     dataGridViewCellStyle); 
    ctl = DataGridView.EditingControl as CalendarEditingControl; 
    // ctl.Invalidated += new InvalidateEventHandler(ctl_Invalidated); 
    ctl.ValueChangedSpecial += new EventHandler(ctl_ValueChangedSpecial); 
    if (rowIndex >= 0) 
    { 
     try 
     {      
      if (String.IsNullOrEmpty(this.Value.ToString())) 
      { 
       ctl.Checked = false; 
       ctl.Format = DateTimePickerFormat.Custom; 
       ctl.CustomFormat = " "; 
      } 
      else 
      { 
       ctl.Checked = true; 
       ctl.Value = (DateTime)this.Value; 
       ctl.Format = DateTimePickerFormat.Short;    
      } 
     } 
     catch (ArgumentOutOfRangeException aex) 
     { 
      //MessageBox.Show("ERROR. " + aex.Message); 
      ctl.Value = (DateTime)this.DefaultNewRowValue; 
     } 
    } 
} 

然後它工作。

這個答案几乎肯定不令人滿意,但沒有答案的問題幫助沒有人,所以希望這裏的東西可能會幫助別人。

相關問題