Get/Set property of FontPropertiesDialog

4 posts, 0 answers
  1. NGA TQ
    NGA TQ avatar
    6 posts
    Member since:
    Apr 2010

    Posted 29 Mar 2011 Link to this post

    Hi Telerik Team.
    I'm using Rad Controls For Silverlight Q3 2010 SP1

    I have a Textbox, a RadButton. How can I implement:
    - When Click the Button show FontPropertiesDialog, and after this dialog close, the Textbox display Font, Font Style, Size I've just choosed.
     - When I reclick the Button, show FontPropertiesDialog with Properties: font, font style, size display the result in textbox

    My code behind:

    Private

     

     

    WithEvents oFontDialog As FontPropertiesDialog

     

     

     

    Private Sub FontButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)

     

     

     

        Dim btnFontSelectButton As RadButton = TryCast(sender, RadButton)

     

     

        oFontDialog =

     

    New FontPropertiesDialog()

     

     

     

     

        AddHandler oFontDialog.Closed, AddressOf FontDialog_Closed

     

     

        oFontDialog.ShowDialog()

     

     

     

    End Sub

     


    Private

     

     

    Sub FontDialog_Closed(sender As Object, e As Telerik.Windows.Controls.WindowClosedEventArgs)

     

     

     

        Dim bResult As Nullable(Of Boolean) = e.DialogResult

     

     

     

     

        If bResult Then

     

     

     

            With oFontDialog

     

     

                txtCompanyNameFont.Text = .FontSize.ToString &

     

    ";" & .FontStyle.ToString

     

     

     

     

            End With

     

     

     

        End If

     

    End Sub

     


    But oFontDialog.FontStyle is Normal. I need get string: "11;Arial;Bold"
    Thanks for your advises
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 31 Mar 2011 Link to this post

    Hello NGA TQ,

    The FontStyle and FontSize properties of the FontPropertiesdialog are the ones it inherits from System.Windows.Controls and not the values set in the listboxes. Here is how you can achieve the result you are looking for.

    private void showFP_Click(object sender, RoutedEventArgs e)
    {
        FontPropertiesDialog oFontPropertiesDialog = new FontPropertiesDialog();
        StyleDefinition defaultStyle = new Telerik.Windows.Documents.Model.Span().ExtractStyleFromProperties();
        oFontPropertiesDialog.ShowDialog(defaultStyle, new Action<StyleDefinition>((style) => SetFontValuesToTextBoxText(style)));
    }
     
    private void SetFontValuesToTextBoxText(StyleDefinition style)
    {
        double fontSize = (double)style.GetPropertyValue(Telerik.Windows.Documents.Model.Span.FontSizeProperty);
        fontSize = Unit.DipToPoint(fontSize);
        FontFamily fontFamily = (FontFamily)style.GetPropertyValue(Telerik.Windows.Documents.Model.Span.FontFamilyProperty);
        FontWeight fontWeight = (FontWeight)style.GetPropertyValue(Telerik.Windows.Documents.Model.Span.FontWeightProperty);
        this.tb.Text = fontSize + ";" + fontFamily + ";" + fontWeight;
    }

    Using this code, no changes will be done to the text in a RadRichTextBox, though, but I guess you are using the dialog in a different scenario.

    Greetings,
    Iva
    the Telerik team
  3. NGA TQ
    NGA TQ avatar
    6 posts
    Member since:
    Apr 2010

    Posted 31 Mar 2011 Link to this post

    Hello Iva
    Thank you for your supporting. I'm try using your way but I can not implement  my purpose: set properties of FontPropertiesDialog following textbox.text.
    Private Sub SetFontValuesToTextBoxText(style As StyleDefinition)
            Dim fontSize As Double = CDbl(style.GetPropertyValue(Telerik.Windows.Documents.Model.Span.FontSizeProperty))
            fontSize = Unit.DipToPoint(fontSize)
            Dim fontFamily As Windows.Media.FontFamily = DirectCast(style.GetPropertyValue(Telerik.Windows.Documents.Model.Span.FontFamilyProperty), Windows.Media.FontFamily)
            Dim fontWeight As Windows.FontWeight = DirectCast(style.GetPropertyValue(Telerik.Windows.Documents.Model.Span.FontWeightProperty), Windows.FontWeight)
            txtCompanyNameFont.Text = fontSize & ";" & fontFamily.ToString & ";" & fontWeight.ToString
    End Sub
      
     Private Sub FontButton_Click(sender As System.Object, e As System.Windows.RoutedEventArgs)
                Dim btn = TryCast(sender, MISAButton)
                Dim oFontPropertiesDialog As New FontPropertiesDialog()
                Dim defaultStyle As StyleDefinition = New Telerik.Windows.Documents.Model.Span().ExtractStyleFromProperties()
                Select Case btn.Tag
                    Case "btnCompanyNameFont"
                        If txtCompanyNameFont.Text <> "" Then
                            Dim lstPro = txtCompanyNameFont.Text.Split(";")
                            With defaultStyle
                                .SetPropertyValue(Telerik.Windows.Documents.Model.Span.FontSizeProperty, lstPro(0))
                                .SetPropertyValue(Telerik.Windows.Documents.Model.Span.FontFamilyProperty, lstPro(1))
                                .SetPropertyValue(Telerik.Windows.Documents.Model.Span.FontWeightProperty, lstPro(2))
                            End With
                        End If
                    Case "btnCompanyAddressFont"
                    Case "btnParentCompanyFont"
                End Select
                oFontPropertiesDialog.ShowDialog(defaultStyle, AddressOf SetFontValuesToTextBoxText)
               End Sub

    There is how I want to implement.
    But I can not set property FontFamily,FontWeight.
  4. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 06 Apr 2011 Link to this post

    Hello NGA TQ,

    The FontPropertiesDialog gets the values from the StyleDefinition that is passed as a first argument to the ShowDialog method. Here is the revised code:

    private void showFP_Click(object sender, RoutedEventArgs e)
    {
        FontPropertiesDialog oFontPropertiesDialog = new FontPropertiesDialog();
        StyleDefinition defaultStyle = new Telerik.Windows.Documents.Model.Span().ExtractStyleFromProperties();
        if (tb.Text != String.Empty)
        {
            string[] values = tb.Text.Split(new char[] { ';' });
            double fontSize;
            if (Double.TryParse(values[0], out fontSize))
            {
                fontSize = Unit.PointToDip(fontSize);
                defaultStyle.SetPropertyValue(Telerik.Windows.Documents.Model.Span.FontSizeProperty, fontSize);
            }
     
            defaultStyle.SetPropertyValue(Telerik.Windows.Documents.Model.Span.FontFamilyProperty, new FontFamily(values[1]));
            FontWeight fontWeight;
            switch (values[2])
            {
                case "Bold": fontWeight = FontWeights.Bold;
                    break;
                case "Normal": fontWeight = FontWeights.Normal;
                    break;
                default: throw new ArgumentException();
            }
            defaultStyle.SetPropertyValue(Telerik.Windows.Documents.Model.Span.FontWeightProperty, fontWeight);
        }
        oFontPropertiesDialog.ShowDialog(defaultStyle, new Action<StyleDefinition>((style) => SetFontValuesToTextBoxText(style)));
    }


    All the best,
    Iva
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top