簡體   English   中英

向下模擬CTRL直到我想要使用C#?

[英]Simulate CTRL down until I desire in c#?

我正在嘗試模擬一個用戶按下ctrl,主要目的是當我以編程方式選擇某個對象時(最初),我不希望用戶隨后更改該選擇,如果不只是添加或減去它,就像如果要按住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);
            }
        };
    }
}

要使用,請填充您的gridview,以編程方式設置初始選擇,然后按以下方式調用它:

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

SetupToggledSelectionMode()方法將注冊必要的事件處理程序,並將網格的選擇狀態存儲在兩個處理程序均可訪問的封閉變量中。 因此,您無需聲明其他任何內容; 只需調用該方法即可。

謝謝你,這真的幫了我。 我所做的就是提高效率。因為它將在每個步驟中調用Selection進行更改,所以我完全擺脫了該事件,只保留了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搜索來工作,所以我只是使用蠻力

-謝謝吳John

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM