2011-02-01 103 views
0

我爲我的winforms程序在各種.settings文件中存儲了一大堆設置。 包括在這些設置是System.Drawing.PointSystem.Drawing.LocationSystem.TimeSpanSystem.DateTime等..數據綁定非字符串TextBox.Text(WinForms)

當我綁定形式的LocationSystem.Drawing.Location,並調用.Save()方法,這一切似乎很好地工作。也就是說,由於似乎不需要演員表,因此表格的System.Drawing.Location與存儲在.settings文件中的System.Drawing.Location設置直接兼容。 此外,如果我說TimeSpan timeSpan = Settings.Duration;也可以正常工作。

現在,我做了一個大的設置表單,用戶可以在其中調整各種參數,包括各種DateTimeTimeSpan設置。這些都是在很多TextBox可見和可編輯的,我有以下方式綁定的數據:

Settings DefaultSettings = Settings.Default; 
TextBox1.DataBindings.Add(("Text", DefaultSettings, "Duration", false, DataSourceUpdateMode.OnValidation, new TimeSpan(00, 30, 00)); 

TimeSpan是可見的TextBox,但是當我嘗試對其進行編輯和調用設置數據源Save(),我得到以下錯誤:

Value of '0' is not valid for 'Value'. 'Value' should be between 'Minimum' and 'Maximum'. Parameter name: Value

錯誤從Visual Studio發起生成的代碼塊:

[global::System.Configuration.UserScopedSettingAttribute()] 
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()] 
[global::System.Configuration.DefaultSettingValueAttribute("00:30:00")] 
public global::System.TimeSpan StartWorkDay { 
    get { 
    return ((global::System.TimeSpan)(this["Duration"])); 
    } 
    set { 
    this["Duration"] = value; 
    } 
} 

我認爲問題是由於它試圖將字符串變成System.TimeSpan以及我在.settings文件中的其他各種類型的事實所致。

因爲我使用DataBindings.Add(來綁定它們,它接受參數的字符串,所以我不能在那裏使用新的關鍵字。

手動代碼處理這一切:通過構建對象參數的參數更新設置文件,但我有存儲在這麼多的文本框和NumericUpDowns這麼多的設定,我寧願只是將它們綁定直接到TextBox,假設可能。

我能達到這個目標的最簡單方法是什麼?

+0

您應該爲WinForms,asp.net,WPF或任何正在使用的技術添加標籤。這個問題與c#以及與演示技術有關的大多數問題都很少。 – 2011-02-01 03:48:17

回答

2

我在示例窗體窗體應用程序中嘗試了下面的代碼,它沒有任何例外地進行保存。

// Define the settings binding source 
private System.Windows.Forms.BindingSource settingsBindingSource; 

private void InitializeComponent() 
    { 
      this.components = new System.ComponentModel.Container(); 
      this.durationTextBox = new System.Windows.Forms.TextBox(); 
      this.settingsBindingSource = new BindingSource(this.components); 
      this.settingsBindingSource.DataSource = typeof(WindowsFormsApplication1.Properties.Settings); 
      this.durationTextBox.DataBindings.Add(new Binding("Text", this.settingsBindingSource, "Duration", true)); 
    } 

// In the form load event where the textbox is displayed 
private void Form1_Load(object sender, System.EventArgs e) 
{ 
    settingsBindingSource.DataSource = Settings.Default; 
} 

// save button click 
private void button1_Click_1(object sender, System.EventArgs e) 
{ 
    // This saved the settings, without any exceptions 
    Settings.Default.Save(); 
} 

希望它有幫助。

2

你可以在Settings類申報了「轉換屬性」(我剛剛發明這個詞):

public class Settings 
{ 
    // The real property 
    public TimeSpan StartWorkDay { get; set; } 

    // The conversion property 
    public string StartWorkDayString 
    { 
     get 
     { 
      return StartWorkDay.ToString(); 
      // (or use .ToString("...") to format it) 
     } 
     set 
     { 
      StartWorkDay = TimeSpan.Parse(value); 
      // (or use TryParse() to avoid throwing exceptions) 
     } 
    } 
} 

...,然後綁定文本框了這一點。

0

我想出了幾分優雅的解決方案,你可能有興趣

因爲這個問題似乎是,我無法強制轉換或構造的數據類型我在.settings文件已經從簡單的文本框,因爲我正在使用數據綁定,我反而做了一些自定義控件。

例如,時間跨度現在使用一個TimeSpanPicker我開出了DateTimePicker控制的,在禁用時,向上/向下切換的時間,並且Value屬性是從選取器控件內轉換爲一個時間跨度。

此方法的附加優點是,我不需要使用文本框做很多我需要的驗證,因爲TimeSpanPicker基本控件DateTimePicker將只顯示有效時間。我需要做的小驗證可以在Set {}屬性中完成,所以我不需要定義事件處理程序。

這似乎工作得很好! 現在我需要做的就是用自定義控件替換所有的文本框。