Пример #1
0
        void txtAddress_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string id = addressBox.Text;

                // avoid to directly use trim, it will create new string even nothing to be trimmed
                if (id.StartsWith(" ") || id.EndsWith(" "))
                {
                    id = id.Trim();
                }

                if (RangePosition.IsValidAddress(id))
                {
                    this.worksheet.SelectionRange = new RangePosition(id);
                    workbook.Focus();
                }
                else if (RGUtility.IsValidName(id))
                {
                    var refRange = this.worksheet.GetNamedRange(id);

                    if (refRange != null)
                    {
                        this.worksheet.SelectionRange = refRange;
                        workbook.Focus();
                    }
                    else
                    {
                        try
                        {
                            this.worksheet.DefineNamedRange(id, this.worksheet.SelectionRange);
                            workbook.Focus();
                        }
                        catch (NamedRangeAlreadyDefinedException)
                        {
                            // should be not reached
                            MessageBox.Show("Another range with same name does already exist.");
                        }
                    }
                }
            }
            else if (e.KeyCode == Keys.Down)
            {
                PushDown();
            }
            else if (e.KeyCode == Keys.Escape)
            {
                workbook.Focus();
            }
        }