Creating Multiple Forms in C# .NET

|
There aren't many programmes that have only one form. Most programmes have other forms that are accessible from the main one that loads at start up. In this section, you'll learn how to create programmes with more than form.
The programme we'll create is very simple one. It will have a main form with a text box and a button. When the button is clicked, it will launch a second form. On the second form, we'll allow a user to change the case of the text in the text box on form one.
Here's what form one looks like:
Change Case Form
Design the above form. Set the Name property of the text box to txtChangeCase. For the Text property, add some default text, but all in lowercase letters. Set the Name property of the button to btnFormTwo.
Adding a new form to the project is easy. Click Project from the menu bar at the top of the Visual C# software. From the Project menu, select Add New Windows Form. You'll see the Add New Item dialogue box appear. Make sure Windows Form is selected. For the Name, leave it on the default of Form2.cs. When you click OK, you should see a new blank form appear:
Form Tabs in Visual C# .NET
It will also be in the Solution Explorer on the right:
A second form showing in the Solution Explorer
Adding the form to the project is the easy part - getting it to display is an entirely different matter!
To display the second form, you have to bear in mind that forms are classes. When the programme first runs, C# will create an object from your Form1 class. But it won't do anything with your Form2 class. You have to create the object yourself.
So double click the button on your Form1 to get at the coding window.
To create a Form2 object, declare a variable of Type Form2:
Form2 secondForm;
Now create a new object:
secondForm = new Form2();
Or if you prefer, put it all on one line:
Form2 secondForm = new Form2();
What we've done here is to create a new object from the Class called Form2. The name of our variable is secondForm.
To get this new form to appear, you use the Show( ) method of the object:
secondForm.Show();
Your code should now look like this:
C# code to create a second form
Run your programme and test it out. Click your button and a new form should appear - the blank second form.
However, there's a slight problem. Click the button again and a new form will appear. Keep clicking the button and your screen will be filled with blank forms!
To stop this from happening, move the code that creates the form outside of the button. Like this:
C# code to Show the form
Try your programme again. Click the button and you won't get lots of forms filling the screen.
In the next lesson, you'll learn what a Modal form is.

Update a Record, Delete a record

|

Update a Record

Sometimes, all you want to do is to update a record in the database, change a name, for example, or amend an address.
Add a new button to your form. Call it btnUdate. Double click the button to get at the code. Add the following lines:
DataRow row = ds.Tables[0].Rows[inc];
row[1] = txtFirstName.Text;
row[2] = txtSurname.Text;
row[3] = txtJobTitle.Text;
row[4] = txtDepartment.Text;
This is very similar to the code for adding a new record. Again we start by creating a DataRow object. This time, we place a specific row in the new object. That row is one from the DataSet:
ds.Tables[0].Rows[ inc ];
However, this is the old row. The new details are in the textboxes, the ones you may have amended. To place the new details into the new row object, we simply transfer the textbox date to a position in the row object:
row[1] = txtFirstName.Text;
row[2] = txtSurname.Text;
row[3] = txtJobTitle.Text;
row[4] = txtDepartment.Text;
All we need to do now is to make a call to the UpdateDatabase method from our class. Again, we can do this in a try … catch block:
try
{
objConnect.UpdateDatabase(ds);
MessageBox.Show("Record Updated");
}
catch (Exception err)
{
MessageBox.Show(err.Message);
}
The only line we really need is this one:
objConnect.UpdateDatabase(ds);
This is the same as before: we're just passing our UpdateDatabase method the name of the DataSet, which is called ds.
The whole of the btnUpdate code is this:
C# code to update a record in a database table
Try it out. Run your form and amend one of the records. Click your Update button. When you close the form down and open it back up again, the amended record will display.

Delete a Record

To delete a record from the Dataset, you use the Delete method on the DataSet:
ds.Tables[0].Rows[inc].Delete( );
This is enough to Delete the entire Row ( Rows[inc] ). But it is only deleted from the Dataset. To delete if from the underlying database as well, we can call our UpdateDatabase method again.
objConnect.UpdateDatabase(ds);
Because a row has been deleted, we need to change the value of our MaxRows variable. We also need to deduct 1 from the inc variable, and make a call NavigateRecords:
MaxRows = ds.Tables[0].Rows.Count;
inc--;
NavigateRecords();
Add another button to your form. Call it btnDelete. Here's the whole of the code to add to your new button:

C# code to delete a record from a database table
Try it out. Run your form. Navigate to a record you want to delete, then press your Delete button. Close your form and reopen it. The record you delete should be gone.

Exercise P
Examine this version of our form:
The completed C# database form
If you look at the bottom, you'll see a label that says Record 1 of 10. Implement this in your own programme. If you set up a method, you can just call it from NavigateRecords.


OK, that's enough of databases! It's a huge subject, obviously, and many books have been written on the subject. We've only touched the surface in these lessons, and encourage you to delve deeper. Especially if you want a job as a programmer!
In the next section, we'll take a look at multiple forms.