C#에서 구조체 관련해서 질문 드립니다

etfrind의 이미지

class Board
{
Form1 frm;
public Board(Form1 frm)
{
this.frm = frm;
}

public struct stone
{
public string content;
public int sX;
public int sY;
public bool stand;
public PictureBox gostone;
}

public void makegame(stone[,] game)
{
int i, j;
Bitmap blackImage, whiteImage;
blackImage = new Bitmap(바둑게임.Properties.Resources.blackStone);
whiteImage = new Bitmap(바둑게임.Properties.Resources.whiteStone);

for (i = 0; i < 19; i++)
{
for (j = 0; j < 19; j++)
{
if ((i + j) % 2 == 0)
{
game[i, j].content = "black";
game[i, j].sX = 3 + 26 * j;
game[i, j].sY = 3 + 26 * i;
game[i, j].stand = true;

game[i, j].gostone = new PictureBox();
game[i, j].gostone.SizeMode = PictureBoxSizeMode.StretchImage;
game[i, j].gostone.Location = new Point(game[i, j].sX, game[i, j].sY);
game[i, j].gostone.Size = new Size(26, 26);
game[i, j].gostone.BackColor = Color.FromArgb(220, 178, 92);
game[i, j].gostone.Image = blackImage;
game[i, j].gostone.Click += frm.stone_Click;
game[i, j].gostone.Parent = frm.goBoard;
}
else if ((i + j) % 2 != 0)
{
game[i, j].content = "white";
game[i, j].sX = 3 + 26 * j;
game[i, j].sY = 3 + 26 * i;
game[i, j].stand = true;

game[i, j].gostone = new PictureBox();
game[i, j].gostone.SizeMode = PictureBoxSizeMode.StretchImage;
game[i, j].gostone.Location = new Point(game[i, j].sX, game[i, j].sY);
game[i, j].gostone.Size = new Size(26, 26);
game[i, j].gostone.BackColor = Color.FromArgb(220, 178, 92);
game[i, j].gostone.Image = whiteImage;
game[i, j].gostone.Click += frm.stone_Click;
game[i, j].gostone.Parent = frm.goBoard;
}
}
}
}

이렇게 구조체 형태랑 배열에 넣는 함수를 만들었구요

public partial class Form1 : Form
{
Board.stone[,] game = new Board.stone[19, 19];
ArrayList playgame = new ArrayList();

public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{
Board newgame = new Board(this);
newgame.makegame(game);
}

public void stone_Click(object sender, EventArgs e)
{
PictureBox temp = (PictureBox)sender;
temp.Dispose();
}

}

여기서 stone_Click 함수를 구조체 안의 picturebox의 클릭 이벤트로 연결해서 사용하려고 합니다.
그런데 이 함수에서 sender를 이용해서 해당 picturebox를 지우는 거는 성공 했는데요.
picturebox를 지우기 전에 picturebox가 속하는 구조체를 arraylist에 넣고싶은데요 함수안에서 구조체가 배열의 어디에 있는지 구하거나 해당하는 구조체를 그대로 arraylist에 직접 삽입하는 방법은 없을까요?

mirheekl의 이미지

프로그램의 용도는 잘 모르겠지만.. 일단 간단한 방법이라면

픽처박스를 상속해서 새 클래스를 만든 다음 거기에다가 말씀하신 구조체의 레퍼런스를 저장하는 방법이 있겠네요. 그러면 stone_click에서 그 새 클래스로 캐스팅하면 구조체의 레퍼런스에도 접근 가능하겠죠. 아예 그 새 클래스에다 stone구조체를 합쳐버릴 수도 있겠네요.

--

etfrind의 이미지

바둑판과 바둑돌로 하는 일종의 게임인데 구조체 배열로 바둑돌의 위치를 저장하려고 2차원 배열을 사용하려고 한거였습니다.
픽쳐박스를 클릭하면 배열의 위치들로 조건을 걸어서 바둑돌을 지울수 있다 없다를 판단할 생각이었는데 문제가 해결이 안되네요 한 두세번 통째로 갈아없은듯 하네요 ㅠㅠ

그리고 제가 상속쪽은 약해서 새클래스를 만들어서 stone구조체를 합친다는게 어떤건지 잘모르겠어요 ㅠㅠ

mirheekl의 이미지

GUI는 조금 천천히 하셔도 되고요.
콘솔프로그램 예제를 통해서 OOP기본개념부터 익혀보시는게 어떨까 합니다.

--

etfrind의 이미지

만약에 picturebox[,] temp 처럼 picturebox를 2차원배열로 만들고 임의의 picturebox하나를 클릭 했을때 해당하는 picturebox의 배열안에서의 위치를 구하는 방법은 없을까요?

mirheekl의 이미지

현재 클래스가 원하는 정보를 담지 못한다면 그것을 확장해서 원하는 정보를 담든지, 아니면 스태틱을 이용해서 아무 위치에서나 해당 배열을 접근 가능하게 만들든지 하는 방법 등등이 존재합니다. 이 부분에 자신이 없으신 듯 해서 GUI를 하기 전에 다른 것을 좀 더 연습하시면 좋겠다는 답변도 드렸습니다.

--

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.