Hide

SftBox/OCX 5.0 - Combo Box Control

Display
Print

FontDlg Sample (C#)

This sample illustrates a font dialog for font selection.

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

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

private void Form1_Load(object sender, System.EventArgs e)
{
    // add all font information
    UpdateFontNames();
}

private void UpdateFontNames()
{
    // add all font names
    fontNames.BulkUpdate = true;
    fontNames.Items.Clear();
    fontNames.Items.AddFontNames(-1, SftBoxFontConstants.fontSftBoxAll, true, 0);
    fontNames.Items.Selection = 0;
    PerformSort(fontNames, SftBoxSortConstants.sortSftBoxAscending);
    fontNames.BulkUpdate = false;
    fontNames.Columns.MakeOptimal(0);
    fontNames.Items.RecalcHorizontalExtent(0);
}

private void UpdateFontStyles()
{
    // add all styles for the current font name
    if (fontNames.Items.Selection >= 0) {
        string OldText = fontStyles.Edit.Text;
        fontStyles.Items.AddFontStyles(fontNames.get_Cell(fontNames.Items.Selection,0).Text, 0, "", "");
        int index = fontStyles.Items.Find(OldText, 0, 0, false, true, true);
        if (index < 0) index = 0;
        fontStyles.Items.Selection = index;
    } else
        fontStyles.Items.Clear();
    PerformSort(fontStyles, SftBoxSortConstants.sortSftBoxAscending);
    fontStyles.Columns.MakeOptimal(0);
    fontStyles.Items.RecalcHorizontalExtent(0);
}

private void UpdateFontSizes()
{
    // add all sizes for the current font name
    if (fontNames.Items.Selection >= 0) {
        string OldText = fontSizes.Edit.Text;
        fontSizes.Items.AddFontSizes(fontNames.get_Cell(fontNames.Items.Selection,0).Text, 0);
        int index = fontSizes.Items.Find(OldText, 0, 0, false, true, true);
        if (index < 0) index = 0;
        fontSizes.Items.Selection = index;
    } else
        fontSizes.Items.Clear();
    PerformSort(fontSizes, SftBoxSortConstants.sortSftBoxAscending);
    fontSizes.Columns.MakeOptimal(0);
    fontSizes.Items.RecalcHorizontalExtent(0);
    UpdateSample();
}

private void fontNames_SelectionChange(object sender, System.EventArgs e)
{
    UpdateFontStyles();
}

private void fontStyles_SelectionChange(object sender, System.EventArgs e)
{
    UpdateFontSizes();
}

private void fontSizes_SelectionChange(object sender, System.EventArgs e)
{
    UpdateSample();
}

private void UpdateSample()
{
    int SelItem = fontStyles.Items.Selection;
    if (SelItem >= 0 && fontSizes.Edit.Text != "") {
        sampleText.Text = "AaBbCcYyZz";
        FontStyle style = 0;
        if ((fontStyles.get_Cell(SelItem, 0).Data & 0x80000000) != 0)
            style |= FontStyle.Italic;
        int weight = (fontStyles.get_Cell(SelItem, 0).Data & 0x7fffffff);
        if (weight > 400)
            style |= FontStyle.Bold;
        sampleText.Font = new Font(fontNames.Edit.Text, Convert.ToSingle(fontSizes.Edit.Text), style);
    } else
        sampleText.Text = "";
}

private void condHeaders_CheckedChanged(object sender, System.EventArgs e)
{
    if (condHeaders.Checked) {
        fontNames.Headers.Main = true;
        fontNames.Headers.DropDown = false;
        fontStyles.Headers.Main = true;
        fontStyles.Headers.DropDown = false;
        fontSizes.Headers.Main = true;
        fontSizes.Headers.DropDown = false;
    } else {
        fontNames.Headers.Main = false;
        fontNames.Headers.DropDown = true;
        fontStyles.Headers.Main = false;
        fontStyles.Headers.DropDown = true;
        fontSizes.Headers.Main = false;
        fontSizes.Headers.DropDown = true;
    }
}

private void PerformSort(AxSftBox box, SftBoxSortConstants currDirection)
{
    if (currDirection == SftBoxSortConstants.sortSftBoxAscending)
        box.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortAsc;
    else
        box.get_Header(0).Image.Appearance = SftPictureImageConstants.sftImageSortDesc;
    // Font styles and font names are sorted by Cell.Text property
    // Font sizes are sorted according to numeric Cell.Data property
    // Outside of this function, only sortSftBoxAscending and sortSftBoxDescending
    // are used, so we need to translate these
    if (box == fontSizes) {
        if (currDirection == SftBoxSortConstants.sortSftBoxAscending)
            currDirection = SftBoxSortConstants.sortSftBoxCellValueAscending;
        else
            currDirection = SftBoxSortConstants.sortSftBoxCellValueDescending;
    }
    box.Items.Sort(-1, 0, currDirection);
}

private void ReverseSortOrder(AxSftBox box, ref SftBoxSortConstants currDirection)
{
    if (currDirection == SftBoxSortConstants.sortSftBoxAscending)
        currDirection = SftBoxSortConstants.sortSftBoxDescending;
    else
        currDirection = SftBoxSortConstants.sortSftBoxAscending;
}

private void fontNames_ItemClick(object sender, AxSftBoxLib50._ISftBoxEvents_ItemClickEvent e)
{
    if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
        ReverseSortOrder(fontNames, ref fontNamesDirection);
        PerformSort(fontNames, fontNamesDirection);
    }
}

private void fontNames_ItemDblClk(object sender, AxSftBoxLib50._ISftBoxEvents_ItemDblClkEvent e)
{
    if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
        ReverseSortOrder(fontNames, ref fontNamesDirection);
        PerformSort(fontNames, fontNamesDirection);
    }
}

private void fontStyles_ItemClick(object sender, AxSftBoxLib50._ISftBoxEvents_ItemClickEvent e)
{
    if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
        ReverseSortOrder(fontStyles, ref fontStylesDirection);
        PerformSort(fontStyles, fontStylesDirection);
    }
}

private void fontStyles_ItemDblClk(object sender, AxSftBoxLib50._ISftBoxEvents_ItemDblClkEvent e)
{
    if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
        ReverseSortOrder(fontStyles, ref fontStylesDirection);
        PerformSort(fontStyles, fontStylesDirection);
    }
}

private void fontSizes_ItemClick(object sender, AxSftBoxLib50._ISftBoxEvents_ItemClickEvent e)
{
    if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
        ReverseSortOrder(fontSizes, ref fontSizesDirection);
        PerformSort(fontSizes, fontSizesDirection);
    }
}

private void fontSizes_ItemDblClk(object sender, AxSftBoxLib50._ISftBoxEvents_ItemDblClkEvent e)
{
    if (e.areaType == SftBoxAreaConstants.areaSftBoxColumn) {
        ReverseSortOrder(fontSizes, ref fontSizesDirection);
        PerformSort(fontSizes, fontSizesDirection);
    }
}

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