csci 3328 object oriented programming in c# chapter 2: introduction to visual c# programming 1 xiang...

23
CSCI 3328 Object CSCI 3328 Object Oriented Programming in Oriented Programming in C# C# Chapter 2: Introduction Chapter 2: Introduction to Visual C# Programming to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg, TX 78539 [email protected]

Upload: william-marsh

Post on 29-Dec-2015

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

CSCI 3328 Object Oriented CSCI 3328 Object Oriented Programming in C# Programming in C#

Chapter 2: Introduction to Visual C# Chapter 2: Introduction to Visual C# ProgrammingProgramming

1

Xiang Lian

The University of Texas Rio Grande Valley

Edinburg, TX 78539

[email protected]

Page 2: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Objectives

• In this chapter, you will– Become familiar with the IDE, Visual Studio – Learn more controls of Visual C# programming– Discover differences between different controls– See more examples of designing the graphical

interface– Become aware of the console application, in

addition to windows forms application

2

Page 3: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Demo in the Classform

label

button

Text

BackColor

3

Page 4: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Integrated Development Environment (IDE)

4

Page 5: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Integrated Development Environment (cont'd)

• Menu bar

– File: Open/New/Save/Close projects or files– Edit: Undo/Redo, Copy, Paste, Cut– View: Other Windows Properties– Build– Debug: Start Debugging, Step Into, Step Over

5

Page 6: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Integrated Development Environment (cont'd)

• Buttons– For files and edit

– For compilation

– For debugging

• If some buttons do not appear, right click your mouse on the tool bar and customize buttons

6

Page 7: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Integrated Development Environment (cont'd)

• Windows

Properties Solution Explorer

events

show all files

7

Page 8: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Visual C# Controls

• Choose Windows forms application• What you see is the IDE (Integrated Development

Environment)• Use Sizing handle to make the form• Adjust the size of the properties window• In the toolbox

– Use common controls

• Start placing objects on the form• Place a button, label it OK and add code this.Close()

8

Page 9: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Controls

9

Page 10: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

TextBox

• Difference between label and text boxes• Properties window

– Changing name and text– Border Style– BackColor– Font Size– Visible– TabIndex

10

Page 11: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

PictureBox

• Properties– Image Local resource Import– SizeMode: Normal, Stretch Image, AutoSize,

CenterImage, Zoom

11

Page 12: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

GroupBox

• Properties– Text

• RadioButton– Checked = "false"– Add radio buttons to the group box– At most one radio button is checked at a time

• CheckBox– 0 or any number of boxes are checked

12

Page 13: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Other Interesting Controls

• ProgressBar– Value: 50

• ListBox– Items: enter strings one per line

• ComboBox

13

Page 14: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Adding a Pop-Up Message Box

Add a form to the application

Add this lineMessageBox.Show("Hello!");

MessageBox.Show("Hello!", "Show Message");

14

Page 15: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Example of GUI Design

• Notepad GUI– Menu strip– Rich TextBox

15

Page 16: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Example of GUI Design (cont'd)

• Calendar GUI– Label– MonthCalendar– RichTextBox

16

Page 17: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Analyzing a Program

• Line numbers: not part of Visual C#

• Comments: (Lines 23-29)

• Classes: class declaration (Lines 12-31)• Keywords: Case sensitive (the same as C++)

• public, private, class

• partial

17

event handling

Page 18: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Variable Declaration and Naming

• Variable declaration– Same as C++

• Data types: int, float, double, char, etc.

• Variable name: (1) digits, letters, underscore ("_"), and (2) cannot start with a digit

• int x = 1;

• int x = 0, y = 1;

• Naming convention– Camel case: e.g. firstNumber

18

Page 19: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Assignment

• Use DOT (.) to refer to attributes of an object– E.g., Label1.Text

• Use assignment operator (=) to give a value to a variable– E.g., Label1.Text = "haha"

19

Page 20: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Applications

• In addition to Windows Forms Application, …

• Console– Output appears in console window– From Visual Studio choose C# and choose a new

project, and choose Console Application– Give it a name

20

Page 21: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Demo of Hello Program

string name;

name=Console.ReadLine();

Console.WriteLine("Hello! " + name);

Console.WriteLine("Welcome to Dr. Lian's C# Class!");

Console.ReadLine();

21

Page 22: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Demo of Hello Program (cont'd)

22

Page 23: CSCI 3328 Object Oriented Programming in C# Chapter 2: Introduction to Visual C# Programming 1 Xiang Lian The University of Texas Rio Grande Valley Edinburg,

Summary

• IDE– Menu bar, buttons, windows, toolboxes

• Controls– TextBox, PictureBox, GroupBox, etc.

• GUI design– Examples

• Program analysis• Windows Forms Application vs. Console

Application23