Beispiel #1
0
        private void listBox1_KeyUp(object sender, KeyEventArgs e)
        {
            if ((!e.Control || e.KeyCode != Keys.F || e.Shift || e.Alt) &&
                (e.Control || e.KeyCode != Keys.F3 || e.Alt) &&
                (!e.Control || e.KeyCode != Keys.C || e.Shift || e.Alt))
            {
                e.Handled = false;
                return;
            }

            //copy
            if (e.Control && e.KeyCode == Keys.C)
            {
                Clipboard.SetText(listBox1.SelectedItem?.ToString() ?? "");

                e.Handled = true;
                return;
            }

            var r = listBox1.SelectedIndex;

            if (lastRowIndex > 0)
            {
                lastRowIndex = r;
            }

            if (e.KeyCode == Keys.F || (e.KeyCode == Keys.F3 && searchtext.Length == 0))
            {
                var dlg = new SearchFrm();
                dlg.Input         = searchtext;
                dlg.StartPosition = FormStartPosition.CenterParent;
                //show input dialog
                if (dlg.ShowDialog(this) == DialogResult.OK)
                {
                    searchtext = dlg.Input;
                }
                else
                {
                    e.Handled = true;
                    return;
                }
            }

            string row;

            if (e.KeyCode == Keys.F3 && e.Shift)
            {
                row = listBox1.Items.Cast <string>().LastOrDefault(x => x.IndexOf(searchtext, StringComparison.InvariantCultureIgnoreCase) >= 0 && listBox1.Items.IndexOf(x) < lastRowIndex);
            }
            else
            {
                row = listBox1.Items.Cast <string>().FirstOrDefault(x => x.IndexOf(searchtext, StringComparison.InvariantCultureIgnoreCase) >= 0 && listBox1.Items.IndexOf(x) > lastRowIndex);
            }

            if (row != null)
            {
                listBox1.ClearSelected();
                listBox1.SelectedIndex = listBox1.Items.IndexOf(row);
                lastRowIndex           = listBox1.SelectedIndex;
            }
            else
            {
                lastRowIndex = 0;
            }

            e.Handled = true;
        }
        private void dataGridView1_KeyUp(object sender, KeyEventArgs e)
        {
            if ((!e.Control || e.KeyCode != Keys.F || e.Shift || e.Alt) && (e.Control || e.KeyCode != Keys.F3 || e.Alt))
            {
                e.Handled = false;
                return;
            }

            var b = dataGridView1.SelectedCells;

            if (b != null && b.Count > 0)
            {
                var r = b[0].RowIndex;
                var c = b[0].ColumnIndex;

                if (lastRowIndex > 0)
                {
                    lastRowIndex = r;
                }

                if (e.KeyCode == Keys.F || (e.KeyCode == Keys.F3 && searchtext.Length == 0))
                {
                    var dlg = new SearchFrm();
                    dlg.Input         = searchtext;
                    dlg.StartPosition = FormStartPosition.CenterParent;
                    //show input dialog
                    if (dlg.ShowDialog(this) == DialogResult.OK)
                    {
                        searchtext = dlg.Input;
                    }
                    else
                    {
                        e.Handled = true;
                        return;
                    }
                }

                DataGridViewRow row;

                if (e.KeyCode == Keys.F3 && e.Shift)
                {
                    row = dataGridView1.Rows.Cast <DataGridViewRow>().LastOrDefault(x => x.Cells[c].Value.ToString().IndexOf(searchtext, StringComparison.InvariantCultureIgnoreCase) >= 0 && x.Index < lastRowIndex);
                }
                else
                {
                    row = dataGridView1.Rows.Cast <DataGridViewRow>().FirstOrDefault(x => x.Cells[c].Value.ToString().IndexOf(searchtext, StringComparison.InvariantCultureIgnoreCase) >= 0 && x.Index > lastRowIndex);
                }

                if (row != null)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.FirstDisplayedScrollingRowIndex = row.Index;
                    row.Cells[c].Selected = true;
                    lastRowIndex          = row.Index;
                }
                else
                {
                    lastRowIndex = 0;
                }
            }

            e.Handled = true;
        }