Hide

SftTree/OCX 7.5 - ActiveX Tree Control

Display
Print

Email Sample (C#)

This sample illustrates using a splitter bar, cell merging, cell images, sorting, column reordering.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\Email\Form1.cs or C:\Program Files\Softelvdm\SftTree OCX 7.5\Samples\Visual Studio - CSharp\Email\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;
            m_InboxFolder = AddFolder("Inbox", "Viewed and unviewed mail", 10);
                AddMessage(1, "support@softelvdm.com", "Re: A support question", "10/09/05", 88, true);
                AddMessage(1, "anyone@acompany.com", "Re: Why did you say that", "10/10/05", 5, false);
                AddMessage(1, "anyone@acompany.com", "Re: You're fired", "10/11/05", 82, true);
                AddMessage(1, "anyone@acompany.com", "Re: You're hired", "10/11/05", 6, false);
                ItemIndex = axSftTree1.Items.get_ItemIndex(m_InboxFolder);
                axSftTree1.get_Item(ItemIndex).Collapse(false);
            m_OutboxFolder = AddFolder("Outbox", "Mail about to be sent", 2);
                AddMessage(1, "me@mycompany.com", "Re: You're fired", "10/11/05", 5, false);
                AddMessage(1, "anyone@acompany.com", "Re: Why did you say that", "10/10/05", 5, false);
                ItemIndex = axSftTree1.Items.get_ItemIndex(m_OutboxFolder);
                axSftTree1.get_Item(ItemIndex).Collapse(false);
            m_SavedFolder = AddFolder("Saved", "Saved messages", 2);
                AddMessage(1, "me@mycompany.com", "A support question", "10/09/05", 3, false);
                AddMessage(2, "support@softelvdm.com", "Re: A support question", "10/09/05", 88, true);
                AddMessage(3, "me@mycompany.com", "Re: A support question", "10/09/05", 3, false);
                AddMessage(1, "me@mycompany.com", "Why did you say that", "10/06/05", 5, false);
                AddMessage(2, "anyone@acompany.com", "Re: Why did you say that", "10/10/05", 5, false);
                AddMessage(1, "me@mycompany.com", "You're fired", "10/08/05", 2, false);
                AddMessage(2, "anyone@acompany.com", "Re: You're fired", "10/11/05", 82, false);
                AddMessage(1, "me@mycompany.com", "You're hired", "10/01/05", 4, false);
                AddMessage(1, "anyone@acompany.com", "Re: You're hired", "10/11/05", 6, false);
                ItemIndex = axSftTree1.Items.get_ItemIndex(m_SavedFolder);
                axSftTree1.get_Item(ItemIndex).Collapse(false);
            m_DeletedFolder = AddFolder("Deleted", "Deleted messages", 0);
            axSftTree1.ColumnsObj.MakeOptimal();
            axSftTree1.Items.RecalcHorizontalExtent();
            axSftTree1.Splitter.MakeOptimal();
            // allow sorting
            axSftTree1.Headers.SortIndicators = SftTreeHeaderSortIndicatorsConstants.headerSortIndicatorsSftTreeAuto;

            axSftTree1.Items.Current = 0;
            axSftTree1_CaretChange(this, new _DSftTreeEvents_CaretChangeEvent(0));
        }

        private int AddFolder(string Folder, string Desc, int Count)
        {
            int ItemIndex = axSftTree1.Items.Add(Folder); // add folder
            axSftTree1.get_Cell(ItemIndex, 1).Text = Desc; // add description
            axSftTree1.get_Cell(ItemIndex, 3).Text = Count.ToString(); // add number of messages
            axSftTree1.get_Cell(ItemIndex, 3).Data = Count; // also save number of messages for sorting

            // make the folder name bold
            if (m_BoldFont == null) {
                m_BoldFont = axSftTree1.get_Cell(ItemIndex, 0).Font;
                m_BoldFont.Bold = true;
            }
            axSftTree1.get_Cell(ItemIndex, 0).Font = m_BoldFont;

            // if this folder has no messages (ie. dependents), we still want the
            // folder graphic, not the email (leaf) graphic
            if (Count == 0)
                axSftTree1.get_Item(ItemIndex).Image = axSftTree1.Items.ItemImageExpandable;

            return axSftTree1.get_Item(ItemIndex).ID;
        }

        private int AddMessage(short Level, string Email, string Desc, string Dt, int Size, bool Attachment)
        {
            int ItemIndex = axSftTree1.Items.Add(Email); // add folder
            axSftTree1.get_Item(ItemIndex).Level = Level;
            axSftTree1.get_Cell(ItemIndex, 1).Text = Desc; // add description
            axSftTree1.get_Cell(ItemIndex, 2).Text = Dt; // add date
            axSftTree1.get_Cell(ItemIndex, 3).Text = Size.ToString() + "K";
            axSftTree1.get_Cell(ItemIndex, 3).Data = Size; // also save size for sorting
            // if this email has replies (ie. dependents), we still want the
            // email graphic, not the expand folder image
            axSftTree1.get_Item(ItemIndex).Image = axSftTree1.Items.ItemImageLeaf;
            // if the email has an attachment, show the image
            if (Attachment) {
                axSftTree1.get_Cell(ItemIndex, 1).Image.NETImageObject = Attach.Image;
                axSftTree1.get_Cell(ItemIndex, 1).ImageHAlign = SftTreeHAlignConstants.halignSftTreeRight;
            }
            return axSftTree1.get_Item(ItemIndex).ID;
        }

        private void axSftTree1_CaretChange(object sender, AxSftTreeLib75._DSftTreeEvents_CaretChangeEvent e) {
            // set column headers based on selected item
            int ID = axSftTree1.get_Item(e.itemIndex).ID;
            if (ID == m_InboxFolder || ID == m_OutboxFolder || ID == m_SavedFolder || ID == m_DeletedFolder) {
                axSftTree1.get_Header(0).Text = "Folder";
                axSftTree1.get_Header(1).Text = "Description";
                axSftTree1.get_Header(2).Text = ""; // we can use this to merge the adjacent title
                axSftTree1.get_Header(3).Text = "Msgs";
            } else {
                axSftTree1.get_Header(0).Text = "From";
                axSftTree1.get_Header(1).Text = "Subject";
                axSftTree1.get_Header(2).Text = "Received";
                axSftTree1.get_Header(3).Text = "Size";
            }
        }

        private void axSftTree1_ItemClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemClickEvent e) {
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeButton) {
                if (axSftTree1.get_Item(e.itemIndex).Expanded)
                    axSftTree1.get_Item(e.itemIndex).Collapse(true);
                else
                    axSftTree1.get_Item(e.itemIndex).Expand(true, false);
            } else if (area == SftTreeAreaTypeConstants.constSftTreeText) {
                // edit description
                if (e.colIndex == 1)
                    axSftTree1.get_Cell(e.itemIndex, e.colIndex).Edit(0, 0);
            } else if (area == SftTreeAreaTypeConstants.constSftTreeColumn) {
                short sortedColumn = axSftTree1.Headers.SortedColumn;
                if (sortedColumn >= 0) {
                    // we have to sort based on current column headers
                    int ItemIndex = axSftTree1.Items.Current;
                    int ID = 0;
                    if (ItemIndex >= 0)
                        ID = axSftTree1.get_Item(ItemIndex).ID;
                    if (ID == m_InboxFolder || ID == m_OutboxFolder || ID == m_SavedFolder || ID == m_DeletedFolder) {
                        // we're on a folder, so sort on folder level
                        if (axSftTree1.get_Header(sortedColumn).SortIndicator == SftTreeSortIndicatorConstants.sortIndicatorSftTreeAscending) {
                            if (sortedColumn == 3)
                                // last column is sorted by Cell.Data (numeric values)
                                axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeAscCellItemData);
                            else
                                axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeAscending);
                        } else {
                            if (sortedColumn == 3)
                                axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeDscCellItemData);
                            else
                                axSftTree1.Items.SortDependents(-1, sortedColumn, SftTreeSortTypeConstants.sortSftTreeDescending);
                        }
                    } else {
                        // we're on a message, sort inside folders
                        // now sort the dependents of all folders
                        ItemIndex = 0;
                        do {
                            if (axSftTree1.get_Header(sortedColumn).SortIndicator == SftTreeSortIndicatorConstants.sortIndicatorSftTreeAscending) {
                                if (sortedColumn == 3)
                                    // last column is sorted by Cell.Data (numeric values)
                                    axSftTree1.Items.SortDependents(ItemIndex, sortedColumn, SftTreeSortTypeConstants.sortSftTreeAscCellItemData);
                                else
                                    axSftTree1.Items.SortDependents(ItemIndex, sortedColumn, SftTreeSortTypeConstants.sortSftTreeAscending);
                            } else {
                                if (sortedColumn == 3)
                                    axSftTree1.Items.SortDependents(ItemIndex, sortedColumn, SftTreeSortTypeConstants.sortSftTreeDscCellItemData);
                                else
                                    axSftTree1.Items.SortDependents(ItemIndex, sortedColumn, SftTreeSortTypeConstants.sortSftTreeDescending);
                            }
                            ItemIndex = axSftTree1.get_Item(ItemIndex).NextSibling;
                        } while (ItemIndex >= 0);
                    }
                    // update column headers by calling CaretChange
                    axSftTree1_CaretChange(this, new _DSftTreeEvents_CaretChangeEvent(axSftTree1.Items.Current));
                }
            }
        }

        private void axSftTree1_ItemDblClick(object sender, AxSftTreeLib75._DSftTreeEvents_ItemDblClickEvent e) {
            SftTreeAreaTypeConstants area = (SftTreeAreaTypeConstants) e.areaType;
            if (area == SftTreeAreaTypeConstants.constSftTreeText || area == SftTreeAreaTypeConstants.constSftTreeButton) {
                // if an item on level 1 or lower is clicked, it's an
                // email message, display message box
                if (axSftTree1.get_Item(e.itemIndex).Level > 0 && area == SftTreeAreaTypeConstants.constSftTreeText) {
                    MessageBox.Show("You could be viewing the message from " + axSftTree1.get_Cell(e.itemIndex, 0).Text +
                        " with the subject '" + axSftTree1.get_Cell(e.itemIndex, 1).Text + "'.");
                } else {
                    // otherwise just expand or collapse the item
                    if (axSftTree1.get_Item(e.itemIndex).Expanded)
                        axSftTree1.get_Item(e.itemIndex).Collapse(true);
                    else
                        axSftTree1.get_Item(e.itemIndex).Expand(true, false);
                }
            } else if (area == SftTreeAreaTypeConstants.constSftTreeColumnRes) {
                if (e.colIndex >= 0) {
                    axSftTree1.get_Column(e.colIndex).MakeOptimal();
                    axSftTree1.Items.RecalcHorizontalExtent();
                } else
                    axSftTree1.Splitter.MakeOptimal();
            }
        }

        private void axSftTree1_EditAllowed(object sender, AxSftTreeLib75._DSftTreeEvents_EditAllowedEvent e) {
            // Only allow editing in column 1
            e.allowed = (e.colIndex == 1);
        }

        private void axSftTree1_EditInitializing(object sender, AxSftTreeLib75._DSftTreeEvents_EditInitializingEvent e) {

            // 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 - EditControl.Height) / 2;
            e.heightPix = EditControl.Height;

            // Set the text in the control used for cell editing and
            // set other control-specific properties
            EditControl.Width = 200;
            EditControl.Text = axSftTree1.get_Cell(e.editIndex, e.editCol).Text;
            EditControl.SelectionStart = 0;
            EditControl.SelectionLength = 999;

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

            // 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);
            // 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_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 = EditControl.Text;
            s = s.Trim();
            if (s.Length <= 0) {
                MessageBox.Show("Please enter a description.");
                e.inputValid = false;
            }
        }

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

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