저는 C#에서 극단적 인 초보자입니다. 그러나 헤드 스타트 C# 튜토리얼 북을 천천히 살펴 보았습니다. 그러나 첫 번째 "실험실"과제에서 벽에 부딪혔습니다. PictureBox를 제어하는 코드를 제공하고 기본 폼에서 작동하도록 코드를 가져올 수는 있지만 클래스 내에서 작동하도록 할 수는 없습니다. . 나는 이전 레슨을 마치고 돌아 왔고, 나는 실종 된 것에 대해 상당히 좋은 생각을 가지고있다. 그러나 내 인생에서 나는 내 수업 내에서 메인 폼의 PictureBox에 액세스하는 방법을 알아낼 수 없다. 튜토리얼에서 나에게해야한다고 말하고 있기 때문에).C# Newbie 튜토리얼 북에서 질문 : "헤드 스타트 C# 그레이하운드 랩"
나는이 책에서 전혀 뛰어 들지 않았기 때문에 조금 실망 스럽지만, 아직 우리가 이것을 다루지 않았다고 맹세하겠다. 어쨌든, 진짜 프로그래머들에게 호소력이 있습니다.
자습서의 코드 "코드를 사용하여 개체가 양식에서 제어 할 수 있습니다"(책이있는 모든 사용자를위한 p208)입니다.
Point p = MyPictureBox.Location
p.x += distance;
MyPictureBox.Location = p
아래에서 관련 코드를 게시 할 예정입니다. 아래 코드를 사용하십시오. Button1은 컴파일 할 때 작동합니다. Button2는 "작동합니다"라는 의미에서 현재 클래스는 전달 된 INT를 인쇄하도록 지시합니다. 왜냐하면 내가 작동하지 못하는 코드를 주석 처리했기 때문입니다.
미리 감사드립니다. Form1에 대한
코드 : PicMover 클래스
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
// Namespaces I'll need.
namespace Troubleshooting_PicBoxes
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent(); // Start all the Form1 stuff (all IDE-generated)
}
private void button1_Click(object sender, EventArgs e) //method from clicking the first button
{
int distance = 5; // Create this variable called "distance"
Point BoxMovement = MyPictureBox.Location; //create a point called BoxMovement
BoxMovement.X += distance; // Adjust the X of BoxMovement by my distance int.
MyPictureBox.Location = BoxMovement; // now adjust the Box by the Point's location.
}
private void button2_Click(object sender, EventArgs e)
{
PicMover PicMoverObject1 = new PicMover(); // Reserve Space for&Create object
PicMoverObject1.MoveThatPic(5); // Execute Object Method with a value of 5
}
}
}
코드 : 당신이 뭔가에 액세스해야하는 경우
//
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Troubleshooting_PicBoxes
{
class PicMover
{
public void MoveThatPic(int distance) // New method,
// takes a variable called Distance.
{
MessageBox.Show(distance.ToString()); // Just show us that Variable.
// I need to be able to access Form1's picture box before I can use this. :(
/* Point BoxMovement = MyPictureBox.Location; //create a point called BoxMovement
BoxMovement.X += distance; // Adjust the X of that by distance.
MyPictureBox.Location = BoxMovement; // now adjust the Box by the Point's location.
*/
}
}
}
그랬어! 나는 INT 변수처럼 PictureBox 객체를 전달할 수 있다는 것을 깨닫지 못했지만 두 객체 모두라는 것을 깨달았어야합니다. 감사합니다. :) – user236494