2015-11-28 26 views
5

Dovrei creare un quadrato magico in 2D utilizzando l'applicazione Windows Form. Esso dovrebbe essere così:Come creare un quadrato magico usando Windows Form?

Image of 3x3 magic Square with numbers and grid shown.

Tuttavia, l'utente dovrebbe essere in grado di decidere le dimensioni del quadrato (3x3, 5x5, 7x7, ecc). Ho già scritto il codice in un'applicazione console, ma non so come aggiungere la grafica 2D.

Qualcuno ha già fatto questa domanda (How do I put my result into a GUI?), e una delle risposte era usare DataGridView, ma non sono sicuro se è quello che sto cercando, dal momento che non riesco a farlo sembrare l'immagine.

Qualche idea o consiglio?

+0

è possibile utilizzare un 'TableLayoutPanel' e aggiungere pulsanti al pannello in modo dinamico. –

risposta

6

È possibile utilizzare un TableLayoutPanel e aggiungere pulsanti al pannello in modo dinamico.

Se non è necessaria l'interazione con i pulsanti, è possibile aggiungere invece Label.

Creare piazza in modo dinamico:

public void CreateSquare(int size) 
{ 
    //Remove previously created controls and free resources 
    foreach (Control item in this.Controls) 
    { 
     this.Controls.Remove(item); 
     item.Dispose(); 
    } 

    //Create TableLayoutPanel 
    var panel = new TableLayoutPanel(); 
    panel.RowCount = size; 
    panel.ColumnCount = size; 
    panel.BackColor = Color.Black; 

    //Set the equal size for columns and rows 
    for (int i = 0; i < size; i++) 
    { 
     var percent = 100f/(float)size; 
     panel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent)); 
     panel.RowStyles.Add(new RowStyle(SizeType.Percent, percent)); 
    } 

    //Add buttons, if you have your desired output in an array 
    //you can set the text of buttons from your array 
    for (var i = 0; i < size; i++) 
    { 
     for (var j = 0; j < size; j++) 
     { 
      var button = new Button(); 
      button.BackColor = Color.Lime; 
      button.Font = new Font(button.Font.FontFamily, 20, FontStyle.Bold); 
      button.FlatStyle = FlatStyle.Flat; 

      //you can set the text of buttons from your array 
      //For example button.Text = array[i,j].ToString(); 
      button.Text = string.Format("{0}", (i) * size + j + 1); 
      button.Name = string.Format("Button{0}", button.Text); 
      button.Dock = DockStyle.Fill; 

      //If you need interaction with buttons 
      button.Click += b_Click; 
      panel.Controls.Add(button, j, i); 
     } 
    } 
    panel.Dock = DockStyle.Fill; 
    this.Controls.Add(panel); 
} 

Se avete bisogno di interazione con i tasti

void button_Click(object sender, EventArgs e) 
{ 
    var button = (Button)sender; 
    //Instead put your logic here 
    MessageBox.Show(string.Format("You clicked {0}", button.Text)); 
} 

A titolo di esempio, è possibile chiamare

CreateSquare(3); 

Screenshot:

enter image description here

+0

Grazie! Questo funziona perfettamente per un quadrato 3x3. Tuttavia, quando la dimensione è diversa (5x5, 7x7) ottengo risultati diversi. Il "ciclo for" sembra ok, quindi non so perché sto ottenendo quell'output. – Jack

+0

Il mio male. Mostra correttamente i numeri, ho solo dovuto ingrandire la finestra per vedere l'intero numero (invece di vedere "24" vedrei solo "2"). Grazie ancora! – Jack

+0

@Jack Sei il benvenuto, Sì, puoi ingrandire la finestra o ridurne il carattere :) –

3

È possibile creare un modulo e aggiungere un TableLayoutPanel con questa proprietà

tableLayoutPanel1.Dock = DockStyle.Fill; 
tableLayoutPanel1.BackColor = Color.Gold; 

e questo è il risultato

enter image description here

Quando si crea riga e colonna, per adattarsi correttamente impostare la percentuale in questo modo:

enter image description here

Dopo questo è possibile aggiungere un pulsante o un'etichetta in ogni quadrato.

enter image description here