Common Dialog Boxes: Font


 

The Font dialog box allows a user to select a font and apply it to a text-based document, paragraph or word.

To create a Font dialog box in MS .Net application, you can use the FontDialog class.

The Font Dialog Box
  1. Start Microsoft Visual Studio .NET
  2. On the Start Page, click New Project (alternatively, on the main menu, you can click File -> New -> Project...)
  3. On the New Project dialog box, in the Project Types tree list, click Visual C++ Projects
  4. In the Templates list, click Managed C++ Empty Project
  5. In the Name edit box, replace the <Enter name> content with The Font Dialog Box
  6. In the Location combo box, accept the suggestion or type your own. If you don't have any, type C:\Programs\MSVC.NET
  7. Click OK
  8. On the main menu, click Project -> Add New Item...
  9. In the Add New Item dialog box, in the Templates list, click C++ File
  10. In the Name box, replace <Enter name> with Main and click OK
  11. Replace the contents of the empty file with the following:
     
    #using <mscorlib.dll>
    #using <System.dll>
    #using <System.Drawing.dll>
    #using <System.Windows.Forms.dll>
    
    using namespace System;
    using namespace System::Drawing;
    using namespace System::Windows::Forms;
    
    __gc class SimpleForm : public Form
    {
    public:
    	SimpleForm();
    private:
    	// Declare a pointer to TextBox
    	TextBox *Editor;
    	FontDialog *dlgFont;
    	Button *btnFont;
    	void btnFontClick(Object *Sender, EventArgs *Args);
    
    };
    
    SimpleForm::SimpleForm()
    {
    	this->Text = S"Font Dialog Box";
    	this->Size = Drawing::Size(460, 320);
    
    	// Initialize the declared TextBox variable
                    Editor = new TextBox();
                    Editor->AcceptsReturn = true;
    	Editor->AcceptsTab = true;
    	Editor->Multiline = true;
    	Editor->ScrollBars = ScrollBars::Vertical;
    	Editor->WordWrap = true;
    	Editor->Size = Drawing::Size(350, 300);
    	Editor->Dock = DockStyle::Left;
    	this->Controls->Add(Editor);
    
    	btnFont = new Button;
    	btnFont->Location = Point(365, 8);
    	btnFont->Text = S"&Close";
    	btnFont->Click += new EventHandler(this, btnFontClick);
    	this->Controls->Add(btnFont);
    
    	dlgFont = new FontDialog;
    }
    
    void SimpleForm::btnFontClick(Object *Sender, EventArgs *Args)
    {
    	if( dlgFont->ShowDialog() == DialogResult::OK )
    		Editor->Font = dlgFont->Font;
    }
    
    int __stdcall WinMain()
    {
    	SimpleForm * FM = new SimpleForm();
    	Application::Run(FM);
    
    	return 0;
    }
  12. Test the application.

 

Related Applications:

Form Design

 

Home Copyright © 2003-2015, FunctionX, Inc.