2013-03-04 2 views
0

안녕 XML 문서에 동적으로 2 차원 배열을 사용하여 생성 된 버튼의 4x4 그리드의 색 상태를 저장하려고 폐쇄 제어 상태 : 내가 저장 누르 그러나저장 양식

enter image description here

이 메시지가 계속 표시됩니다.

버튼에 1 차원 배열을 사용하면이 작업을 수행 할 수 있지만 원하는 그리드를 얻지는 못합니다. 버튼에 2D 배열을 사용하면 작동하지 않습니다.

나는이 어떤 제안을 작동시킬 수 있도록 변경할 수 있습니다 무엇 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

예외에서 "세부 정보보기"를 클릭하면 클래스가 직렬화 가능으로 표시되어 있지 않습니다. –

+0

아니요 : { "XML 문서를 생성하는 동안 오류가 발생했습니다."} – Tacit

+0

그리고 계속 드릴하는 경우? 내부 예외? –

답변

1

이것은 이상적인 솔루션이 아니므로 시도하지 않았으므로 작동하지 않을 수도 있지만 2 차원 배열 대신 중첩 된 배열을 만들 수 있습니다. states[i, j]를 사용하여 색인의 대신

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

같은 것을, 당신은 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]); 
     } 
    } 
} 

이렇게하면 코드의 나머지 부분을 변경할 수있는 구문을 제공하기에 충분합니다.

또한 선언을 typeof(FormState[]) 대신 typeof(FormState[][])을 사용하도록 변경해야 할 수도 있습니다.

+0

좀 더 오래 기다리시겠습니까 – Tacit

+0

게시 한 코드를 기반으로 한 더 긴 예제를 포함하도록 내 대답을 편집했습니다. 희망이 도움이됩니다. –

+0

감사합니다. 친구가 작동하도록했습니다. – Tacit

0

틀림없이 나는 pu t은 많은이에 생각하지만 수도 있도록 의견에 따라 당신은 다중 차원 배열을 직렬화 캔트 :

[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

으로 변환해야하며 SomeCallingMethod()는 어디에 위치하고 호출 되었습니까? – Tacit