Kamran Ahmed

Visual Inheritance in C#



You can use visual inheritance to create several forms that are closely related to each other. Visual inheritance allows you to create a form that incorporates all the members, controls, menus, and code associated with a preexisting form, and use it as a base for additional functionality. This allows you to create a single form that incorporates elements common to the entire interface, and then individually tailor each form to its specific purpose. To use inheritance with forms, you can either use the Inheritance Picker or code to create the inheritance relationship. Inheritance is discussed in greater detail in Chapter 4.

To create an inherited form with the Inheritance Picker

  1. From the Projects menu, select Add Inherited Form.
The Add New Item dialog box opens.
  1. In the left pane of the dialog box, choose Local Project Items. In the right pane, select Inherited Form. Name this form in the Name box, and click Open to open the Inheritance Picker.
  2. The forms in your project are displayed in the Inheritance Picker. If the form you want to inherit from is one of these forms, choose the form from which you want to inherit and click OK.
A new inherited form is added to your project.
  1. If you want to inherit from a form outside of your project, click Browse. Navigate to the project containing the form you want. Click the name of the DLL file containing the form and click Open.
This returns you to the Inheritance Picker dialog box where the selected project is now listed. Choose the appropriate form and click OK. A new inherited form is added to your project.
NOTE

To use the Inheritance Picker, the form from which you inherit must either be in the project or be compiled in an EXE or DLL file.

To create an inherited form in code

  1. From the Projects menu, select Add Windows Form.
A new form class is added to your project.
  1. Open the code editor for your new form. Modify the class declaration (C#) or use the Inherits keyword (Visual Basic) to specify the inherited form as indicated by the example:

Visual Basic .NET

' This example assumes that you are inheriting from a form class

' named MainForm and that that form resides in your project

Public Class myForm

   Inherits MainForm

   ' Additional class implementation omitted

End Class

Visual C#

// This example assumes that you are inheriting from a form class

// named MainForm, and that that form resides in your project

public class myForm : MainForm

{

   // Additional class implementation omitted

}
NOTE

To use inheritance in code as shown in the preceding example, your project must either contain a reference to the assembly that contains the form you are inheriting from, or that form must be a member of your project.


Create speech application

1.     Create an interop DLL
Since SAPI is a COM component, an interop DLL is needed to use it from a managed app. To create this, open the project in Visual Studio. Select the Project menu and click Add Reference. Select the COM tab, select "Microsoft Speech Object Library" in the list, and click OK. These steps add this reference to your project and create an Interop.SpeechLib.dll in the same folder as your executable. This interop DLL must always be in the same folder as your .exe to work correctly.
2.     Reference the interop namespace
Include this namespace in your application. In C#, add "using SpeechLib;"; iIn VB, add “Imports SpeechLib”.
  Dim voice As SpVoice
        voice = New SpVoice
        voice.Speak(TextBox1.Text, SpeechVoiceSpeakFlags.SVSFDefault)

No comments:

Post a Comment