OOP Reviewer 2
OOP Reviewer 2
Namespace declaration
A class
Class methods
Class attributes
A Main method
Comments
Hello World in C#
Hello World in C#
The first line of the program using System; - the using keyword is used to include the System namespace
in the program. A program generally has multiple using statements.
The next line has the namespace declaration. A namespace is a collection of classes. The
HelloWorldApplication namespace contains the class HelloWorld.
The next line has a class declaration, the class HelloWorld contains the data and method definitions that
your program uses. Classes generally contain multiple methods. Methods define the behavior of the
class. However, the HelloWorld class has only one method Main.
The next line defines the Main method, which is the entry point for all C# programs. The Main method
states what the class does when executed.
Hello World in C#
The next line /*...*/ is ignored by the compiler and it is put to add comments in the program.
The Main method specifies its behavior with the statement Console. WriteLine("Hello World");
WriteLine is a method of the Console class defined in the System namespace. This statement causes the
message "Hello, World!" to be displayed on the screen.
The last line Console.ReadKey(); is for the VS.NET Users. This makes the program wait for a key press and
it prevents the screen from running and closing quickly when the program is launched from Visual
Studio .NET.
Note:
C# is case sensitive.
Unlike Java, program file name could be different from the class name.
Click the Run button or press F5 key to execute the project. A Command Prompt window appears that
contains the line Hello World.
Compile a C# program by using the command-line instead of the Visual Studio IDE:
Open the command prompt tool and go to the directory where you saved the file.
If there are no errors in your code, the command prompt takes you to the next line and generates
helloworld.exe executable file.
You can see the output Hello World printed on the screen.
C# - Basic Syntax
In Object-Oriented Programming (OOP) methodology, a program consists of various objects that interact
with each other by means of actions. The actions that an object may take are called methods.
Objects of the same kind are said to have the same type or, are said to be in the same class.
SAMPLE PROGRAM
C# - Basic Syntax
Objects of the same kind are said to have the same type or, are said to be in the same class.
C# - Basic Syntax
The using keyword is used for including the namespaces in the program can include multiple using
statements.
Comments in C#
Comments are used for explaining code. Compilers ignore the comment entries. The multiline
comments C# program start with /* and terminates with the characters */. Single-line comments are
indicated by the ‘//’ symbol.
C# - Basic Syntax
Member Variables
Variables are attributes or data members of a class, used for storing data. In the preceding program, the
Rectangle class has two member variables named length and width.
Member Functions
Functions are set of statements that perform a specific task. The member functions of a class are
declared within the class. Our sample class Rectangle contain three member functions: AcceptDetails,
GetArea and Display.
Instantiating a Class
In the preceding program, the class ExecuteRectangle contains the Main() method and instantiates the
Rectangle class.
C# - Basic Syntax
Identifiers
An identifier is a name used to identify a class, variable, functions, or any other user-defined item.
A name must begin with a letter could be followed by a sequence of letters, digits (0-9) or underscore.
The first character in an identifier cannot be a digit.
It must not contain embedded space or symbols such as ? - + ! @ # % ^ & * () [] {} . ; : “ ‘ / and \.
However, an underscore (_) can be used.
Keywords are reserved words predefined to the C# compiler. These keywords cannot be used as
identifiers. However, if you want to use these keywords as identifiers, you may prefix the keyword with
the @ character.
THANK YOU!
◼ Forms Programming I
◼ Contents
◼ System.Drawing Namespace
◼ System.Windows.Forms Namespace
◼ Forms Programming
System.Drawing
System.Windows.Forms
◼ Higher-level controls
◼ System.Drawing
◼ This namespace provides many graphic data structures which are used throughout the GUI
programming model
◼ These can be used to draw anything, not just what is offered by the pre-built controls
◼ System.Drawing.Point
◼ Constructor
Point(int x, int y)
◼ Properties
X – get/set of X coordinate
Y – get/set of Y coordinate
◼ System.Drawing.Size
◼ Constructor
◼ Properties
◼ System.Drawing.Rectangle
◼ Structure representing a rectangle as the point of the top-left-corner, width and height
◼ Constructor
◼ Properties
System.Drawing.Color
◼ Structure representing an alpha-RGB color
◼ Methods
◼ Properties
Black, White, Red, Green, Yellow, Cyan, Coral, Blue, etc. – get values of pre-defined
colors
System.Drawing.Font
◼ Constructor
◼ Properties
◼ System.Drawing.FontStyle
◼ The values can be ORed together to indicate that more than one style should apply at once
◼ System.Windows.Forms
◼ This namespace contains all of the controls used on the average Windows interface
◼ Form Class
◼ Although the class has numerous methods, most of the time you interact with it via properties
and delegates
◼ Form Properties
◼ Form Events
◼ Form Events
◼ Form Methods
◼ We will demonstrate how to build a simple GUI interface using a text editor
◼ Doing it by hand
Label greetingLabel;
Button cancelButton;
greetingLabel.ForeColor = Color.Black;
cancelButton.Text = "&Cancel";
this.Controls.Add(cancelButton);
this.Controls.Add(greetingLabel);
Application.Exit();
◼ * see HelloForm
◼ You can hook event handlers onto the events and write the code for them
◼ CheckBoxes
◼ * see ListBoxDemo
◼ GroupBox
◼ * see TextBoxDemo
◼ Panels
◼ A panel is like a group box but does not have a text label
◼ BorderStyle.Fixed3D
◼ BorderStyle.FixedSingle
◼ BorderStyle.None
◼ Radio Buttons
◼ A group is formed when the radio buttons are in the same container – usually a group box or
panel
◼ Radio Buttons
* see TextBoxDemo
◼ TextBox
AcceptsReturn – in a multiline box, if true then pressing Return will create a new line. If
false then the button referenced by the AcceptButton property of the form, will be
clicked.
PasswordChar – if this is set to a char, then the box becomes a password box
◼ TextBox
ReadOnly – if true, the control is grayed out and will not accept user input
◼ The file dialog allows you to navigate through directories and load or save files
OpenFileDialog
SaveFileDialog
◼ You should create the dialog once and reuse it so that it will remember the last directory the
user had navigated to
◼ File Dialog
◼ Windows Bitmap|*.bmp|JPEG|*.jpg|GIF|*.gif
◼ File Dialog
◼ ShowDialog – a method to show the dialog and block until cancel or OK is clicked
if (openDialog.ShowDialog() == DialogResult.OK) {
pictureBox1.Image = img;
◼ * see ImageViewer
◼ Image Class
◼ Several concrete classes are used for image types such as BMP, GIF, or JPG
◼ *see ImageViewer
◼ PictureBox Class
SizeMode – determines what to do if the image does not fit into the window
◼ Normal
◼ StretchImage
◼ AutoSize
◼ CenterImage
◼ Zoom
◼ * see ImageViewer
◼ ToolTips
◼ These are the small pop-up boxes which explain the purpose of a control
◼ To use
◼ * see ImageViewer
◼ ToolTips
◼ ToolTips
◼ After the tooltip appears in the tray, a new tooltip property appears for every component
◼ That text will be displayed when the mouse hovers over that component
◼ NumericUpDown
◼ * see DateSelector
◼ MonthCalendar
◼ * see DateSelector
◼ DateTimePicker
More configurable
◼ Properties/methods
◼ * see DateSelector
◼ System.DateTime Structure
◼ Constructors
◼ Properties
◼ DateTime
◼ DateTime
◼ Methods
◼ DateTime
TimeSpan Subtract(DateTime)
int CompareTo(DateTime)
ToLongDateString()
ToShortDateString()
ToLongTimeString()
ToShortTimeString()
ListBox
ListBox
◼ None – no selection
◼ ListBox
◼ * see ListBoxDemo
◼ Populating a ListBox
listBox1.Items.Add(
◼ ComboBox
◼ A combo box is like a list but lets you displays a selected value.
◼ Options allow the selected text to be editable or to require it to be selected from the drop-down
list
◼ ComboBox
◼ DropDownStyle –
DropDown – default indicating text is editable & user must click to see list
DropDownList – value is not editable & user must click to see list
◼ ComboBox