creating windows applications with csharp -part 1
TRANSCRIPT
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
1/18
Microsoft Virtual LabsCreating Windows Applicationswith C# -Part 1
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
2/18
2 Creating Windows Appli cations with C# -Part 1
Table of Contents
Creating Windows Applications with C# -Part 1......................................................................................3
Exercise 1 Creating Controls on a Windows Form .............................................................................................4Exercise 2 Creating and Using Menus ..............................................................................................................14
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
3/18
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
4/18
4 Creating Windows Appli cations with C# -Part 1
Exercise 1Creating Controls on a Windows Form
ScenarioIn this exercise, you will use various properties of the MonthCalendar control to change itsbehavior. You will also handle the DateSelected event of the MonthCalendar control to make the
selected dates bold. Then, you will display the print preview of an empty document using the
PrintPreviewDialog control. You will use the FolderBrowserDialog control to display the folder
browser dialog box and the selected path. Then, you will create a system tray icon using the
NotifyIcon control.
Tasks Detailed steps
1. Setup Calendar Control. a. Create a new Windows Application project named Lab2-CS using
Microsoft Visual Studio .NET: Navigate to Start | All Programs | Microsoft Visual Studio
.NET 2003 | Microsoft Visual Studio .NET 2003.
Select File | New | Project menu command or pressCTRL+SHIFT+N.
ClickVisual C# Projects in Project Types list. ClickWindows Application in Templates list. Type Lab2-CS in the Name field and clickOK.
Visual Studio .NETcreates a default form namedForm1.cs. It also creates
Lab2-CS.csproj,Lab2-CS.sln, andAssemblyInfo.cs files.
b. Rename Form1.cs to CreatingControls.cs:
Select the View | Solution Explorer menu command or pressCTRL+ALT+L to open Solution Explorer.
In the Solution Explorer, right-clickForm1.cs and selectRename.
Rename the form CreatingControls.cs. and press ENTER.c. Double-click the CreatingControls.cs file in Solution Explorer and
select the View | Properties Window menu command or press F4 to
view the properties window of control.
d. Set the following properties:
Name - CreatingControlsForm Size (Expand the Size node) Width - 584 Height - 460 StartPosition - CenterScreen Text - Creating Controls Example
e. Change the code in the Main method:
Select the View | Code menu command or press F7 to switch tocode view ofCreatingControlsForm form.
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
5/18
Creating Windows Applic ations with C# -Part 1 5
Locate the Main method in the form. In this method, only one line of code exists:static void Main()
{
Application.Run(new Form1());
}
Modify the code to look as follows:static void Main()
{
Application.Run(new CreatingControlsForm());
}
f. Add a TabControl control to the form:
Select the View | Designer menu command or press SHIFT+F7to switch back to design view.
Select the View | Toolbox menu command or pressCTRL+ALT+X to view Toolbox.
Double-click the TabControl control on the Windows Forms tabin the Toolbox.
g. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the
TabControl control.
h. Set the following properties:
Name - tabControl Location (Expand the Location node) X - 24 Y - 16 Size (Expand the Size node) Width - 528 Height - 312
i. Add the Calendar Control tab page to the tabControl control:
Locate the TabPages property at the bottom of the Propertieswindow.
Click the ellipsis().A TabPage Collection Editor dialog box appears.
ClickAdd.A new tab pagetabpage1 appears in theMembers listbox.
Set Text property oftabpage1 to Calendar Control. Set Name property oftabpage1 to tabCalendarControl. ClickOK.
j. Select the View | Toolbox menu command or press CTRL+ALT+X to
view Toolbox.
k. Drag a Label control from the Toolbox and place it on the
tabCalendarControl tab page in the form designer.
l. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Label
control.
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
6/18
6 Creating Windows Appli cations with C# -Part 1
m. Set the following properties:
Name - calendarControlHeaderLabel Font (Expand the Font node) Name - Microsoft Sans Serif Bold - True Size - 8.25 Unit - Point Size (Expand the Size node) Width - 528 Height - 312 Text - To select multiple dates, click one date and
drag the mouse to another date. The maximum number of
dates that can be selected depends on the control settings. The
selected dates have to be continuous
n. Select the label control and align it with the borders of the calendar
control.o. Select the View | Toolbox menu command or press CTRL+ALT+X to
view the Toolbox.
p. Drag a Button control from the Toolbox and drop it on the form.
q. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
r. Set the following properties:
Name - closeButton Text - &Close
s. To write the code for the Click event ofClose button:
Double clickClose button in the form designer. It takes you tocloseButton_Click method in the code view of the form.
Write the following line of code in the closeButton_Clickmethod.
private void closeButton_Click(object sender,
System.EventArgs e)
{
this.Close();
}
Note: Visual Studio .NETautomatically adds event handler function body
to respond to events.
t. Select the View | Designer menu command to switch to the formdesigner.
u. Drag the MonthCalendar control from the Toolbox and drop it on the
tabCalendarControl tab page in the form designer.
v. Click the control and select the View | Properties Window menu
command or press F4 to view the properties of the MonthCalendar
control.
w. Set the following properties:
Name - labMonthCalendar
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
7/18
Creating Windows Applic ations with C# -Part 1 7
FirstDayOfWeek - Monday ShowTodayCircle - False ShowWeekNumbers - True CalendarDimensions (Expand the CalendarDimensions node) Width - 2 Height - 1 MaxSelectionCount - 5 TitleBackColor - GrayText TitleForeColor - ControlText TrailingForeColor - ActiveCaption
x. Drag a Button control from the Toolbox and drop it on the
tabCalendarControl in the form designer.
y. Click the control and select the View | Properties Window menu
command or press F4 to view the properties window of the Button
control.
z. Set the following properties:
Name - displaySelectedDateButton Text - Display Selected Dates
The completed form will appear.
aa. Double-click the displaySelectedDateButton button in the designer.
Note: To save on typing a snippets file is located in C:\ Microsoft Hands-
On-Lab\HOL-02.
bb. To display the selected dates, add the following code to
displaySelectedDateButton_Click method or paste snippet1 in the
area specified below:
// Displays message box showing selected
dates.
Private void
displaySelectedDateButton_Click(object
sender, System.EventArgs e)
{
SelectionRange selectedRange =
labMonthCalendar.SelectionRange;
DateTime startDate =
selectedRange.Start.Date;DateTime endDate =
selectedRange.End.Date;
if(startDate != endDate){
MessageBox.Show(string.Format("{0} {1}{2} {3}", "Selected Date Range: From",
startDate.ToShortDateString(), "to",endDate.ToShortDateString()), "Lab2 -
Creating Windows Applications");}
else{
-
8/6/2019 Creating Windows Applications With Csharp -Part 1
8/18
8 Creating Windows Appli cations with C# -Part 1
MessageBox.Show("Selected Date: "+ startDate.ToShortDateString(), "Lab2 -Creating Windows Applications");
}
}
cc. To make the selected dates bold:
Select the View | Designer menu command to switch to the formdesigner. Click icon in the Properties window of the
labMonthCalendar control.
Locate DateSelected event and double-click the same. This takesyou to labMonthCalendar_DateSelected method in the code
view.
Add the following code to the DateSelected event of the monthcalendar control or paste snippet2 in the area specified below:
// Makes the selected dates bold
private void
labMonthCalendar_DateSelected(object sender,
System.Windows.Forms.DateRangeEventArgs e){
DateTime startDate =
labMonthCalendar.SelectionStart.Date;DateTime endDate =
labMonthCalendar.SelectionEnd.Date;
TimeSpan timeSpan =
endDate.Subtract(startDate);
DateTime [] selectedDates = newDateTime [timeSpan.Days + 1];
for (int index = 0; index