2017-11-30 6 views
1

N * M 사이즈 SUDOKU 게임을 만들고 있습니다. 모든 번호는 버튼에 있습니다. 프로그램 시작 모든 버튼이 비어있는 상태에서 버튼을 클릭하면 버튼 하나 하나를 선택하여 각 버튼을 하나씩 선택하면됩니다.C# 마우스의 동적 패널

private void adatB_Click(object sender, EventArgs e) 
    { 
     Button button = sender as Button; 
     int[] hely = button.Tag.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray(); 
     Panel szamok = new Panel 
     { 
      Location = MousePosition, 
      Size = new Size(100, 100) 
     }; 
     Controls.Add(szamok); 
     TableLayoutPanel minitabla = new TableLayoutPanel 
     { 
      Dock = DockStyle.Fill, 
      ColumnCount = szorzat, 
      RowCount = szorzat, 
     }; 
     for (int i = 0; i < szorzat; i++) 
     { 
      minitabla.RowStyles.Add(new RowStyle(SizeType.Percent, 100F)); 
      minitabla.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 100F)); 
     } 
     szamok.Controls.Add(minitabla); 
     Button[,] szamokB = new Button[meret[0], meret[1]]; 
     int d = 1; 
     for (int i = 0; i < meret[0]; i++) 
     { 
      for (int j = 0; j < meret[1]; j++) 
      { 
       szamokB[i, j] = new Button(); 
       szamokB[i, j].Tag= hely[0]+","+hely[1]; 
       szamokB[i, j].Text = d.ToString(); 
       szamokB[i, j].Dock = DockStyle.Fill; 
       szamokB[i, j].Click += szamokB_Click; 
       minitabla.Controls.Add(szamokB[i, j], i, j); 
       d++; 
      } 
     } 
    } 

    private void szamokB_Click(object sender, EventArgs e) 
    { 
     Button button = sender as Button; 
     int[] hely = button.Tag.ToString().Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).Select(Int32.Parse).ToArray(); 
     adatB[hely[0], hely[1]].Text = button.Text; 
    } 

버튼을 클릭하면 패널이 생성되지 않습니다. meret [0] 변수가 N이고, [1]이 M이고, adatB는 태그에있는 positons가있는 단추입니다. 번호를 선택하면 어떻게 그 패널을 닫을 수 있습니까?

답변

1

우선 마우스 위치를 올바르게 계산해야합니다. MSDN에서

:

화면 좌표에서 마우스 커서의 위치를 ​​가져옵니다.

당신이 뭔가를 사용해야 패널을 사용하면 선택기를 저장할 수 닫으려면

Controls.Add(szamok); 
szamok.BringToFront(); 

:

당신은 아마이 필요합니다
Location = new Point(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y) 

, 전면에 패널을 가지고 패널에서 제거하고 나중에 컨트롤에서 제거 할 수 있습니다.

public partial class Form1 : Form 
{ 
    private Panel myPanel = null; 

    private void adatB_Click(object sender, EventArgs e) 
    { 
     ... 

     Panel szamok = new Panel 
     { 
      Location = new Point(MousePosition.X - this.Location.X, MousePosition.Y - this.Location.Y), 
      Size = new Size(100, 100) 
     }; 

     if (this.myPanel != null) 
     { 
      this.Controls.Remove(this.myPanel); 
     } 
     this.myPanel = szamok; 

     Controls.Add(szamok); 
     szamok.BringToFront(); 

     ... 
    } 

    private void szamokB_Click(object sender, EventArgs e) 
    { 
     if (this.myPanel != null) 
     { 
      this.Controls.Remove(this.myPanel); 
      this.myPanel = null; 
     } 

     ... 
    } 
}