在 c# 中模拟 CTRL 直到我想要为止



我正在尝试模拟用户按下ctrl,当我以编程方式选择某些内容时,主要目标是在datagridview中(最初(我不希望用户然后更改该选择,如果不是只是添加或减去,就像您按住ctrl +鼠标左键单击一样。我什至不知道从哪里开始。我试图创建一个与逻辑相结合的选择更改事件,但这会导致无限循环,因为我们将由用户选择一个,然后代码更改其他和其他等无限触发该事件。请帮忙,我是编码新手。我也不知道如何确定 ctrl 键是否已按下、按下并被按住。

        private void selecttionh(object sender, EventArgs e)
    {
        if (stage == "4A" || stage == "3B" && ModifierKeys.HasFlag(Keys.Control))
        {
            int nothing = 0;
            btnclickercl bt = new btnclickercl();
            bt.dataGridView1_SelectionChanged(sender, e, dataGridViewReslist, dataGridViewnewres, nothing);
        }
        if (stage == "4A" || stage == "3B" && (ModifierKeys & Keys.Control) != Keys.Control)
        {
            MessageBox.Show("Please Press and hold " + "'ctrl'" + " to continue");
            dataGridViewReslist.ClearSelection();
            for (int i = 0; i < ResRoomSelections.Count; i++)
            {
                dataGridViewReslist.Rows[ResRoomSelections[i][0]].Cells[ResRoomSelections[i][1]].Selected = true;
                dataGridViewReslist.Rows[ResRoomSelections[i][0]].Cells[(ResRoomSelections[i][1]) + 1].Selected = true;
            }
        }
        else
        {
                dataGridViewReslist.ClearSelection();
                for (int i = 0; i < ResRoomSelections.Count; i++)
                {
                    dataGridViewReslist.Rows[ResRoomSelections[i][0]].Cells[ResRoomSelections[i][1]].Selected = true;
                    dataGridViewReslist.Rows[ResRoomSelections[i][0]].Cells[(ResRoomSelections[i][1]) + 1].Selected = true;
                }
        }  
    }

实现此目的的方法是单独存储选择状态,在用户单击单元格时更新它,然后重新应用它。这可以防止每次单击时丢失所选内容。如果您挂接正确的事件处理程序(鼠标向上,而不是单击(,则可以执行此操作,而不会使屏幕闪烁,否则看起来一团糟。

您需要的一切都在此类中,包括扩展方法SetupToggledSelectionMode(),这是您的入口点。

static public class Example
{
    static private bool[][] GetSelectionState(DataGridView input)
    {
        int rowCount = input.Rows.Count;
        int columnCount = input.Columns.Count;
        var result = new bool[rowCount][];
        for (var r = 0; r < rowCount; r++)
        {
            result[r] = new bool[columnCount];
            for (var c = 0; c < columnCount; c++)
            {
                var cell = input.Rows[r].Cells[c];
                result[r][c] = cell.Selected;
            }
        }
        return result;
    }
    static private void SetSelectionState(DataGridView input, bool[][] selectionState)
    {
        for (int r = 0; r <= selectionState.GetUpperBound(0); r++)
        {
            for (int c = 0; c <= selectionState[r].GetUpperBound(0); c++)
            {
                input.Rows[r].Cells[c].Selected = selectionState[r][c];
            }
        }
    }

    static public void SetupToggledSelectionMode(this DataGridView input)
    {
        bool[][] selectionState = GetSelectionState(input);  //This will be stored in a closure due to the lambda expressions below
        input.CellMouseUp += (object sender, DataGridViewCellMouseEventArgs e) =>
        {
            selectionState[e.RowIndex][e.ColumnIndex] = !selectionState[e.RowIndex][e.ColumnIndex];
            SetSelectionState(input, selectionState);
        };
        input.SelectionChanged += (object sender, EventArgs e) =>
        {
            if (selectionState != null)
            {
                SetSelectionState(input, selectionState);
            }
        };
    }
}

若要使用,请填充网格视图,以编程方式设置初始选择,然后按如下所示调用它:

myDataGrid.DataSource = myData;
myDataGrid.Refresh();
myDataGrid.SelectAll();
myDataGrid.SetupToggledSelectionMode();

SetupToggledSelectionMode() 方法将注册必要的事件处理程序,并将网格的选择状态存储在两个处理程序都可以访问的封闭变量中。因此,您不必声明任何其他内容;只需调用该方法。

谢谢你,这真的帮助了我。 我所做的只是让它更有效率。由于它会在每一步都调用选择更改,所以我完全摆脱了该事件,只保留了 CellMouseup 事件。

static private bool[][] GetSelectionState(DataGridView input)
{
    int rowCount = input.Rows.Count;
    int columnCount = input.Columns.Count;
    var result = new List<int[]>();
    for (var r = 0; r < rowCount; r++)
    {
        for (var c = 0; c < columnCount; c++)
        {
            if(input.Rows[r].Cells[c].Selected==true)
           {
            result.add(new int[]{r,c});//will keep only the integer of selected items
           }
        }
    }
    return result;//this for me was a recycled variable it can be used or recycled from somewhere else
}
private void SetSelectionState(DataGridView input,result)
    {
        for (int i=0;i<result.Count;i++)
        {
            input.Rows[result[i][0]].Cells[result[i][1]].Selected = true;
        }
    }
public void SetupToggledSelectionMode(DataGridView input,result)
    {
     for (int i=0;i<result.Count;i++)
            {
                if(result[i].SequenceEqual(new int[] { e.RowIndex, e.ColumnIndex }))
                {
                    result.RemoveAt(i);
                    continueer = 1;
                    break;
                }
            }
            if (continueer == 0)
            {
                ResRoomSelections.Add(new int[] { e.RowIndex, e.ColumnIndex });
            }
        SetSelectionState(input);
         //whatever else you need to do 
 }

我知道还有更好的方法来搜索列表,但我无法让 lamda 搜索工作,所以我只是使用了蛮力

-谢谢吴约翰

·

最新更新