Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
My question is in the title ...
I can get the name, the size, but not the Style..What do you miss me?
Thank you


What I have tried:

<pre lang="c#"> 
                   FontDialog Dlg = new FontDialog
                {
                    ShowColor = false,
                    ShowApply = false,
                    ShowEffects = false,
                    ShowHelp = false,
                   //   AllowVerticalFonts = false,
                    //  AllowScriptChange = false,
                   //   FixedPitchOnly = true,
                    MinSize = 9,
                    MaxSize = 16
                };


 if (Dlg.ShowDialog() == DialogResult.OK)
                {                 
                    string fontName = Dlg.Font.Name;
                     fontStyle = Dlg.Font.Style; // This code does not work ....
                    float fontSize = Dlg.Font.Size;

                    TxtPolice.Text = fontName;
                    TxtStyle.Text = fontStyle; // This code does not work ....
                    TxtFont.Text = "" + fontSize;
                }
Posted
Updated 14-May-19 20:17pm
Comments
Richard Deeming 14-May-19 13:29pm    
What does "does not work" mean? Provide the full details of any errors you're getting.

C#
fontStyle = Dlg.Font.Style;
You are using here a fontStyle variable which it seems you did not declare. Try
C#
System.Drawing.FontStyle fontStyle = Dlg.Font.Style;
instead. You can also import System.Drawing namespace so that you can reference the FontStyle enumeration in a shorter way.
 
Share this answer
 
Comments
BillWoodruff 15-May-19 22:57pm    
relevant, but does not address the main issue.
Hello,
Thank you, but I have solved my story ....
Here is the complete code:


C#
FontDialog Dlg = new FontDialog
         {
             ShowColor = false,
             ShowApply = false,
             ShowEffects = false,
             ShowHelp = false,
            //   AllowVerticalFonts = false,
             //  AllowScriptChange = false,
            //   FixedPitchOnly = true,
             MinSize = 9,
             MaxSize = 16
         };


         if (Dlg.ShowDialog() == DialogResult.OK)
         {
             string fontName = Dlg.Font.Name;
             Object fontStyle = Dlg.Font.Style;
             float fontSize = Dlg.Font.Size;

             TxtPolice.Text = fontName;
             TxtStyle.Text = fontStyle.ToString();
             TxtFont.Text = "" + fontSize;
         }
 
Share this answer
 
Comments
phil.o 14-May-19 13:43pm    
A very bad habit to use an object where you can use the proper type. Please see my answer.
BillWoodruff 15-May-19 22:56pm    
Every one of those six Fields declared in the scope of the if clause will not exist outside the if clause, and can never be used.
C#
FontDialog Dlg = new FontDialog
                {
                    ShowColor = false,
                    ShowApply = false,
                    ShowEffects = false,
                    ShowHelp = false,
                   //   AllowVerticalFonts = false,
                    //  AllowScriptChange = false,
                   //   FixedPitchOnly = true,
                    MinSize = 9,
                    MaxSize = 16
                };


                if (Dlg.ShowDialog() == DialogResult.OK)
                {                 
                    string fontName = Dlg.Font.Name;
                    FontStyle fontStyle = Dlg.Font.Style;
                    float fontSize = Dlg.Font.Size;

                    TxtPolice.Text = fontName;
                    TxtStyle.Text = fontStyle.ToString();
                    TxtFont.Text = "" + fontSize;
                }
 
Share this answer
 
Comments
BillWoodruff 15-May-19 22:55pm    
Every one of those six Fields declared in the scope of the if clause will not exist outside the if clause, and can never be used.
LSB71 19-May-19 2:55am    
Hello,
Thank you, how should I declare them then?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900