Link to home
Create AccountLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Help needed with declaring a font

Hi,

I need do this below. Is that possible?

        private void simpleButtonOk_Click(object sender, EventArgs e)
        {

         font  = textEditFontName.EditValue;
         font  =  textEditFontStyle.EditValue;
         font =   textEditFontSize.EditValue;
          Properties.Settings.Default.FontGrid = font;
          Properties.Settings.Default.Save();
        }

Who knows the answer and is willing to help me?

Peter
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

If you want to instantiate a font from text values (with font name, size, etc) you should use

Dim font As System.Drawing.Font = New System.Drawing.Font(name, size, style)

Hope that helps.
Avatar of Peter Kiers

ASKER

I have some errors:

        private void simpleButtonOk_Click(object sender, EventArgs e)
        {
         System.Drawing.Font font = new System.Drawing.Font(textEditFontName.Text, textEditFontSize.Text, textEditFontStyle.Text);
          Properties.Settings.Default.FontGrid = Font;
          Properties.Settings.Default.Save();
        }

Open in new window


The erros are:
cannot convert from 'object' to 'float'
cannot convert from 'DevExpress.XtraEditors.TextEdit' to 'System.Drawing.GraphicsUnit'            MyFont
Error      1      The best overloaded method match for 'System.Drawing.Font.Font(string, float, System.Drawing.GraphicsUnit)' has some invalid arguments      

Peter
What is the contents of you textEditFontStyle.Text?
The content of the textEditFontStyle could be one of the following strings:
-Regular
-Italic
-Bold
-Bold Italic

P.
I almost got it bug free:
        private void simpleButtonOk_Click(object sender, EventArgs e)
        {
            string name = textEditFontName.Text;
            float size = System.Convert.ToSingle(textEditFontSize.Text);
            string style = textEditFontStyle.Text; <====
            System.Drawing.Font font = new System.Drawing.Font(name, size, style);
            Properties.Settings.Default.FontGrid = Font;
            Properties.Settings.Default.Save();
        }

Open in new window


At the line where I have put the arrow I still have the error:
'DevExpress.XtraEditors.TextEdit' to 'System.Drawing.GraphicsUnit'

Peter
Then try this:

        Dim font As System.Drawing.Font
        Dim style As System.Drawing.FontStyle

        Select Case textEditFontStyle.Text
            Case "Regular"
                style = FontStyle.Regular
            Case "Bold"
                style = FontStyle.Bold
            Case "Italic"
                style = FontStyle.Italic
            Case "Bold Italic"
                style = FontStyle.Bold Or FontStyle.Italic
        End Select

        font = New System.Drawing.Font(textEditFontName.Text, Convert.ToSingle(textEditFontSize.Text), style, GraphicsUnit.Point)

Open in new window

This is supposing that your font size is in points. If not, change the GraphicsUnit.Point for GraphicsUnit.Pixel or whatever it is.

Hope that helps.
ASKER CERTIFIED SOLUTION
Avatar of Luis Pérez
Luis Pérez
Flag of Spain image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Ah, it bug free.

        private void simpleButtonOk_Click(object sender, EventArgs e)
        {
            font = new System.Drawing.Font(textEditFontName.Text, Convert.ToSingle(textEditFontSize.Text), style, GraphicsUnit.Point);
            Properties.Settings.Default.FontGrid = Font;
            Properties.Settings.Default.Save();
            MessageBox.Show(font.ToString());
        }

Open in new window


I want to check what is written to the FontGrid, and the messagebox gives me
the chosen font, and size but I don't see the style that I have chosen. Is that possible?
Peter
Ah, that was stupid of me.
Thanks for the solution.
500 points are comming to you RolandDeschain.

Peter
Yes, you can use
MessageBox.Show(font.ToString() + " " + font.Style.ToString());

Hope that helps.