Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

CellEditingII Sample (C#)

This sample illustrates cell editing using ActiveX controls.

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


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

            Text1.Text = "In this example, the first column uses a SftMask/OCX Masked Edit control to enter an amount.  Try entering a new amount, then type + or - to access the built-in popup calculator.\r\n\r\nThe second column uses a SftBox/OCX Combo Box control.\r\n\r\nThe last column again uses a SftMask/OCX Masked Edit control to enter a date.  Click on the drop down button to access the popup calendar.";

            ItemIndex = axSftTree1.Items.Add("Edit cells\r\nusing SftMask/OCX:");
            axSftTree1.get_Item(ItemIndex).EditIgnore = true;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "\r\nusing SftBox/OCX:";
            axSftTree1.get_Cell(ItemIndex, 2).Text = "and again\r\nusing SftMask/OCX:";

            DateTime Dt;

            ItemIndex = axSftTree1.Items.Add("5.33");
            axSftTree1.get_Cell(ItemIndex, 0).TextHAlign = SftTreeHAlignDefaultConstants.halignSftTreeDefaultRight;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Option 1";
            Dt = DateTime.Today.AddDays(-1);
            string s;
            s = "short";
            SftMaskDateTime.Calendar.FormatDate(Dt, ref  s);
            axSftTree1.get_Cell(ItemIndex, 2).Text = s;
            axSftTree1.get_Cell(ItemIndex, 2).DataTag = Dt;

            ItemIndex = axSftTree1.Items.Add("122.33");
            axSftTree1.get_Cell(ItemIndex, 0).TextHAlign = SftTreeHAlignDefaultConstants.halignSftTreeDefaultRight;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Option 3";
            Dt = DateTime.Today;
            s = "short";
            SftMaskDateTime.Calendar.FormatDate(Dt, ref  s);
            axSftTree1.get_Cell(ItemIndex, 2).Text = s;
            axSftTree1.get_Cell(ItemIndex, 2).DataTag = Dt;

            ItemIndex = axSftTree1.Items.Add("66.05");
            axSftTree1.get_Cell(ItemIndex, 0).TextHAlign = SftTreeHAlignDefaultConstants.halignSftTreeDefaultRight;
            axSftTree1.get_Cell(ItemIndex, 1).Text = "Option 2";
            Dt = DateTime.Today.AddDays(1);
            s = "short";
            SftMaskDateTime.Calendar.FormatDate(Dt, ref  s);
            axSftTree1.get_Cell(ItemIndex, 2).Text = s;
            axSftTree1.get_Cell(ItemIndex, 2).DataTag = Dt;

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

            axSftTree1.get_Item(0).Selected = true;
        }

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

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemClickEvent e)
        {
            // User clicked on a cell
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeCellText)
                axSftTree1.get_Cell(e.itemIndex, e.colIndex).Edit(0, 0);
        }

        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 == 0)
                ctrl = SftMaskAmount;
            else if (e.editCol == 1)
                ctrl = SftBox1;
            else
                ctrl = SftMaskDateTime;

            // 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 == SftMaskAmount) {
                SftMaskAmount.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
                SftMaskAmount.SelStart = 0;
                SftMaskAmount.SelLength = 999;
            } else if (ctrl == SftMaskDateTime) {
                DateTime Dt = (DateTime) axSftTree1.get_Cell(e.editIndex, e.editCol).DataTag;
                SftMaskDateTime.Contents.DateTime = Dt;
            } else {
                SftBox1.Items.Clear();
                int ItemIndex = SftBox1.Items.Add("Option 1");
                SftBox1.get_Cell.Text = "Description for option 1";
                ItemIndex = SftBox1.Items.Add("Option 2");
                SftBox1.get_Cell(ItemIndex, 1).Text = "Description for option 2";
                ItemIndex = SftBox1.Items.Add("Option 3");
                SftBox1.get_Cell(ItemIndex, 1).Text = "Description for option 3";
                string s = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
                if (SftBox1.Items.Find(s, 0, 0, false, true, true) < 0) {
                    ItemIndex = SftBox1.Items.Add(s);
                    SftBox1.get_Cell(ItemIndex, 1).Text = "Description for " + s;
                }
                SftBox1.Columns.MakeOptimal(0);
                SftBox1.RecalcHorizontalExtent(0);
                SftBox1.Edit.Text = s;
            }

            // 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 == SftMaskAmount) {
                // We want these keys just for the amount 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) {
            // We position the control (ActiveX controls must be positioned using the EditInitialized event)
            e.positioned = true;

            Control ctrl = (Control) e.vData;
            if (ctrl == SftMaskAmount) {
                SftMaskAmount.Left = e.leftPix;
                SftMaskAmount.Top = e.topPix;
                SftMaskAmount.Width = e.widthPix;
                SftMaskAmount.Height = e.heightPix;
                SftMaskAmount.Enabled = true;
                SftMaskAmount.Visible = true;
                SftMaskAmount.Focus();
            } else if (ctrl == SftMaskDateTime) {
                SftMaskDateTime.Left = e.leftPix;
                SftMaskDateTime.Top = e.topPix;
                SftMaskDateTime.Width = e.widthPix;
                SftMaskDateTime.Height = e.heightPix;
                SftMaskDateTime.Enabled = true;
                SftMaskDateTime.Visible = true;
                SftMaskDateTime.Focus();
            } else {
                SftBox1.Left = e.leftPix;
                SftBox1.Top = e.topPix;
                SftBox1.Width = e.widthPix;
                SftBox1.Height = e.heightPix;
                SftBox1.Enabled = true;
                SftBox1.Visible = true;
                SftBox1.Focus();
                SftBox1.Edit.SetSelection(0, -1);
                SftBox1.DropDown.Dropped = 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;
            Control ctrl = (Control) e.vData;
            if (ctrl == SftMaskAmount) {
                if (!SftMaskAmount.Contents.Valid) {
                    // could validate
                }
                s = SftMaskAmount.TextDisplay;
            } else if (ctrl == SftMaskDateTime) {
                if (!SftMaskDateTime.Contents.Valid) {
                    MessageBox.Show("Please enter a valid date.");
                    e.inputValid = false;
                    return;
                }
                s = SftMaskDateTime.TextDisplay;
            } else
                s = SftBox1.Edit.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
            Control ctrl = (Control) e.vData;
            if (ctrl == SftMaskAmount) {
                SftMaskAmount.Visible = false;
                SftMaskAmount.Enabled = false;
                if (e.saveInput)
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = SftMaskAmount.TextDisplay;
            } else if (ctrl == SftMaskDateTime) {
                SftMaskDateTime.Visible = false;
                SftMaskDateTime.Enabled = false;
                if (e.saveInput) {
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = SftMaskDateTime.TextDisplay;
                    axSftTree1.get_Cell(e.editIndex, e.editCol).DataTag = SftMaskDateTime.Contents.DateTime;
                }
            } else {
                SftBox1.Visible = false;
                SftBox1.Enabled = false;
                if (e.saveInput)
                    axSftTree1.get_Cell(e.editIndex, e.editCol).Text = SftBox1.Edit.Text;
            }
        }

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

        private void SftMaskAmount_UpDownPress(object sender, AxSftMaskLib70._ISftMaskEvents_UpDownPressEvent e) {
            double amount;
            double increment;
            if (e.counter == 0 || e.counter > 5) {
                amount = Convert.ToDouble(e.field);
                increment = 0.01;
                if (e.counter > 14) increment = 0.1;
                if (e.counter > 23) increment = 1;
                if (e.counter > 52) increment = 10;
                if (e.up)
                    amount = amount + increment;
                else
                    amount = amount - increment;
                if (amount > 99999.99) amount = 99999.99;
                if (amount < -99999.99) amount = -99999.99;
                e.field = amount.ToString("N2");
            }
        }
    }
}

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