Differences between VB6 and VB.NET for COP3175
Some properties that are different
VB6 |
VB.NET |
Label.Caption |
Label.Text |
Form.Caption |
Form.Text |
ListBox.Clear |
ListBox.Items.Clear( ) |
ListBox.List(0) |
ListBox.Items.Item(0) |
ListBox.ListCount |
ListBox.Items.Count |
ListBox.AddItem |
ListBox.Items.Add |
Using a DataGrid control to view a table from an Access database
The steps for filling a data grid with data from an access table is completely
different in VB.NET. There is no longer an ADO Data Control to tie the connection
to the data grid. In its place is a DataAdapter and a DataSet.
The DataAdapter handles the interface between VB and the Access database.
The DataSet contains a copy of the data that is in the database. The
DataSet uses the DataAdapter to connect to the database.
-
Creating a connection
-
Select the View menu
-
Select Server Explorer
-
Right-click the Data Connections entry and click Add Connection
-
This will bring up the Data Link Properties. This is the same property page
the VB6 displays. Set the Provider and the Connection as we did in class
for VB6. Remember to test the connection.
-
Drag the new connection onto your form
-
Creating an adapter
-
From the Toolbox, select the Data tab
-
Select OleDbDataAdapter
-
Click on your form. This will bring up the data adapter wizard.
-
Select the connection that you just created.
-
Select Next on the following two screens
-
Select Query Builder when you get to the Generate the SQL
Statments page
-
You can now build a query. This process is similar to what we did in class,
but it has a GUI to help build the query.
-
Creating a data set
-
Click on your form in the form designer.
-
Select the Data menu, and select Generate DataSet
-
Be sure that New is selected
-
Select the table(s) that you want in the data set
-
Connecting the DataSet to the DataGrid
-
From the Toolbox, select the Windows Forms tab.
-
Add a DataGrid to your form.
-
Select the DataSource property.
-
Select the DataSet you just created, along with the name of the table you
are referencing.
-
Filling the DataSet with data. In VB.NET you still need to add a line of
code to your project in order to fill the data set. In VB6 this step is not
necessary.
-
In the form editor, double-click on the form itself. This will add an event
handler to your code for when the form is loaded.
-
Add the following code. I am using the default names, your names are probably
different. I am connecting to the Authors table in Biblio.mdb.
OleDbDataAdapter1.Fill(DataSet1, "Authors")
-
In order to change the SQL statement that controls what is displayed in the
DataGrid:
-
Open the properties fir the DataAdapter
-
Choose the SelectCommand property
-
Choose the CommandText sub-property
-
Click the ... to open the Query Builder
-
Here is an example of opening the Authors table of Biblio.mdb:
db.zip. You can download it and run it through VB.NET.
You may have to change the location of Biblio.mdb. Here is a link to
Biblio.mdb in case you don't have it on your home
system.
Changing Properies in VB.NET
-
Changing colors
-
The easiest way is to use
Color.Red
or some other common color.
As soon as you type Color.
in VB.NET, you will get a choice
of colors.
lbl.BackColor = Color.Red
-
You can also be more creative by specifying the levels of red, green, and
blue. Each number is from 0 to 255.
lbl.BackColor = Color.FromArgb(255,0,0)
-
You can also change it using the VB6 format, but that is more difficult because
you won't know the numbers to plug use
lbl.BackColor = ColorTranslator.FromOle(&HFF)
-
For other properties, like alignment and border style, VB.NET will give you
a list of choices after you type the =
lbl.BorderStyle = [choose from the list]
-
For the font, use a statement like this. It will set the name of the font
and its size.
lbl.Font = New Font("Arial", 15)