开发者

Changing location values of Form Objects in a List Array

开发者 https://www.devze.com 2023-04-11 06:26 出处:网络
I have a winform and in it I have a List of MyController Object. List<MyController> _myController = new List <MyController>();

I have a winform and in it I have a List of MyController Object.

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

This mycontroller object contains 1 checkbox 4 textbox and 1 button on each line.

What I want is when I click a button in a row, I want that whole row to move up and the row on the upper side will automatically move to down.

How can I write a code for that in C#?

In the buttonClick function I tried th开发者_如何学Goe following but apparently it does not work:

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;

        }
    }

probably I need to do this change via pointers and references. But how can I reflect this change in my active form?


You're changing the order in the list, but not the relative locations of the two rows in your UI. The order of controls in a collection is near-meaningless for most UI objects, unless the order is specifically used to define location (such as if the collection were the DataSource of a ListBox or similar control).

What you'll need to do is swap the Y-coordinates of each instance of MyController, or their contained controls, in addition to the controls themselves. It would be very easy if MyController were derived from UserControl, or otherwise had its own drawing area within which the children were positioned:

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);
    }
}


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号