private void buttonAction_Click(object sender, System.EventArgs e)
        {
            if (((Control)sender).Tag == null)
            {
                return;
            }

            ItemIndices indices = (ItemIndices)(((Control)sender).Tag);

            OnButtonClick(new ButtonClickedEventArgs(textBox.Text, indices.ItemIndex, indices.SubItemIndex));
            textBox.Focus();
        }
        private void listView_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ItemIndices indices = GetSubitemIndexAt(e.X, e.Y);

            if (indices.IsValid)
            {
                if (IsEditable(indices.SubItemIndex))
                {
                    Cursor.Current = (this.SelectedIndex == indices.ItemIndex)?selectedCursor:editableCursor;
                }
            }
        }
 public void SetFieldData(string text, int rowIndex, int colIndex)
 {
     if (this.textBox.Tag != null)
     {
         ItemIndices indices = (ItemIndices)(this.textBox.Tag);
         if ((indices.ItemIndex == rowIndex) && (indices.SubItemIndex == colIndex))
         {
             textBox.Text = text;
             OnModifiedChanged(new ModifiedChangedEventArgs(textBox.Text, indices.ItemIndex, indices.SubItemIndex));
             return;
         }
     }
     this.listView.Items[rowIndex].SubItems[colIndex].Text = text;
 }
        private void StartEdit(ItemIndices indices)
        {
            // nothing to edit here
            if (!IsEditable(indices.SubItemIndex))
            {
                return;
            }

            Rectangle bounds = GetSubitemRectangle(indices.ItemIndex, indices.SubItemIndex);

            // take x from header if on first item (getsubitem returns bounds of whole item)
            if (indices.SubItemIndex == 0)
            {
                Rectangle header = GetHeaderRectangle(indices.SubItemIndex);
                bounds.X     = header.X;
                bounds.Width = header.Width;
            }

            // don't edit if textbox overlaps listview
            if (!this.listView.ClientRectangle.Contains(bounds))
            {
                return;
            }

            if (HasButton(indices.SubItemIndex))
            {
                // make room for the button
                bounds.Width -= GetButtonWidth(indices.SubItemIndex);
            }

            this.textBox.Bounds         = bounds;
            this.textBox.Text           = this.listView.Items[indices.ItemIndex].SubItems[indices.SubItemIndex].Text;
            this.textBox.Modified       = false;
            this.textBox.Tag            = indices;
            this.textBox.SelectionStart = this.textBox.Text.Length;

            if (HasButton(indices.SubItemIndex))
            {
                this.button.Location = new Point(this.textBox.Left + this.textBox.Width, this.textBox.Top);
                this.button.Height   = this.textBox.Height;
                this.button.Width    = GetButtonWidth(indices.SubItemIndex);
                this.button.Text     = GetButtonText(indices.SubItemIndex);
                this.button.Tag      = indices;
            }
        }
        private void listView_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
        {
            ItemIndices indices = GetSubitemIndexAt(e.X, e.Y);

            if (!indices.IsValid)
            {
                return;
            }

            if (indices.ItemIndex != this.SelectedIndex)
            {
                return;
            }

            if (this.textBox.Tag != null)
            {
                return;
            }

            StartEdit(indices);
        }
 private void NextIndex(ref ItemIndices indices, bool previous)
 {
     indices.SubItemIndex += previous?-1:1;
     if (indices.SubItemIndex >= (this.listView.Items[indices.ItemIndex].SubItems.Count))
     {
         indices.SubItemIndex = 0;
         indices.ItemIndex++;
     }
     else if (indices.SubItemIndex < 0)
     {
         indices.SubItemIndex = this.listView.Items[indices.ItemIndex].SubItems.Count - 1;
         indices.ItemIndex--;
     }
     if (indices.ItemIndex >= this.listView.Items.Count)
     {
         indices.ItemIndex = 0;
     }
     if (indices.ItemIndex < 0)
     {
         indices.ItemIndex = this.listView.Items.Count - 1;
     }
 }
        private void textBox_Leave(object sender, System.EventArgs e)
        {
            if (this.textBox.Tag == null)
            {
                return;
            }

            // don't hide editbox and button if focus is on button
            if (button.Visible && button.Bounds.Contains(this.listView.PointToClient(Cursor.Position)))
            {
                return;
            }

            ItemIndices indices = (ItemIndices)(this.textBox.Tag);

            this.listView.Items[indices.ItemIndex].SubItems[indices.SubItemIndex].Text = textBox.Text;
            if (textBox.Modified)
            {
                OnModifiedChanged(new ModifiedChangedEventArgs(textBox.Text, indices.ItemIndex, indices.SubItemIndex));
            }
            this.button.Visible = this.textBox.Visible = false;
            this.button.Tag     = this.textBox.Tag = null;
        }
        private void textBox_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                if (e.Alt || e.Control || e.Shift)
                {
                    return;
                }

                this.button.Tag = this.textBox.Tag = null;
                textBox.Visible = false;
                button.Visible  = false;
                this.listView.Focus();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Enter)
            {
                if (e.Alt || e.Control || e.Shift)
                {
                    return;
                }

                textBox.Visible = false;
                button.Visible  = false;
                this.listView.Focus();
                e.Handled = true;
            }
            else if (e.KeyCode == Keys.Tab)
            {
                if (e.Alt || e.Control)
                {
                    return;
                }

                if (this.textBox.Tag == null)
                {
                    return;
                }

                // copy from textbox to listview
                ItemIndices indices = (ItemIndices)(this.textBox.Tag);
                this.listView.Items[indices.ItemIndex].SubItems[indices.SubItemIndex].Text = textBox.Text;
                if (textBox.Modified)
                {
                    OnModifiedChanged(new ModifiedChangedEventArgs(textBox.Text, indices.ItemIndex, indices.SubItemIndex));
                }

                // advance to next editable field
                do
                {
                    NextIndex(ref indices, e.Shift);
                } while (!IsEditable(indices.SubItemIndex));

                StartEdit(indices);

                this.button.Visible = HasButton(indices.SubItemIndex);                    // ==1);
            }
            else if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
            {
                // copy from textbox to listview
                ItemIndices indices = (ItemIndices)(this.textBox.Tag);
                this.listView.Items[indices.ItemIndex].SubItems[indices.SubItemIndex].Text = textBox.Text;
                if (textBox.Modified)
                {
                    OnModifiedChanged(new ModifiedChangedEventArgs(textBox.Text, indices.ItemIndex, indices.SubItemIndex));
                }

                // move editable field up or down
                indices.ItemIndex += (e.KeyCode == Keys.Up)?-1:1;
                if (indices.ItemIndex >= this.listView.Items.Count)
                {
                    indices.ItemIndex = 0;
                }
                if (indices.ItemIndex < 0)
                {
                    indices.ItemIndex = this.listView.Items.Count - 1;
                }

                StartEdit(indices);
            }
        }