Title: Visual Studio .NET
1Visual Studio .NET
- Introduction to Visual Studio .NET
2Creating a project
- At the Start screen, select New Project
- Select Visual C Projects on the left
- Select MFC Application on the right
- Enter a Name for the project and click OK
- Select Application Type on the left
- Select Dialog Based
- Click Finish to create the project
3Creating a Console application
- Select Win32 project from the New Project dialog
- Name the project and click OK
- Select Application Type and choose consol
application - Click Finish to create the project
- Dont forget to include ltiostreamgt, using
namespace std etc - To create a new .cpp or .h file to use in the
program, go to File -gt Add New Item to add it to
the solution
4.NET Interface
ToolBox
Tabs
Resource/Solution Pane
- The .NET Interface consists of a main window
pane, with a toolbox on the left, resource
view/solution explorer on the top right, output
window on the bottom, and a property pane to the
bottom right. The interface also has the usual
menus and toolbars at the top. The top of the
main pane has tabs which allow you to navigate
through the files youre using.
Output Window
Main Pane
Properties Pane
5Resource View
- To enable the resource view, press CTRLShiftE,
or select it from the view menu at the top - Once the resource view is enabled, you can view
it by selecting its tab on the bottom of the top
right pane - Upon opening an existing project, you can open
the main dialog screen by navigating to
example-gtexample.rc-gtDialog -gtIDD_EXAMPLE_DIALOG,
where example is the name of your project
6Tabs
- These tabs are located above the main pane
which normally contains code. - Each tab represents a different file that you
have open within the project. - To switch between files, merely select a
different tab. If you desire to close a certain
file. Select the tab and then the x on the far
right of the toolbar. - These buttons allow you to scroll horizontally
when you have enough files open.
7Solution Explorer
- To enable the solution explorer, press
CtrlAltL, or select it from the view menu at
the top - The solution explorer allows you to navigate
through files associated with your project-- .cpp
files will be under the Source Files folder and
.h files will be under the Header Files folder - Upon opening an existing project, you can use the
solution explorer to open files associated with a
class youve created for the project
8Solution Explorer
- To add a file to your solution
- Select File from the menu at the top
- Select Add New Item to create a new file and
add it to your project - Select the type of File you wish to add (.cpp or
.h) - Name the file and click Open to add the file
- Select Add Existing Item to add a file you
have already created - It is best to copy the file you wish to add to
your solutions folder - Find the file you wish to add and click Open
- The files you add to your project will appear in
the appropriate section in the solution explorer - The files that you add will still need to be
included as they did in CS53 include
myNewFile.h remember that you only need to
include .h files and not .cpp files
9Properties Pane
- To enable the Properties pane, press F4 or select
it from the view menu - The properties pane shows the properties for the
currently selected control. The property name
appears on the left and the value appears on the
right - Notable Properties
- Caption changes the text displayed on static
text, buttons, and the dialog box title bar - ID changes the ID of a button so that event
handler function names are more meaningful
10Toolbox
- To enable the Toolbox, press CtrlAltX, or
select it from the view menu at the top - The Toolbox contains all of the controls that we
can add to our dialog box the controls we will
use in this class are the button, static text,
edit control, and the listbox - To add a control to the dialog, click, hold, and
drag the desired control to the proper location,
or select the desired control and drag a box in
the dialog box where you want the control to
appear
11Static Text
- The static text control is a control that simply
displays text on your Dialog - Static text can simply be used to label other
controls, so the user understands their purpose - To Create Static text, drag the Static Text
tool onto your dialog resource screen. Then,
change the caption property to the text you want
displayed.
12The Button Control
- The Button control is a simple clickable button.
Its most common use is to perform a function when
the user clicks the button. - To add a button to your dialog, find the button
control in the toolbox and drag it to the dialog - To change the ID of your button, select the
button and find the ID property in the properties
pane - It is best to leave the IDC_Button portion of the
ID and simply add the name that you want to the
end, so for an OK button we name it IDC_Button_OK - To change the Text that appears on a button,
change the Caption property
13Button Control (cont)
- To create a function that executes when the
button is clicked, just double click the button
in the dialog box and Visual Studio will create
and display the OnBnClicked function - Any code added to this function will execute when
the button is clicked - The above code will pop up a message box that
says Button OK Clicked when the OK button is
clicked - To make a button whose OnBnClicked function is
executed whenever the Enter key is pressed,
change the Default Button property to True
14Edit Control
- The Edit control is a simple text box that can be
manipulated by either the program or the user - In order to retrieve data entered into the edit
control, associate a variable with it and use the
UpdateData(TRUE) function - In order to change the data displayed in an edit
control, associate a variable with it and use the
UpdateData(FALSE) function - Add a CString variable to an edit control that
will accept letters and number - To create an edit control that the can only be
changed from within the program (inaccessible by
the user) change the Read Only property of the
edit control to True
15Edit Control (cont)
- For an edit control that will only accept an
integer - Change the Number property of the edit control to
True to prevent the user from entering anything
other than 0-9. - Add an int variable to the edit control with the
category of value. To limit the numbers a user
can enter into an edit control, set a Min and Max
value. - The above will display an error popup when
UpdateData(TRUE) is called, and UpdateData(TRUE)
will return FALSE. - To limit the amount of text that an edit box will
accept, change the Auto HScroll property to
false, so that the edit control will not scroll
to accept more input
16Listbox Control
- The Clistbox control is a control that displays
text line by line, allows users to select lines,
and will allow the user to scroll through lines
when the listbox contains too many lines to be
displayed at once. - To add a Listbox, simply click List Box in the
toolbox on the left and drag to the desired
loaction on the dialog - Certain properties of the Clistbox are
automatically set such as the sort property. So
if you want the information to display in the
order it was added to the listbox, you should
change the sort property to false. - To manipulate the data in a list box, see
Controls with variables and Using the CListbox
Object
17Event Handlers
- Event Handlers allow you to create a function
that is called when certain actions are performed
on controls such as mouse clicks - In Visual Studio, there is no main loop that is
executed from top to bottom. Instead, Visual
Studio uses event handlers to call functions that
perform the necessary operations a program will
execute until a button is clicked that tells the
program to exit - To add an event handler, right click a control
and select Add Event Handler
18Event Handlers (cont.)
- Select the event you want to handle for a mouse
click, select BN_CLICKED - For this class, it is unnecessary to change the
other options - Click Add and Edit
- Visual Studio will create necessary Function
declarations and take you to the Function
definition the name of the function will
reflect the controls ID property - Any code in the function will be executed when
the event (ie mouse click) occurs
19Controls with Variables
- In order to get information from controls or
display information using controls, we associate
variables with them - To add a variable to a control, right-click the
control and select Add Variable - Selecting Value creates a variable whose value
is link to the control - Selecting Control creates a object whose
functions give access to the control from the
code - For an edit control, change the category to
Value and the Variable type to CString - For a listbox, the category should be Control
and variable type CListBox - Name the variable and click finish to associate
it with the control
20UpdateData(FALSE)
- To update a control so that it reflects the value
in its associated variable, we call the function
UpdateData(FALSE). Ex - We have an edit control with the CString myEdit
associated with it. The pictures show the state
of the control after the lines of code to their
left have executed. - myEditfoo
-
- UpdateData(FALSE)
- myEditbar
- UpdateData(FALSE)
- myEdit
- UpdateData(FALSE)
-
21UpdateData(TRUE)
- To update the variable associated with a control
so that the variable reflects the data in the
control, we call UpdateData(TRUE) For instance
we have an edit box with the CString variable
myEdit associated with it - Control Code myEdit
- myEditfoo
- UpdateData(TRUE)
- UpdateData(TRUE)
- myEditbar
- UpdateData(TRUE)
22Using CStrings
- CStrings are similar to the String object in
CS53, but CStrings are specific to Visual C.
In many cases it is more convenient to use a
CString than a character array. - CStrings do not need to be initialized with a
null terminator like a character array does - CStrings do not need to be assigned a length and
have no maximum length other than physical
limitations of the machine - To convert from a CString to a character array,
you can use strcpy() or strncpy() - CString c1foo char a10
- strncpy(a,c,10) //foo
- The CString and operator can used with other
CStrings, a double quote enclosed string, or a
null terminated string - CString c1,c2 char a20
- strcpy(a, 456)
- c1a //456 Note there is no special method
needed to convert character array to CString - c2123 //123
- c2c1 //123456
23Using CStrings (cont)
- You can access a specific character in a CString
using brackets the same way you would with a
character array - c2153 //153
- c212 //123
- To avoid errors, do not add different string
types together to create a CString - //This does not work because the operation
(123a) is performed first, this is not legal - c2123ac1
- //Instead, do the following
- c2123
- c2a
- c2c1
- To find the length of a CString, we can use the
GetLength() function. Ex c1.GetLength() This
makes it easy to iterate through all of the
characters of a CString - for(int i0 iltc1.GetLength() i)
- //perform operation using each character in the
string c1i
24Using the CListBox Object
- In order to manipulate the data stored by a
listbox, you must use a CListBox object
associated with it - To clear the data stored in a listbox, we call
the Reset() function, so if we have a CListBox
variable named myListBox, we use the code
myListBox.Reset() to clear its data - To add a line to display in a listbox, we call
the function AddString(String) where String is a
String we want to add to the listbox. If the
listbox has the sort property set to false, the
new string will appear as the last line in the
listbox. Example myListBox.AddString(Add this
string) - To delete a string from a listbox, we call the
DeleteString() function. The only parameter
DeleteString takes is the index of the string to
delete the indexes start at 0 for the top
element of the listbox. - myListBox.Reset()
- myListBox.AddString(a)
- myListBox.AddString(b)
- myListBox.DeleteString(0)
25Using the CListBox Object
- In some cases, we want to update a listbox to
reflect a change in a Bag or other data
structure. To do this, it is best to reset the
entire content of the listbox and then iterate
through all contents of the data structure,
adding them to the listbox one by one. This
ensures that the data in the listbox will always
represent exactly the data in the data structure. - When using the CListBox, the listbox displays the
results of any function calls automatically it
is unnecessary to call UpdateData(FALSE) to
refresh the listbox display
26Working with the CDialog Class
- When you create an MFC Application, .NET creates
a program that just runs a CDialog class, so to
change the functionality of our programs, we just
edit this class to make it do what we want.
Programs no longer execute from the top of a main
loop to the bottom instead, certain functions
are called when events like mouse clicks occur - The name of the class that windows creates for us
is CProgramNameDlg where ProgramName is the name
you specified when creating the project - The files for the class are appropriately named
ProgramNameDlg.h and ProgramName.cpp, and we can
open these files to edit them using the Solution
Explorer - Below is code from the header file of the dialog
class created for my program example3
27CDialog Class (Cont.)
- The CDialog class contains private, protected,
and public variables and functions just like any
other class. Some functions and variables are
created automatically when the program is
created. Others are created when you add event
handlers and controls. - The code on the following page is for the dialog
shown above. This dialog contains a button with
the ID IDC_BUTTON_MYBUTTON with and OnBnClicked
event handler added and an edit control with a
CString named myEdit associated with it - To compile and run your program, go to
Debug-gtStart or press F5. If the program
compiles, he dialog screen should appear and you
should be able to use the controls.
28CDialog Class (Cont.)
The CDialog class has Public and Protected member
variables and functions
These portions of code were created by Microsoft
upon project creation
This portion of code contains the code add by
Microsoft for the variables and event handlers
added. Note that there is a public member
function named OnBnClickedMyButton which is the
function that will be called when the button with
the ID IDC_BUTTON_MYBUTTON is clicked. It also
contains a CString public member variable named
myEdit which is associated with the edit control
29CDialog Class (Cont.)
- You can add member variables and functions to the
CDialog class just like any other class. The
advantage of this is that member variables are
accessible from all member functions which
includes OnBnClicked functions. Also, a member
function will have access to the member variables
such as the variables associated with controls
and functions inherited by the Dialog class such
as the MessageBox function. - The following shows a portion of the header with
an array of integers and a function named
parseEditControl added to the class. The class
also has a myBag object named m_bag that may
allow us to keep track of a collection things
such as multiple text inputs from the user.
30MessageBox function
- The MessageBox function is a function that
creates a message box popup - The first parameter of the function is a string
that is the text that will be displayed in the
main part of the message box - The second parameter is a string for the caption
which is the text that appears in the Title bar
of the message box - The final parameter accepts predefined integer
constants which determine the buttons and
functionality of the message box for a message
box with an ok button we use MB_OK - The MessageBox function is inherited by the
Dialog CDialog class, so it is unavailable in any
classes you create - MessageBox(This text appears in the body of the
window, This text appears in the Title Bar,
MB_OK)
31Using The MSDN in .Net
- To use the MSDN in .Net, select Help-gt Search,
and you will get a search screen on the right
side. - You may want to filter by Visual C to get more
relevant results - Results for your inquiry will appear at the
bottom. - Click on a result to view it in the main pane
- The MSDN is a good way to get an answer about how
a particular object or function works
32Common Problems
- Insufficient AFS drive space - If you are
creating your program on your S drive, you are
limited to 100MB of space. When you Run your
program it creates a Debug folder which can take
up as much as 10MB of space. It is a good idea
to go to Build-gtClean Solution when you are
finished with a program to clear this Debug
folder and recover the space - The following error is a typical sign of low Disk
Space - In some cases, you may see an error message that
doesnt seem to make sense. It is possible that
the error could be cleared up by selecting Build
-gtRebuild Solution. - Another way to avoid errors is to disable
Precompiled headers. To do this, go to Project
on the top menu, Properties, navigate to
Configuration-gtC/C-gtPrecompiled Headers on the
right, and then select Not using precompiled
Headers for Create/Use Precompiled Headers.
This technique is useful when your error message
says something about precompiled headers