Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

53
Using Multiple Forms

Transcript of Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Page 1: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Using Multiple Forms

Page 2: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Creating a New Form

Project Add Windows Form

Page 3: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Creating a New Form

Select Windows

Form

Name of the form

Page 4: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Creating a New Form

You can set

properties of the form as usual.

Page 5: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Adding Controls to the Form

Controls

Drag and drop

controls

Page 6: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Button Properties

Button

Properties should be

set

Page 7: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Making “Button” Functional

EventsProperties

Click Event

Double click to create a

“handler” to the “Click” event.

Page 8: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Generated Code

Page 9: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Creating and Opening new Form

Creating a new form object frmNew frm = new frmNew ();

Modal and Modeless Forms Modal -> frm.ShowDialog(this); Modeless -> frm.Show();

Class name

Name of the object

“new” keyword Constructor

Page 10: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Creating and Opening new Form - Code

Page 11: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Creating and Opening new Form - Result

Page 12: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Closing the Form

Code

To write the code that will close the form, first add a button to the form named “Close”. Then double click on the button and add the code below.

Page 13: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Transferring Data between forms

Send data via constructor.

Constructor

Page 14: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Example

Add a new ListView to frmMain.

Page 15: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

ListView Properties

Page 16: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Adding Columns to ListView

Page 17: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Adding Columns to ListView

Page 18: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Working with data

Operations: Add/Delete/Modify

We will need to create a new form for addition and modification operations.

Deletion operation will delete selected record.

Page 19: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Add/Delete Data

To perform deletion and modification operations, first we add two new buttons to the form and assigning appropriate properties.

Page 20: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Add new record

To transfer data, first define a “struct” named Uye.

Page 21: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Add/Modify Form

Create a new form named “frmKayitEkleDuzelt” to perform add and modify operations. Add three textboxes and labels, one OK and one Cancel buttons to the form.

Page 22: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Displaying records in add/modify form

Get data in Uye structure via constructor and assign to the controls.

Assign Operation

Page 23: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

OK and Cancel Buttons

How can we understand that user pressed OK or Cancel buttons?

How to send data back?

Page 24: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Solution

First, define a global Uye structure.

Page 25: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Solution - Continues

Double click to OK and Cancel buttons, add following codes to handler functions.

Structure to be returned.

Page 26: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Solution - Continues

Create a public function on “frmKayitEkleDuzelt”form. This function will return the global struct m_Uye which is created by OK button click.

Code that will return structure.

Page 27: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Main form- Add

To add a new record, add following code to the handler function of the Ekle button

Empty Uye structAdding record

formIf OK button is pressed

Add new element to listview

Page 28: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Main form - Modify

To modify a record let’s create a handler function to double click event of the listview. When a row is double clicked, we will open it to be edited.

To achieve this, create a event handler function for double click event of listview.

Page 29: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Main form - Modify

Add following code to handler function.

Record to be modified

If OK button is pressed

At least one row is selected.Selected

element

Modify record

Page 30: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Main form - Delete

To delete selected row, create a handler function for Sil button.

Page 31: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Main form - Delete

Add following code to handler function.

At least one element is selectedIf user is sure

of what she is doing

Remove selected element

from listview

Page 32: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Main Menu

Main menu is the control that resides upper section of the form.

It provides easy access for frequently used operations.

Page 33: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Adding Main Menu

MainMenu is added to the form by dragging and dropping from.

Page 34: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Adding new element to the Main Menu

To add new items to the MainMenu, just type the name of the element to the areas written “Type Here” in the MainMenu.

Page 35: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Making menu element operational

Just double click on the menu element to create handler function to their default events.

You may enter whatever code you wanted to the handler function.

Page 36: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Writing to a file

In C# language System.IO namespace is used for file operations.

To enable Turkish language support, it is suggested to use “StreamWriter” class.

To get file name to be written from user, you may use SaveFileDialog class.

Page 37: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Example

Add following code to the click event handler function of the MainMenu element named “Save”.

Page 38: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

SaveFileDialog

SaveFileDialog used used for asking to user which file will be selected for write operation.

Create SaveFileDialog

object

Filter definition

Show dialog box and decide whether OK button is pressed.

Page 39: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

StreamWriter

This object is used for writing to a file. Its constructor accepts two arguments, one for file name to be written, one for “encoding”which decides code page. WriteLine function write data to the file.

Create StreamWriter object

Add Turkish support

Write a line to the file

Create special formatted strings Get data from

listview

Page 40: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – New record

Page 41: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – New record

Page 42: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Modify a record

Page 43: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Modify a record

Page 44: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Saving to a file

Page 45: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Saving to a file

Page 46: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Saving to a file

Page 47: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Read from file

As in writing file, System.IO name space includes classes which are used fro reading from a file.

Use “StreamReader” class for Turkish language support.

Use OpenFileDialog class to ask user which file is meant to be read.

Page 48: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Example

Add following code to the click event handler function of the “Read from file” menu item.

Page 49: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

OpenFileDialog

OpenFileDialog is used for selecting the file name to be opened for reading.

Show dialog box and decide whether OK button is pressed.

Define filterCreate

OpenFileDialog object

Page 50: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

StreamReader

This is the object that reads from a file. Its constructor accepts two arguments, one for file name to be read, one for “encoding” which decides code page. ReadLine function reads one line from file.

Create StreamReader

Read a line from file

Add read data to the listview

Read with Turkish support

Page 51: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Read from a file

Page 52: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Read from a file

Page 53: Using Multiple Forms. Creating a New Form ProjectAdd Windows Form.

Try the program – Read from a file