0

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 the 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?

1
  • You'll need to provide more details such as what is MyController? Commented Oct 7, 2011 at 17:05

2 Answers 2

1

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);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

This changes the order of the rows in the collection, but not their positions on the UI. It would only work if _myController was the data source for a ListView or something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.