Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

CellEditing Sample (C#)

This sample illustrates cell editing using edit controls and combo boxes, cell navigation, uneditable cells, checkbox cell image.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\CellEditing\Form1.cs or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\CellEditing\Form1.cs (on 32-bit Windows versions).


        private void Command1_Click(object sender, System.EventArgs e) {
            Application.Exit();
        }

        private void Form1_Load(object sender, System.EventArgs e) {
            int ItemIndex;

            ItemIndex = axSftTree1.Items.Add("Click on a cell to edit");
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Use Tab/Return keys";
            axSftTree1.get_Cell(ItemIndex, 2).Text = "Use arrow keys";

            ItemIndex = axSftTree1.Items.Add("This sample supports cell navigation");
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Ctrl+Home and Ctrl+End";

            int i;
            SftTreeItem Item;
            SftTreeCell Cell;

            for (i = 1 ; i < 50 ; ++i) {

                ItemIndex = axSftTree1.Items.Add("An item");
                axSftTree1.get_Cell(ItemIndex, 1).Text = "2nd Column";
                axSftTree1.get_Cell(ItemIndex, 2).Text = "3rd Column";

                ItemIndex = axSftTree1.Items.Add("Another item");
                Item = axSftTree1.get_Item(ItemIndex);
                Item.Level = 1;
                Item.get_Cell(1).Text = "2nd Column";
                Item.get_Cell(2).Text = "3rd Column";

                ItemIndex = axSftTree1.Items.Add("This item can't be edited");
                Item = axSftTree1.get_Item(ItemIndex);
                Item.Level = 2;
                Item.EditIgnore = true;
                Item.get_Cell(0).Image.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                Item.get_Cell(1).Text = "2nd Column (can't edit this item)";
                Item.get_Cell(2).Text = "3rd Column (can't edit this item)";

                ItemIndex = axSftTree1.Items.Add("A fourth item");
                Item = axSftTree1.get_Item(ItemIndex);
                Item.Level = 1;
                Cell = axSftTree1.get_Cell(ItemIndex, 1);
                Cell.Text = "This cell can't be edited";
                Cell.EditIgnore = true;
                Cell.Image.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                axSftTree1.get_Cell(ItemIndex, 2).Text = "3rd Column";

            }

            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.RowHeaders.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
        }

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemClickEvent e) {
            SftPictureObject Img;
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeCellText)
                // User clicked on a cell
                axSftTree1.get_Cell(e.itemIndex, e.colIndex).Edit(0, 0);
            else if (area == SftTreeAreaTypeConstants.constSftTreeCellGraphic) {
                // check if check box - toggle
                Img = axSftTree1.get_Cell(e.itemIndex, e.colIndex).Image;
                if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxNo)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                else if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxYes)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxNo;
            }
        }

        private void axSftTree1_ItemDblClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemDblClickEvent e) {
            SftPictureObject Img;
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeColumnRes)
                axSftTree1.get_Column(e.colIndex).MakeOptimal();
            else if (area == SftTreeAreaTypeConstants.constSftTreeCellGraphic) {
                // check if check box - toggle
                Img = axSftTree1.get_Cell(e.itemIndex, e.colIndex).Image;
                if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxNo)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxYes;
                else if (Img.Appearance == SftPictureImageConstants.sftImageCheckboxYes)
                    Img.Appearance = SftPictureImageConstants.sftImageCheckboxNo;
            }
        }

        private void axSftTree1_ToolTipVScroll(object sender, AxSftTreeLib75._DSftTreeEvents_ToolTipVScrollEvent e) {
            e.text = "Item " + e.itemIndex.ToString() + " - " + e.text;
        }

        private void axSftTree1_EditAllowed(object sender, AxSftTreeLib75._DSftTreeEvents_EditAllowedEvent e) {
            // Last chance to suppress cell editing for a cell
            //if (e.itemIndex == 1 && e.colIndex == 1)
            //    e.allowed = false;
        }

        private void axSftTree1_EditInitializing(object sender, AxSftTreeLib75._DSftTreeEvents_EditInitializingEvent e) {
            System.Windows.Forms.Control ctrl;

            // Choose a control based on current column being edited
            if (e.editCol == 1)
                ctrl = Combo1;
            else
                ctrl = Text1;

            // LeftPix/TopPix/WidthPix/HeightPix describes the current cell area
            // we need to return the position and size needed for editing.
            // In this example, we use the height of the control on the form
            // and center it over the cell.

            e.topPix = e.topPix + (e.heightPix - ctrl.Height) / 2;
            e.heightPix = ctrl.Height;

            // Set the text in the control used for cell editing and
            // set other control-specific properties
            if (ctrl == Text1) {
                Text1.Width = 200;
                Text1.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
                Text1.SelectionStart = 0;
                Text1.SelectionLength = 999;
            } else {
                Combo1.Items.Clear();
                Combo1.Items.Add("Option 1");
                Combo1.Items.Add("Option 2");
                Combo1.Items.Add("Option 3");
                Combo1.Items.Add(axSftTree1.get_Cell(e.editIndex, e.editCol).Text);
                Combo1.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
            }

            // Return the control's window handle
            e.window = (int) ctrl.Handle;
            e.vData = ctrl;

            // Define navigation keys
            // VK_TAB
            axSftTree1.CellEditIntercept(9, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_RETURN
            axSftTree1.CellEditIntercept(13, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            // VK_HOME
            axSftTree1.CellEditIntercept(36, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar);
            // VK_END
            axSftTree1.CellEditIntercept(35, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar);

            if (ctrl == Text1) {
                // We want these keys just for the edit control.
                // VK_UP
                axSftTree1.CellEditIntercept(38, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
                // VK_DOWN
                axSftTree1.CellEditIntercept(40, SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeControlChar | SftTreeCellEditInterceptStyleConstants.cellEditInterceptSftTreeShiftChar);
            }
        }

        private void axSftTree1_EditInitialized(object sender, AxSftTreeLib75._DSftTreeEvents_EditInitializedEvent e) {
            // Show the combo box dropdown portion
            if (e.vData == Combo1) {
                // We're taking over positioning, so we can drop down the dropdown portion of the control
                e.positioned = true;
                Combo1.Left = e.leftPix;
                Combo1.Top = e.topPix;
                Combo1.Width = e.widthPix;
                Combo1.Height = e.heightPix;
                Combo1.Enabled = true;
                Combo1.Visible = true;
                Combo1.Focus();
                Combo1.DroppedDown = true;
            }
        }

        private void axSftTree1_EditNavigating(object sender, AxSftTreeLib75._DSftTreeEvents_EditNavigatingEvent e) {
            // Process key pressed
            axSftTree1.EditNavigate(e.key, e.shift);
        }

        private void axSftTree1_EditValidate(object sender, AxSftTreeLib75._DSftTreeEvents_EditValidateEvent e) {
            // Validate the new cell contents
            string s;
            if (e.vData == Text1)
                s = Text1.Text;
            else
                s = Combo1.Text;
            s = s.Trim();
            if (s.Length <= 0) {
                MessageBox.Show("Just to demonstrate data input validation, this example rejects empty cells.  Please enter some data.");
                e.inputValid = false;
            }
        }

        private void axSftTree1_EditEnding(object sender, AxSftTreeLib75._DSftTreeEvents_EditEndingEvent e) {
            // Save the new cell contents
            if (e.saveInput) {
                if (e.vData == Text1) {
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = Text1.Text;
                } else {
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = Combo1.Text;
                }
            }
            Control ctrl = (Control) e.vData;
            ctrl.Visible = false;
            ctrl.Enabled = false;
        }

    }
}

Last Updated 08/13/2020 - (email)
© 2024 Softel vdm, Inc.