by Jim Krumm
Click here to go to the Computer Science Department Home Page for Casper College at www.caspercomsci.com.
Below are some windows based applications written in C++ in our MFC class (COSC2405). Conversion from Studio.NET 2003 to NET 2005 to NET 2008 to .NET 2010 and back can be challenging. The latest version of 2010.NET intellisense is missing in both the Express and Professional Editions, making it difficult to determine methods and parameters. This disadvantage is easily overcome by using an older version of Visual C++.NET 2008 which does support intellisense. The code is virtually identical in the 2008 and 2010 environment, making it easy to import or convert projects to the Visual Studio 2010 environment. Though automatic importation is usually possible, Visual C++ 2010.NET does not allow the importation of projects previously made in the 2008 environment employing Visual C++ DataSets in Database projects. One 2008 Visual C++ project is included below linking to a 2007 Access database using canned Visual C++ dataSets (precisely that type that will not import into the 2010 environment). A second database project is included below which links to an Access database directly without using dataSet objects in the Visual C++.NET 2010 and 2008 environment employing SQL commands.
Here a series of programs and tutorials that show how to write windows applications in C++ in the Studio.NET 2010 and 2008 environment.
Tutorial 1: Simple Intro on Setting up a Project and Using Basic Controls in Visual C++.NET 2010, 2008
Tutorial 2: Windows Basic Form Controls in C++ and Their Properties in Visual C++.NET 2010, 2008
Tutorial 3: Windows Forms and Useful ListBox Properties in Visual C++.NET 2010, 2008
Tutorial 5: Menus using Visual C++.NET 2010, 2008
Tutorial 6: File Saving, Opening and Using Common Dialog Boxes using Visual C++.NET 2010, 2008
Tutorial 8: Detecting Asynchronous Keys using Visual C++.NET 2010, 2008
Tutorial 9: Using Arrays of Strings using Visual C++.NET 2010
Tutorial 10: Using SQL to Search and Modify an Access Database in Visual C++.NET 2010, 2008
Tutorial 11: Visual AddressBook using XML, Visual C++.NET 2008 and Access 2007
Tutorial 12: SQL Calls to a MySQL Database in Visual C++.NET 2010, 2008
Tutorial 14: Making A Setup or Deployment Package of a Microsoft Windows Program in C++
Tutorial 1 : Simple Intro on Setting up a Project and Using Basic Controls in Visual C++.NET 2010, 2008 This project will create a simple VB like interface using Windows C++ application in Microsoft Studio.NET 2008 which transfers text from a TextBox to a Label at the click of a button. Click here for the source code to the Click Me Program in Visual C++.NET for the 2010 version or here for the 2008 version. First set up the project by going to: File > New > Project > Visual C++ Projects > Windows Forms Application Then click browse and navigate to the folder to where you want to put your application and then click OK. Name your project something simple like myInterface. Go to the toolbox and add a button, a textbox and a label to your form, and make your form look something like this:
To Change the caption on the button click on the button, and go to the buttons Property Window and select caption and type “Press Me”. Click on the label, go to autosize under properties and set autosize to false. Still with the the label selected, set the borderstyle property to fixed3D. Go to text property for for the label remove all the text. Next double click on the button which will bring up the code window. Add the one line of code between the brace below to the buton1_Click function:
Notice the code you add is almost identical to what would be added in Studio.NET Visual Basic, except an arrow is used instead of a period and the usual C++ semicolon and braces. |
Tutorial 2: Windows Basic Form Controls in C++ and Their Properties Using Visual C++.NET 2010, 2008 This program will show how to transfer an image to a pictureBox and do conversions from Strings to numerical values and numbers to String. Click here for the source code to the Basic Tools and Conversions Program in Visual C++.NET for the 2010 version or here to download the 2008 version. Set up a form that looks like this:
Set picturebox1 to a sizemode of stretchImage. Then within your project add a file called "heads.jpg" having a picture of a penny to your folder containing your .cpp source file and then add the following lines of code to the appropriate buttons functions:
|
Tutorial 3 : Windows Forms and Useful ListBox Properties Using Visual C++.NET 2010, 2008 This program will show how to use listboxes, and some of their more useful properties. Click here for the source code to the ListBox Demo Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. First create a form which looks as follows, including the one listBox shown:
Code the buttons as followings:
Note: If you lose the design window (which often happens when you open the “sln” file using explorer) try the following: Navigate to File > New > Project > Window Form Application To make a design window appear which shows the forms go to view > designer |
Tutorial 4: Simple Animation, Which Shows the Use of the Timer and Location Commands Using Visual C++.NET 2010, 2008 This tutorial will show how to move a control button around the form employing the timer. It also shows something about how placement works and the coordinate system of the form. Click here for the source code to the Simple Animation Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Make the form size 286, 314. Begin by designing a form which looks as follows:
Set the timer's enabled property to false. Then code the following to the buttons:
|
Tutorial 5 : Menus Using Visual C++.NET 2010, 2008 In this tutorial you will add a Menu to make button and textbox appear. Click here for the source code to the Menus Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. To add a menu to your form go to the toolBox and double Click on the the mainMenu tool. This will add a menu to your form. Then type "&File" where it says "Type Here", which add File to the Menu. Then in the box below "File" type "&Make Button Appear" and then "M&ake TextBox Appear", then a lone hyphen (-), and then E&xit. The form should look something like this:
Then add the following code to the appropriate item in the menu by double clicking on this item to get to its Code Window. To add code to specific menu items, double click on the menu item to bring up the code window.:
|
Tutorial 6 : File Saving, Opening and Using Common Dialog Boxes Using Visual C++.NET 2010, 2008 This program will show you how to save to file, open from file and how to use dialog boxes to perform these tasks. Click here for the source code to the File Saving, Opening Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Begin by placing a text file call "myfile.txt" in your inner-most debug directory with a few lines of text placed in it of your choosing. Make this file in Visual Studio by going to >File>New>File>General>Text File. Place this file in the directory with the source (.cpp) you are creating. Then design a form which looks as follow with a listbox to the left and a textBox to the right with multiline property selected. Add a openFileDialog and a saveFileDialog tool to your form. To the right of the listbox is a textbox with its multiline property selected.
Add to the namespace region: using namespace System::IO; Add the following code to the relevant buttons:
|
Tutorial 7: Concatenating Strings Together in the Windows C++ Environment using Visual C++.NET 2010, 2008: This program will show how to put two strings together to create a combination of the two. This called concatenation. Click here for the source code to the Concatenating Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. Below the button is a label with its autosize property set to false. Make your form look something like this:
Add the following code to the button:
|
Tutorial 8 : Detecting Asynchronous Keys Using Visual C++.NET 2010, 2008 This project will allow you to move a label around your form using Asynchronous Keys. Click here for the source code to the Asynchronous Keys Program in Visual C++.NET for the 2010 version or click here for the 2008 version of the source and the project. It is called asynchronous because more than one key can be pressed at a time and it will be recognized. This is usually something that isn't allowed. First set up your form to look as follows (and yes a timer needs to be added). You must include the dynamic link library user32.dll for this program to allow the asynchronous reading of key presses.
Enable the timer.. Next code the following, add everything in bright red:
|
Tutorial 9: Using Arrays of Strings Using Visual C++.NET 2010 In this tutorial you will see how to create arrays of strings. Click here for the source code to the Arrays of Strings Program in Visual C++.NET for the 2010. Make a form which looks as follows and code what is in bright red:
Insert the following code:
|
Tutorial 10: Using SQL to Search and Modify an Access Database In Visual C++.NET 2008, 2010
Click here for the source code to the Concatenating Program in Visual C++.NET for the 2010 version or click here to download the 2008 version. This program shows how to create a simple database program, which uses odbcad32, and an oleDataAdapter to hook up to a Access 2010 database using SQL commands. This program will not use the Data Sources window found in Visual Basic, which probably doesn't exist in Visual C++.NET 2010. First, open Microsoft Access (if you do not have Access 2007 use Access 2005, or a supplied database) and select new blank database. You can download a free redistributable version of Access at http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10910 You may also need the Access database engine at http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d . You may not be able to directly make tables with what is provided in the links, but you will be able to access my database in the sample project. Depending on what version of Windows you have you may have to make some adjustments. There is an issue with 64 bit vs. 32 bit Windows7 (refer to http://support.microsoft.com/kb/942976) which says if you use a 64 bit operating system you must have a 32 bit datasource (database). The code here works in the 32 bit environment. If you wish to go to the 64 bit environment, it has been reported that if you use an older mdb database that you may be able to overcome this issue. Both PhoneBook.accdb written in Access 2010 format and PhoneBook.mdb written in a 2003 format are included in this project as well as all the graphics.
Create a directory called download on your c: drive in the root and save the file as PhoneBook.accdb
To create fields in access for the first name and last name be careful. If you choose a key word in Access the program won't. Specifically it turns out first and last have become key words. For a list of key words that you can't use as column names refer to http://sqlserver2000.databases.aspfaq.com/what-are-reserved-access-odbc-and-sql-server-keywords.html . So make the columns LastName, FirstName, Phone, and fill in as follows:
Try to exit Acess after you have entered your data and it will present the following, save this as is as Table1
Alternative: Save the program as a 2002-2003 mdb database: This should look as follows:
When you finish entering your data go to file and save the table as Table1 (the default). Say yes to creating a primary field. Since it matters where you put your database, it is easiest to put it in the root of your C:\ drive after you have created it. Also be careful, databases may be case sensitive. Close Access and your database before going on! Open My Computer or Windows Explorer, navigate to the C:\windows\system32 and search for odbcad32.exe. Right click on this file as follows and select >send to>desktop. Double click on the odbcad32.exe icon which will start the ODBC Data Source Administrator. Select the System DSN tab and Add In Create New Data Source select Microsoft Access Driver (*.mdb, *.accdb) as shown and click Finish. In the ODBC Microsoft Access Setup Window enter PhoneBook under Data Source Name, leave description blank, and click Select. Navigate to and select your phonebook.mdb in the root of your c: drive and click OK. Click OK under ODBC Microsoft Access Setup This completes configuring the ODBC Administrator so your program will be able access the database. Design a form that looks as follows called NamesDBProgram3 (the large white field in the program below is a textbox with it multi-line function set to true). Go to the tool box and under data items right click, and select choose items as shown below. Add an oleDataAdapter to your form as follows: Under the .NET Framework Components select OleDbDataAdapter as shown below This will add a OleDbDataAdapter to the toolbox. Add the OleDBDataAdapter to your form which will trigger the following window. Select New Connection. This will trigger the appearance of the following dialog window: Select Change, which will cause the following window to appear and select “Microsoft Access Database File” and then OK. This will cause you to return to the Add Connection Dialog which looks as follows. Click Browse and navigate to and select phonebook.accdb and click Open. Don’t include a password but leave Admin as shown. Click on test connection and make sure a connection to the database exists. Then click OK. Back at the wizard click OK. In the Data Adapter Configuration Wizard click Next. In the following dialog window select Use SQL statements. In the next window click on Query Builder… In the next dialog window select Add, then close this “Add Table Window and then click close. Click close to Add Table, and in Query Builder select All Columns, ID, lastName, firstName, and Phone and click on OK. This should lead to the dialog box. Click Finish.
Your design window in Visual C++ should now look as follows: If something goes wrong in setting up your database, it is very, very difficult to fix. If you do have to change something, you may have to go to view, select Server Explorer, right click on the database you just configured and select delete. Then remove the related objects on your form. If this doesn’t clean things out so you get the above window you may have to close your project, open a new one and start over. It may even be necessary to reboot your computer. Add the following lines of code. I have high lighted in yellow some lines that are easy to miss. Make sure to get the code to the form load event and the buttons.
The program looks as follows when it runs:
|
Tutorial 11 : Visual AddressBook using XML, Visual C++.NET 2008 and Access 2007 (Updated 12/11/2011) Click here for the source code to the Visual AddressBook using XML in Visual C++.NET for the 2008. This program will involve using XML to talk to the database, written by Visual Basic using the Data Sources Tool which is no longer available in Visual C++.NET 2010 (but hopefully will be again in the future.) Hence it is impossible to make this program in the 2010 MSVC++.NET environment (and probably future MSVC versions). Open Microsoft Access (if you do not have Access 2007 use Access 2005, or a supplied database) and select new blank database. You can download a free redistributable version of Access at http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10910 You may also need the Access database engine at http://www.microsoft.com/downloads/en/details.aspx?displaylang=en&FamilyID=c06b8369-60dd-4b64-a44b-84b371ede16d . You may not be able to directly make tables with what is provided in the links, but you will be able to access my database in the sample project. Depending on what version of Windows you have you may have to make some adjustments. There is an issue with 64 bit vs. 32 bit Windows7 (refer to http://support.microsoft.com/kb/942976) which says if you use a 64 bit operating system you must have a 32 bit datasource (database). The code here works in the 32 bit environment. If you wish to go to the 64 bit environment, it has been reported that if you use an older mdb database that you may be able to overcome this issue. Both PhoneBook.accdb written in Access 2010 format and PhoneBook.mdb written in a 2003 format are included in this project as well as all the graphics.
Create a directory called download on your c: drive in the root and save the file as PhoneBook.accdb
To create fields in access for the first name and last name be careful. If you choose a key word in Access the program won't. Specifically it turns out first and last have become key words. For a list of key words that you can't use as column names refer to http://sqlserver2000.databases.aspfaq.com/what-are-reserved-access-odbc-and-sql-server-keywords.html . So make the columns LastName, FirstName, Phone, and fill in as follows:
Try to exit Acess after you have entered your data and it will present the following, save this as is as Table1
Alternative: Save the program as a 2005 mdb database: This should look as follows:
When you finish entering your data go to file and save the table as Table1 (the default). Say yes to creating a primary field. Since it matters where you put your database, it is easiest to put it in the root of your C:\ drive after you have created it. Also be careful, databases may be case sensitive. Close Access and your database before going on! Start a project in Visual C++ Studio 2008 as a C++ windows form application called AddressBook. Go the ToolBox and right click on the toolbar. Select Choose items.
From “Choose Toolbox Items” select Select OleDbDataAdapter and click OK. This will take you back to the form and add the OleDbDataAdapter tool to the toolbox.
Right click on the toolbar. Select add new items. Select OleDbDataAdapter. Draw the OleDbDataAdapter on the form. In the window that appears select New Connection.
Select Change.
Select Microsoft Access Database File
Click Browse and browse to the AddressBook.mdb database on the root drive and then select OK.
Say Next
Say Next
Click Query Builder
Click Add and then Close
Select ID, LastName, FirstName and Phone and click OK. Test by clicking execute Query. Then click OK
Click advanced Options
Click OK
Click Finish.
Below your form click on the oleDbAdapter1 tool and an arrow will appear on its top. Click on this arrow as shown below and select Generate DataSet.
Select OK in the next Window.
Click Data on the Form, and select Show Data Sources
Expand Table 1 in the Data Sources Window as shown below.
Drag and Drop the LastName, FirstName, and Phone Tags to your form from the Data Sources Window. When you do this labeled TextBoxes will appear on your form corresponding to the tags in the data sources window.
Add a data grid view and click on the arrow on the top right corner and select choose data source
Select Table1BindingSource
After the addition of the data grid view and making a link with the Table1BindingSource the form should look as follows:
Add the following buttons, groupbox, labels, textboxes and a picturebox with its sizemode set to stretch and open a default no image (saved as "Default.jpg") into the picturebox. To add pictures to your project add jpg's saved as a person's first name+last name+".jpg". You are welcome to use my default.jpg or create your own.
Insert the following code:
The final program should look as follows:
This is the way the program works: 1. First Item, Next Record and Last Record allow you to navigate through the records. 2. If you wish to add a record you must click Add Item. Then enter in the last, first and phone fields to the left the information. Then click Save Database. 3. To find someone in the database, you must enter their first and last name in the find groupbox to the right. Then click the Find Record button. 4. If you wish to delete a record, first find them. Then click Delete Record. Then click Save Database. 5. If you wish to see all the records again after a find, click Reset Record. 6. Cancel is for canceling an Add Record or a Delete Record, before you hit save. 7. To update a record either navigate to the person's record or do a Find Record. Then on the left edit first, last, or phone. Then click the Save Database button. To view this record and the rest click Reset Record. |
Tutorial 12: SQL Calls to a MySQL Database in Visual C++.NET 2010, 2008 (Updated 12/11/11) 1. Download the MySQL database server/client: at http://www.mysql.com and select mysql community server>mysqlcommunity
server>windows>windowsx86 zip/setup>pick a mirror>new user>No
thanks just take me to the downloads> Then install the executable by selecting custom. In the second window (as shown below) select that developer components should be installed to your hard drive, but go with the defaults on everything else. To download the VC.NET 2010 verion of the MySQL Database Project click here or for the 2008 version click here.
Then select skip sign-up. Go with Detailed configuration.>Developer Machine>Multifunctional Database>C: Installation Path>Decision Support (DSS)/OLAP>Enable TCP/IP Networking>Standard Character Set>Install as Windows Service and Include Bin Directory in Windows Path >Modify Security Settings (just done within MySQL…not as settings on your computer) and type a root password (don’t forget this…or you will have to reinstall it. Here I set the password to "student") >Enable root access from remote machines>Execute
2. Download the MySQL ODBC driver (open database connectivity)at database products>MySQL drivers>ODBC Driver for MySQL >Download>windows>windows x86 zip/setup. Install as follows by first clicking Next as shown below:
Next Select Typical as shown below and click next:
Then click install as shown below:
Here I am using MySQL Connector/ODBC 3.51.
3. Create Database and Tables by going to Start>All Programs>MySQL>MySQL Server 5.0 >MySQL command line client>(you are now at a command line) type the password (I used "student") entered and hit enter> Then enter the following given in yellow:
It is important to note that a single database file has not been created (unlike Access), rather it is put in a collection of database files usually in a common set of files. Also in MySQL it is appropriate to name columns first and last if you wish.
4. Making a Program in Visual C++.NET 2010:
A. Go to New Project>Visual C++>Windows Form Application: Name the programi MyMySQLVCProgram.
B. Make the following form:
C. Enter the following code to the form : This program does not use any of the built in database tools in the toolbox for Microsoft Visual C++.NET. It makes the link to the database from scratch. When running the program, hit the open connection button before executing any of the other buttons, and close the connection when finished. Particularly make sure the code highlighted in yellow is included.
To run the program: 1. Click Open Connecton before pressing any other button 2. Perform any of the operations on any of the other buttons. 3. To quit click Close Connection. This is what the program looks like when it runs:
|
Tutorial 13: Creating a Visual C++ Project from Scratch in 2010 that Calls an Access Database in Visual C++.NET 2010 (No XML) (Updated 12/11/2011) The Form: Click here for a complete project with source in MSVC++.NET 2010. This program runs and compiles in 32 Bit Windows XP. The executable should run in Windows 7, 64 bit, provided the database is present, and the path adjusted to the database. But development can an issue because of the 32 bit vs. 64 bit drivers. I have a student who reports that using a older mdb database may resolve the issue with working on 64 Windows 7 using a 32 bit database, and then using the older Jet driver instead of ACE. At any rate, this program uses a database set inside it's innermost debug directory (which may have to be adjusted in Windows 7). A 2010 accdb database and a 2002 mdb database (with a working but remarked connection string) is included in the above zip files. If you run the 2002 mdb database and need the Microsoft jet driver refer to http://support.microsoft.com/kb/239114. This program does not use Odbcad32. However to make this work you will need to at least install the Access Runtime available as a free download from Microsoft. To download the Access Runtime go to http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=10910 and/or the Microsoft Access Database Engine at: http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=13255 This is the best way to set up the project. It does not rely on any special tools in MVC++.NET, and you form the link to the database right in your code. The code works with 2008 and 20010. Begin by creating a project called myPhoneBook and designing the form as shown below. Note, by default the textboxes will start with a lower case (t)extBox, so you will need to rename these with an uppercase (T)extBox to use my code below:
Database and Pictures: The way I have the database here it assumes your database is in the root of your C: drive. If you have Windows 7 you may have to place the database in with your source files and adjust the path to your database in your source file accordingly. Copy new pictures of people in the address book folder into the source file directory using the convention of firstname+lastname+”.jpg” . Insert the Following Code:
|
©All rights reserved by James Krumm. Originally made available at www.caspercomsci.com. Materials here can be used, and redistributed, provided proper reference is made to the origin and author(s) of these materials. Please send any corrections or suggestions to jkrumm@caspercollege.edu. Last modified May 15, 2012.
To find out more about the Casper College Computer Science Program contact us at:
James Krumm |