CV31 Passing Data Between Multiple Forms

preview_player
Показать описание

Рекомендации по теме
Комментарии
Автор

Form 1 5:38
Form 2 6:32

Methods in second form will be called in the first form:
int iRes =
Specify form 2 contains the method and variable.

graytabbycat
Автор

9.6
*Creating Multiple Forms in a Project*

• Every project that has a form in a Visual C# has a class.
–The default name of the form is Form1 and its class is also named Form1 which is stored in the Form1.cs file.
• A Visual C# project can have multiple forms.
–Each form has its own class that can be instantiated and displayed on the screen.
• When you add additional forms to a project, you add additional classe(s), which are stored in their own files.
• When you create event handler for a specific form's controls, you write them as methods in the form's class.


*Displaying a Form*

• To display a form in your application, you need to create an instance of the form's class. E.g.

ErrorForm myErrorForm = new ErrorForm();

• The above code:
–declares a reference variable named myErrorForm;
–creates an object of the ErrorForm class in memory; and
–assigns a reference to the object to the myErroForm variable.

• After creating the instance, use ShowDialog method
to display the form. E.g.

myErrorForm.ShowDialog();

• The ShowDialog method displays a form on the screen and
it gives that form the focus.


*Accessing Control on a Different Form*

• Controls provided by the .NET Framework have private access by default.
• This means that a control that exists on a particular form can be accessed only by code that is written in that form's
class.
• You can change the access of a control by changing its Modifiers property in the Properties window.
• If you change a control's Modifiers property to Public, the control can be accessed by code outside the form's class.


*Accessing Control on a Different Form*

• For example, suppose GreetingsForm has a Label control named messageLabel, and the messageLabel control's Modifiers property has been set to Public.
• The following code shows how you can create an instance of GreetingsForm, assign a value to the messageLabel control's Text property, and then display the form:

















Modal
and Modeless Forms


•A modal
form or dialog box must be closed or hidden
before you can continue working with the rest of the application.


–When a modal
form is displayed, no other form in the
application can receive the focus until the modal form is closed.





•A modeless
form allows the user to switch focus to
another form while it is displayed.


–The user does not have to close a
modeless form to switch focus to another form.


Flow
of Executions

graytabbycat