2011-09-27 8 views
8

의 구조체에 대한 참조를 만드는 방법 LineShape 컨트롤과 사용자 지정 컨트롤 (기본적으로 Label이있는 PictureBox)이 있습니다.내 응용 프로그램에서 C#

사용자 지정 컨트롤의 위치에 따라 LineShape에서 포인트 좌표 중 하나를 변경하려고합니다.

사용자 지정 컨트롤 내에서 LineShape 지점에 대한 참조를 설정하고 참조 지점 좌표를 변경하는 위치 변경 이벤트 처리기를 추가하는 아이디어가있었습니다.

그러나 내장 된 Point는 값 유형 인 구조체이므로 작동하지 않습니다. 누구나 아이디어를 가지고, 구조에 대한 참조를 만드는 방법 또는 어쩌면 누군가 내 문제에 대한 해결 방법을 알고 있습니까?

nullable 유형의 사용과 관련하여 해결책을 시도했지만 여전히 작동하지 않습니다.

private Point? mConnectionPoint; 

그리고 위치 변경 이벤트 핸들러의 구현 :

private void DeviceControl_LocationChanged(object sender, EventArgs e) 
{ 
    if (mConnectionPoint != null) 
    { 
     DeviceControl control = (DeviceControl)sender; 

     Point centerPoint= new Point(); 
     centerPoint.X = control.Location.X + control.Width/2; 
     centerPoint.Y = control.Location.Y + control.Height/2; 

     mConnectionPoint = centerPoint; 
    } 
} 

답변

7
당신은 추가 '심판'에 의해 참조 값 유형을 전달할 수 있습니다

여기 내 사용자 지정 컨트롤 (디바이스 컨트롤)의 필드를 정의하는 방법이있다 전에 메서드를 전달할 때. 이 같은

:

void method(ref MyStruct param) 
{ 
} 
0

정말 mConnectionPoint 멤버에 '참조'액세스를 필요로하지 않습니다 방법; 당신은 당신의 클래스의 구성원으로, 참조 된 포인트에 직접 위치 값을 지정할 수 있습니다 :이 코드에 대한 이유는 LineShape 제어를 이동하는 경우

private void DeviceControl_LocationChanged(object sender, EventArgs e) 
{ 
    if (mConnectionPoint != null) 
    { 
     DeviceControl control = (DeviceControl)sender; 

     mConnectionPoint.X = control.Location.X + control.Width/2; 
     mConnectionPoint.Y = control.Location.Y + control.Height/2; 
    } 
} 

그러나, 당신은 소스로 바로 이동합니다. 컨트롤의 속성을 변경하는 가장 좋은 방법은 컨트롤의 속성을 변경하는 것입니다.

DeviceControl control = (DeviceControl)sender; 

    line1.StartPoint = [calculate point1 coordinates]; 
    line1.EndPoint = [calculate point2 coordinates]; 
관련 문제