Click to See Complete Forum and Search --> : [RESOLVED] [1.0/1.1] Converting
francisstokes
Apr 16th, 2006, 07:50 AM
I thought a good way to learn C# would be to re-write all the VB.NET apps i've already written. To start small, i went with a RichText editor, but i can't get the font dialog to work. It shows up, but it won't change the text. Heres the code im using:
private void fontDialog1_Apply(object sender, System.EventArgs e)
{
rtb.SelectionFont = fontDialog1.Font;
}
Kasracer
Apr 16th, 2006, 08:07 AM
The font dialog doesn't return a DialogResult.OK?
jmcilhinney
Apr 16th, 2006, 08:32 AM
The Apply event is raised when the user clicks the Apply button on the dialogue, not the OK button. If you haven't set the ShowApply property of the dialogue to True then there is no Apply button and that event can never be raised. Normally you would test the result of ShowDialog and if it is OK then apply the font. If you have set the ShowApply property to True then you would also handle the Apply event and use the same method to set the font. The Apply event is provided because pressing the Apply button does not dismiss the dialogue. private void button1_Click(object sender, System.EventArgs e)
{
if (this.fontDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
// The user pressed the OK button.
this.applyFont();
}
}
private void fontDialog1_Apply(object sender, System.EventArgs e)
{
// The user pressed the Apply button.
this.applyFont();
}
private void applyFont()
{
this.richTextBox1.SelectionFont = this.fontDialog1.Font;
}
francisstokes
Apr 16th, 2006, 09:26 AM
Well, i didn't know....the 'private void fontDialog1_Apply' code was generated by the Borland C# builder when i doubled clicked the font dialog.
Thanks for your replies, you solved my problem.
jmcilhinney
Apr 16th, 2006, 09:49 AM
Apply is the default event for the FontDialog, thus a handler is created for it when you double-click it, just like every component. The same would happen in the Microsoft VB IDE. With most dialogues though, it's the return value of ShowDialog that you're most interested in.
francisstokes
Apr 16th, 2006, 10:06 AM
It funny that its default event is Apply, yet, by default it doesn't add an apply button...
jmcilhinney
Apr 16th, 2006, 07:09 PM
The FontDialog only has two events: Apply and HelpRequest. It doesn't show a Help button by default either, and I guess they judged that Apply would be used more often so that should be the default. I guess you would expect that it would have an event that corresponded to the OK button being pressed, like the FileDialogs do. Not that that event gets used much anyway as most people just test the return value of ShowDialog anyway. It would be a bit more intuitive though.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.