2013-03-04 154 views
0

您好我試圖動態地使用2D陣列創建的按鈕的這種4×4柵格的顏色狀態保存到XML文檔的控制狀態:保存當窗體關閉

enter image description here

然而,當我按保存我一直得到這個消息:

我可以使這個工作,如果我使用一維數組的按鈕,但不會給我我想要的網格,但當我使用二維數組的按鈕它不會工作:

enter image description here

我能改,所以我可以得到這個工作的任何建議都大加讚賞:

這是我的代碼,我有:

FormState類:

public class FormState 
{ 
    public string ButtonBackColor { get; set; } 
} 

表單代碼:

public partial class Form1 : Form 
{ 
     int col = 4; 
     int row = 4; 
     Button[,] buttons; 
     FormState[,] states; 
     public Form1() 
     { 
      InitializeComponent(); 
      buttons = new Button[col, row]; 
      states = new FormState[col, row]; 
     } 

     public void placeRows() 
     { 
      for (int r = 0; r < row; r++) 
      { 
       createColumns(r); 
      } 
     } 

     public void createColumns(int r) 
     { 
      int s = r * 25; //gap 
      for (int c = 0; c < col; c++) 
      { 
       buttons[r, c] = new Button(); 
       buttons[r, c].SetBounds(75 * c, s, 75, 25); 
       buttons[r, c].Text = Convert.ToString(c); 
       buttons[r, c].Click += new EventHandler(grid_Click); 
       panel1.Controls.Add(buttons[r, c]); 
      } 
     } 

     int count = 0; 
     //backcolor change 
     void grid_Click(object sender, EventArgs e) 
     { 
      Button button = sender as Button; 

      if (count == 0) 
      { 
       button.BackColor = Color.Red; 
       count++; 
      } 

      else if (count == 1) 
      { 
       button.BackColor = Color.Blue; 
       count--; 
      } 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      placeRows(); 

      if (File.Exists("config.xml")) 
      { 
       loadConfig(); 
      } 

      for (int i = 0; i < col; ++i) 
      { 
       for (int j = 0; j < row; ++j) 
       { 
        if (states[i,j] != null) 
        { 
         buttons[i,j].BackColor = ColorTranslator.FromHtml(states[i,j].ButtonBackColor); 
        } 
       } 
      } 
     } 

     //method to load file 
     private void loadConfig() 
     { 
      XmlSerializer ser = new XmlSerializer(typeof(FormState[])); 
      using (FileStream fs = File.OpenRead("config.xml")) 
      { 
       states = (FormState[,])ser.Deserialize(fs); 
      } 
     } 

     private void writeConfig() 
     { 
      for (int i = 0; i < col; i++) 
      { 
       for (int j = 0; j < row; j++) 
       { 
        if (states[i,j] == null) 
        { 
         states[i,j] = new FormState(); 
        } 
        states[i,j].ButtonBackColor = ColorTranslator.ToHtml(buttons[i,j].BackColor); 
       } 

       using (StreamWriter sw = new StreamWriter("config.xml")) 
       { 
        XmlSerializer ser = new XmlSerializer(typeof(FormState[])); 
        ser.Serialize(sw, states); 
       } 
      } 
     } 

     private void btnSave_Click(object sender, EventArgs e) 
     { 
      writeConfig(); 
     } 
    } 
+0

在您的例外中單擊「查看詳細信息」,它很可能說您的課程沒有標記爲可序列化 – 2013-03-04 01:04:43

+0

不,它顯示:{「生成XML文檔時發生錯誤。」} – Tacit 2013-03-04 01:07:15

+0

如果您繼續鑽探?內部異常? – 2013-03-04 01:08:08

回答

1

這可能不是一個理想的解決方案(我還沒有嘗試過,所以它可能甚至不工作),但你可以創建一個嵌套的數組,而不是2-d陣列。像

FormStates[][] states = new FormStates[row][]; 
for(Int32 i = 0; i < row; i++) 
{ 
    states[i] = new FormStates[col]; 
} 

使用states[i, j]索引,而不是其他,你會使用states[i][j]。由於1-D數組是可序列化的,所以這可能起作用。

編輯

稍長例如,根據您的代碼:

public partial class Form1 : Form 
{ 
    int col = 4; 
    int row = 4; 
    Button[][] buttons; 
    FormState[][] states; 
    public Form1() 
    { 
     InitializeComponent(); 
     buttons = new Button[col][]; 
     states = new FormState[col][]; 
     for(Int32 c = 0; c < col; c++) 
     { 
      buttons[c] = new Button[row]; 
      states[c] = new FormState[row]; 
     } 
    } 

    public void createColumns(int r) 
    { 
     int s = r * 25; //gap 
     for (int c = 0; c < col; c++) 
     { 
      buttons[r][c] = new Button(); 
      buttons[r][c].SetBounds(75 * c, s, 75, 25); 
      buttons[r][c].Text = Convert.ToString(c); 
      buttons[r][c].Click += new EventHandler(grid_Click); 
      panel1.Controls.Add(buttons[r][c]); 
     } 
    } 
} 

這應該足以給你的語法更改代碼的其餘部分。

您還需要將XmlSerializer聲明更改爲使用typeof(FormState[][])而不是typeof(FormState[])

+0

不知你能不能給我一個較長的例子請 – Tacit 2013-03-04 17:33:28

+0

我編輯我的答案,包括更長的例子(根據您發佈的代碼)。希望有所幫助。 – 2013-03-05 04:55:57

+0

感謝隊友我得到它的工作 – Tacit 2013-03-06 16:23:28

0

誠然我沒有PU牛逼多想這個,但是按照意見,你不能序列化多維數組,所以你可能:如果有必要

[Serializable] 
    public class FormState 
    { 
     public int RowIndex { get; set; } 
     public int ColIndex { get; set; } 
     public string BackColor { get; set; } 
    } 

    [Serializable] 
    public class Layout : Collection<FormState> { 

     public Layout(){} 
    } 

..

 public void SomeCallingMethod() { 
      Layout l = new Layout(); 
      foreach (FormState fs in l) { 
       buttons[fs.RowIndex, fs.ColIndex].BackColor = ColorTranslator.FromHtml(fs.BackColor); 
      }  
     } 

也可以是列表和序列化。

+0

? – Tacit 2013-03-04 17:31:22