HeaderPane
Main
Hide
Share Link
Print

DemoSample Sample (C#)

This sample shows an overview of the tab control's features, with options that change its look, tab position, rows, buttons and other properties.

The source code is located at C:\Program Files (x86)\Softelvdm\SftTabs NET 3.0\Samples\CSharp\DemoSample\Form1.cs.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using Softelvdm.SftTabsNET;
using Softelvdm.SftTabsNET.Controls;

namespace DemoSample {
    public partial class Form1 : Form {

        private bool loading = false;
        private bool minimized = false;
        private int addedTabs = 0;
        private Dictionary<TabClass, Image> tabImages = new Dictionary<TabClass, Image>();

        public Form1() {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e) {
            // the options show the properties as they were defined in the designer
            loading = true;
            FillCombo(comboLook, sftTabs1.Look);
            FillCombo(comboPosition, sftTabs1.TabPosition);
            FillCombo(comboOrientation, sftTabs1.TextOrientation);
            FillCombo(comboAlignment, sftTabs1.TabAlignment);
            FillCombo(comboCloseButton, sftTabs1.TabCloseButton);
            FillCombo(comboDarkMode, sftTabs1.DarkMode);
            FillCombo(comboLayout, sftTabs1.LayoutMode);
            checkScrolling.Checked = sftTabs1.Scrolling.Enabled;
            numericRows.Value = sftTabs1.Rows;
            checkFixed.Checked = sftTabs1.Fixed;
            checkFill.Checked = sftTabs1.FillComplete;
            checkBold.Checked = sftTabs1.BoldSelected;
            checkFlyby.Checked = sftTabs1.FlybyHighlight;
            checkImages.Checked = true;
            checkReorder.Checked = sftTabs1.AllowReordering;
            checkButtons.Checked = sftTabs1.Buttons.CloseButton;
            checkToolTips.Checked = sftTabs1.ShowToolTips;
            loading = false;

            foreach (TabClass tab in sftTabs1.Tabs) {
                if (tab.Image != null)
                    tabImages.Add(tab, tab.Image);
            }
            UpdateOptions();
        }

        private void FillCombo(ComboBox combo, object current) {
            foreach (object value in Enum.GetValues(current.GetType()))
                combo.Items.Add(value);
            combo.SelectedItem = current;
        }

        // options that have no effect with the current settings are disabled
        private void UpdateOptions() {
            bool scrolling = sftTabs1.Scrolling.Enabled;
            bool autoFlow = sftTabs1.LayoutMode == TabLayoutModeEnum.AutoFlow;
            bool oneRow = scrolling || (sftTabs1.Rows == 1 && !autoFlow);
            // tabs scroll in one row and not while they fill the row
            checkScrolling.Enabled = sftTabs1.Rows == 1 && !sftTabs1.FillComplete;
            labelRows.Enabled = numericRows.Enabled = !scrolling && !autoFlow;
            labelLayout.Enabled = comboLayout.Enabled = !scrolling;
            checkFill.Enabled = !scrolling;
            // tabs are reordered and the buttons are shown in one row only
            checkReorder.Enabled = oneRow;
            checkButtons.Enabled = oneRow;
            TabLookEnum look = sftTabs1.Look;
            buttonAccent.Enabled = look == TabLookEnum.Underline || look == TabLookEnum.Document || look == TabLookEnum.Pills;
        }

        private void comboLook_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.Look = (TabLookEnum)comboLook.SelectedItem;
            UpdateOptions();
        }

        private void comboPosition_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            RestorePages();
            sftTabs1.TabPosition = (TabPositionEnum)comboPosition.SelectedItem;
        }

        private void comboOrientation_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.TextOrientation = (TabTextOrientationEnum)comboOrientation.SelectedItem;
        }

        private void comboAlignment_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.TabAlignment = (TabAlignmentEnum)comboAlignment.SelectedItem;
        }

        private void comboCloseButton_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.TabCloseButton = (TabCloseButtonEnum)comboCloseButton.SelectedItem;
        }

        private void comboDarkMode_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.DarkMode = (DarkModeEnum)comboDarkMode.SelectedItem;
        }

        private void comboLayout_SelectedIndexChanged(object sender, EventArgs e) {
            if (loading) return;
            RestorePages();
            sftTabs1.LayoutMode = (TabLayoutModeEnum)comboLayout.SelectedItem;
            UpdateOptions();
        }

        private void checkScrolling_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            RestorePages();
            sftTabs1.Scrolling.Enabled = checkScrolling.Checked;
            UpdateOptions();
        }

        private void numericRows_ValueChanged(object sender, EventArgs e) {
            if (loading) return;
            RestorePages();
            sftTabs1.Rows = (int)numericRows.Value;
            UpdateOptions();
        }

        private void checkFixed_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.Fixed = checkFixed.Checked;
        }

        private void checkFill_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.FillComplete = checkFill.Checked;
            UpdateOptions();
        }

        private void checkBold_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.BoldSelected = checkBold.Checked;
        }

        private void checkFlyby_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.FlybyHighlight = checkFlyby.Checked;
        }

        private void checkImages_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            foreach (KeyValuePair<TabClass, Image> entry in tabImages)
                entry.Key.Image = checkImages.Checked ? entry.Value : null;
        }

        private void checkReorder_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.AllowReordering = checkReorder.Checked;
        }

        private void checkButtons_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            RestorePages();
            sftTabs1.Buttons.CloseButton = checkButtons.Checked;
            sftTabs1.Buttons.MinimizeButton = checkButtons.Checked;
            sftTabs1.Buttons.RestoreButton = checkButtons.Checked;
        }

        private void checkToolTips_CheckedChanged(object sender, EventArgs e) {
            if (loading) return;
            sftTabs1.ShowToolTips = checkToolTips.Checked;
        }

        private void buttonAccent_Click(object sender, EventArgs e) {
            colorDialog1.Color = sftTabs1.AccentColor;
            if (colorDialog1.ShowDialog(this) == DialogResult.OK)
                sftTabs1.AccentColor = colorDialog1.Color;
        }

        private void buttonAdd_Click(object sender, EventArgs e) {
            ++addedTabs;
            TabClass tab = sftTabs1.Tabs.Add("Added " + addedTabs.ToString());
            tab.CloseButton = TabCloseButtonOptionalEnum.Always;

            // the page of the new tab is created by the control
            Label info = new Label();
            info.Dock = DockStyle.Fill;
            info.Padding = new Padding(12);
            info.Text = "This tab was added by the Add Tab button. Its close button removes it.";
            tab.Page.Controls.Add(info);
            SetPageColors();

            tab.Select();
        }

        private void sftTabs1_DarkModeChanged(object sender, EventArgs e) {
            SetPageColors();
        }

        // the control paints the pages; the text color of the controls on the pages is the application's
        private void SetPageColors() {
            Color color = sftTabs1.DarkModeActive ? Color.White : SystemColors.ControlText;
            foreach (TabClass tab in sftTabs1.Tabs)
                tab.Page.ForeColor = color;
        }

        private void sftTabs1_TabCloseButtonClicked(object sender, TabEventArgs e) {
            // the control does not remove the tab, the application does
            tabImages.Remove(e.Tab);
            e.Tab.Remove();
        }

        private void sftTabs1_CloseButtonClicked(object sender, EventArgs e) {
            // the control's Close button removes the selected tab in this sample
            TabClass tab = sftTabs1.SelectedTab;
            if (tab != null) {
                tabImages.Remove(tab);
                tab.Remove();
            }
        }

        private void sftTabs1_MinimizeButtonClicked(object sender, EventArgs e) {
            // the control is reduced to its tabs; the panel behind it becomes visible
            if (minimized) return;
            Rectangle strip = sftTabs1.StripBounds;
            Size border = sftTabs1.Size - sftTabs1.ClientSize;
            switch (sftTabs1.TabPosition) {
                case TabPositionEnum.Top:
                    sftTabs1.Dock = DockStyle.Top;
                    sftTabs1.Height = strip.Height + border.Height;
                    break;
                case TabPositionEnum.Bottom:
                    sftTabs1.Dock = DockStyle.Bottom;
                    sftTabs1.Height = strip.Height + border.Height;
                    break;
                case TabPositionEnum.Left:
                    sftTabs1.Dock = DockStyle.Left;
                    sftTabs1.Width = strip.Width + border.Width;
                    break;
                case TabPositionEnum.Right:
                    sftTabs1.Dock = DockStyle.Right;
                    sftTabs1.Width = strip.Width + border.Width;
                    break;
            }
            minimized = true;
            sftTabs1.Buttons.MinimizeButtonEnabled = false;
            sftTabs1.Buttons.RestoreButtonEnabled = true;
        }

        private void sftTabs1_RestoreButtonClicked(object sender, EventArgs e) {
            RestorePages();
        }

        private void RestorePages() {
            if (!minimized) return;
            sftTabs1.Dock = DockStyle.Fill;
            minimized = false;
            sftTabs1.Buttons.MinimizeButtonEnabled = true;
            sftTabs1.Buttons.RestoreButtonEnabled = false;
        }
    }
}