2011-10-07 3 views
0

winform이 있고 목록에 MyController 객체 목록이 있습니다.목록 배열에서 양식 객체의 위치 값 변경

List<MyController> _myController = new List <MyController>(); 

이 mycontroller 개체에는 각 줄마다 1 개의 확인란과 4 개의 텍스트 상자와 1 개의 버튼이 있습니다.

내가 원하는 것은 행에있는 버튼을 클릭하면 전체 행이 위로 이동하고 위쪽에있는 행이 자동으로 아래쪽으로 이동하기를 원합니다.

어떻게 C#에서 코드를 작성할 수 있습니까?

private void downButton_Click(object sender, EventArgs e) 
    { 
     string NameSet = (sender as Button).Name.Split(new char[] { '_' })[1]; 
     int itemNo = Int32.Parse(NameSet); 
     MyControls tempObj = new MyControls(); 
     if (itemNo>0) 
     { 
     tempObj = _myControls[itemNo]; 
     _myControls[itemNo] = _myControls[itemNo - 1]; 
     _myControls[itemNo - 1] = tempObj; 

     } 
    } 

아마도 나는 포인터와 참조를 통해이 변경을 할 필요가 다음 buttonClick 기능에서

나는 다음과 같은하지만 분명히 작동하지 않습니다했습니다. 그러나이 변화를 나의 적극적인 형태로 어떻게 반영 할 수 있습니까?

+0

당신은 자세한 내용을 제공해야합니다 같은 것입니다 : MyController에이 UserControl에서 파생 된, 또는 그렇지 않으면 아이들이 배치 된 내 자신의 드로잉 영역을 가지고 있다면 그것은 매우 쉬운 것입니다? –

답변

1

목록의 순서는 변경되지만 UI의 두 행의 상대적 위치는 변경하지 않습니다. 컬렉션이 ListBox 또는 유사한 컨트롤의 DataSource 인 경우와 같이 위치를 정의하는 데 특히 순서를 사용하지 않는 한 컬렉션의 컨트롤 순서는 대부분의 UI 개체에 거의 무의미합니다.

당신이해야 할 일은 컨트롤 자체와 함께 MyController의 각 인스턴스 또는 포함 된 컨트롤의 Y 좌표를 바꾸는 것입니다. MyController에 어떤

private void downButton_Click(object sender, EventArgs e) 
{ 
    string NameSet = (sender as Button).Name.Split(new char[] { '_' })[1]; 
    int itemNo = Int32.Parse(NameSet); 
    if (itemNo>0) 
    { 
     //swap row locations 
     var temp = _myControls[itemNo-1].Y; 
     _myControls[itemNo-1].Y = _myControls[itemNo].Y; 
     _myControls[itemNo].Y = temp; 
     //swap list order 
     var tempObj = _myControls[itemNo]; 
     _myControls.RemoveAt(itemNo); 
     _myControls.Insert(tempObj, itemNo-1); 
    } 
} 
+0

고마워 친구. –

0
public void MoveItemUp(int index) { 
    MyController c = _myController[index]; 
    _myController.RemoveAt(index); 
    _myController.Insert(index - 1, c); 
} 

public void MoveItemDown(int index) { 
    MyController c = _myController[index]; 
    _myController.RemoveAt(index); 
    _myController.Insert(index + 1, c); 
} 
+0

컬렉션의 행 순서는 변경하지만 UI의 위치는 변경하지 않습니다. _myController가 ListView 또는 뭔가에 대한 데이터 소스 인 경우에만 작동합니다. – KeithS