向下模拟CTRL,直到需要C#? [英] Simulate CTRL down until I desire in c#?

查看:48
本文介绍了向下模拟CTRL,直到需要C#?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to simulate a user pressing ctrl down, the main goal would be in a datagridview when I select something programarly (initially) I dont want the user to then change that selection if not just add on to it or subtract, just as if you were to hold ctrl + left mouse click. I have no idea where to even begin. I tried to create a selection change event conbined with logicals but that will cause an infinite loop since we would be selecting one by a user then the code change other and other etc infinitely triggering that event. Please help, I'm sort of new to coding. I also don't know how to determine whether a ctrl key has been pressed, is pressed and being held.

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

    }


推荐答案

实现此目的的方法是单独存储选择状态,当用户单击一个单元格时对其进行更新,然后重新应用它。这样可以防止每次单击时都丢失选择。如果您钩住了适当的事件处理程序(鼠标,而不是单击鼠标),则可以做到这一点而不会闪烁屏幕,否则就一团糟。

The way to make this happen is to store the selection state separately, update it when the user clicks a cell, then re-apply it. This prevents the selection from being lost every time they click. If you hook the proper event handlers (mouseup, not click) you can do this without the screen flickering and otherwise being a mess to look at.

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

Everything you need is in this class, including an extension method SetupToggledSelectionMode(), which is your entry point.

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

要使用,请填充您的gridview,设置初始以编程方式进行选择,然后像这样调用它:

To use, populate your gridview, set up the initial selection programmatically, and call it like this:

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

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

The SetupToggledSelectionMode() method will register the necessary event handlers and store the selection state of the grid in a closed variable accessible to both handlers. So you won't have to declare anything additional; just call the method.

这篇关于向下模拟CTRL,直到需要C#?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆