I have a two window basic system not using System.Windows.Forms but System.Windows.Controls, one of them creates a FontDialog, so I had to include the System.Windows.Forms, because otherwise I wouldn’t be able to have the font dialog. In the other window I have a RichTextBox which should use the font/size/style selected in the FontDialog of the other window.
This is the class for the window that creates the FontDialog:
public partial class Window1 : Window
{
public FontDialog font;
public Window1(String name)
{
InitializeComponent();
this.textBox1.Text = name;
}
private void button2_Click(object sender, RoutedEventArgs e)
{
font = new FontDialog();
font.ShowDialog();
}
}
And here I call the Window1 and try to use its font.
private void button2_Click(object sender, RoutedEventArgs e)
{
Window1 a = new Window1(this.name);
a.ShowDialog();
var cvt = new FontConverter();
string s = cvt.ConvertToString(a.font.Font);
Console.Out.WriteLine("Value is: " + s);
System.Windows.Media.FontFamily g = (System.Windows.Media.FontFamily) cvt.ConvertFromString(s);
if (g != null)
{
this.textBox2.FontFamily = g;
}
}
It outputs exactly what is selected in the FontDialog, but then it crashes at the line “this.textBox2.FontFamily = g;”:
Value is: Microsoft Sans Serif; 8,25pt
'_.NetworkingGT_Incrementer.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\WindowsBase.resources\v4.0_4.0.0.0_pt-BR_31bf3856ad364e35\WindowsBase.resources.dll'
A first chance exception of type 'System.ArgumentException' occurred in WindowsBase.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
A first chance exception of type 'System.Reflection.TargetInvocationException' occurred in mscorlib.dll
'_.NetworkingGT_Incrementer.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\Windows\Microsoft.Net\assembly\GAC_MSIL\System.Xaml.resources\v4.0_4.0.0.0_pt-BR_b77a5c561934e089\System.Xaml.resources.dll'
A first chance exception of type 'System.Xaml.XamlObjectWriterException' occurred in System.Xaml.dll
A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
An unhandled exception of type 'System.Windows.Markup.XamlParseException' occurred in PresentationFramework.dll
Solved:
Both outputs are equal.