Professional Documents
Culture Documents
Chapter 5 - Windows Programming in C#
Chapter 5 - Windows Programming in C#
C# by Dagne w 2
Common Controls
C# by Dagne w 4
Common Controls
Data
Chart –it used to display data in the form of chart.
DataGridView – is used to show data in form of rows
and columns.
Dataset-it represents memory data.
Dialogs
ColorDialogs- it used to choose color.
FolderBrowserDialog – it used to browse folder.
OpenfileDialogs-it used to open file.
SaveFileDialogs –it used to save file
fontDialog –it used to select font .
C# by Dagne w 5
Common Controls
Printing
PagesetupDialog- to arrange page format, example
print setting, orientation.
PrintDialog:it used to choose print option. Example
number of copy
printDocument-it used to print documents.
Form methods
Close –it used to close form.
Show-it used to show form
Hide –it used to hide form
Activate it used to activate a form
Focus-it used to focus form
C# by Dagne w 6
Common GUI events
Mouse move
Mouse click
Mouse double-click
Key press
Button click
Change in focus
Window activation
C# by Dagne w 7
13.2 Windows Forms
• Windows Forms
• Used to create GUIs for programs
• Graphical element that appears on your computer’s desktop
• Active window is the front most window
• A Form is a container for controls and components
• In visual programming, Visual Studio generates much of the GUI-related code
C# by Dagne w 8
Form Properties
Property Description
AutoScaleDimensions DPI resolution of display it was built for. Will be scaled to look correct on other
displays.
C# by Dagne w 9
Form Events
Event Description
1
0
C# by Dagne w 10
Form Methods
Method Description
By dagne
1
1
11
CheckBoxes
C# by Dagne w 12
GroupBox
C# by Dagne w 13
Panels
• A panel is like a group box but does not have a text label
• It contains a group of controls just like group box
• BorderStyle – get/set border style as
• BorderStyle.Fixed3D
• BorderStyle.FixedSingle
• BorderStyle.None
C# by Dagne w 14
Radio Buttons
10
C# by Dagne w 15
Radio Buttons
• Checked – get/set Boolean indicating if the button is checked
• CheckedChanged – delegate invoked when the button is
checked or unchecked
C# by Dagne w 11
TextBox
• This is a single line or multi-line text editor
• Multiline – get/set Boolean to make multiline
• 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
C# by Dagne w 12
TextBox
• ReadOnly – if true, the control is grayed out and will not
accept user input
• ScrollBars – determines which scrollbars will be used:
ScrollBars.None, Vertical, Horizontal, Both
• TextAlign – get/set HorizontalAlignment.Left, Center, or
Right
• TextChanged – event raised when the text is changed
C# by Dagne w 13
File Dialog
• The file dialog allows you to navigate through directories and load or
save files
• This is an abstract class and you use
• OpenFileDialog
• SaveFileDialog
• You should create the dialog once and reuse it so that it will
remember the last directory the user had navigated to
C# by Dagne w 14
File Dialog
• InitialDirectory – string representing the directory to start
in
• Filter – a string indicating the different types of files to be
displayed
• A set of pairs of display name and pattern separated by vertical bars
• Windows Bitmap|*.bmp|JPEG|*.jpg|GIF|*.gif
• FilterIndex – the filter to use as an origin 1 index
C# by Dagne w 15
File Dialog
• FileName – the name of the file selected
• ShowDialog – a method to show the dialog and block until cancel
or OK is clicked
C# by Dagne w 16
Image Class
• An abstract class that can store an image
• Several concrete classes are used for image types such as
BMP, GIF, or JPG
• FromFile(string fname) – loads any supported
image format from a file
• FromStream(stream) – loads an image from a stream
• Height – image height
• Width – image width
C# by Dagne w 17
PictureBox Class
• This displays an image
• Image – assigned an Image object to display
• SizeMode – determines what to do if the image does not fit into
the window
• Normal
• StretchImage
• AutoSize
• CenterImage
• Zoom
C# by Dagne w 18
ToolTips
• These are the small pop-up boxes which explain the purpose of
a control
• To use
• Create a new tooltip in the designer
• Drop the tooltip onto the form
• The tooltip will appear on a tray below the form
C# by Dagne w 19
ToolTips
20
C# by Dagne w 25
ToolTips
• After the tooltip appears in the tray, a new tooltip property
appears for every component
• This can be assigned different text for each component
• That text will be displayed when the mouse hovers over that
component
C# by Dagne w 21
NumericUpDown
C# by Dagne w 22
MonthCalendar
C# by Dagne w 23
DateTimePicker
• Similar to a month calendar but
• Calendar pulls down and selection displayed
• More configurable
• Selects a single value, not a range
• Properties/methods
• Format – Long, Short, Time, Custom
• Value – DateTime value selected
• ValueChanged – event which fires when date or time changes
C# by Dagne w 24
System.DateTime Structure
• A structure representing a date and time
• Constructors
• DateTime(int d, int m, int y)
• DateTime(int d, int m, int y, int h, int m, int s)
• Properties
• Now – returns a DateTime object set to the current local time
C# by Dagne w 25
DateTime
• Day – day from 1-31
• Month – month from 1-12
• Year – tear from 1-9999
• Hour – from 0-23
• Minute – minute from 0 -59
• Second – second from 0 -59
• Millisecond – millisecond from 0-999
C# by Dagne w 26
DateTime
• DayOfWeek – get enumeration of Sunday, Monday,…
• DayOfYear – day of year from 1 – 366
• Methods
• DateTime AddYears(double value)
• DateTime AddMonths(double value)
• DateTime AddDays(double value)
• DateTime AddHours(double value)
• DateTime AddSeconds(double value)
• DateTime AddMilliseconds(double value)
C# by Dagne w 27
DateTime
• TimeSpan Subtract(DateTime)
• int CompareTo(DateTime)
• static DateTime Parse(string)
• ToLongDateString()
• ToShortDateString()
• ToLongTimeString()
• ToShortTimeString()
C# by Dagne w 28
ListBox
• The ListBox presents a list of items which can be
selected
• A scrollbar is displayed if needed
• MultiColumn – displays list as multiple columns
• SelectedIndex – index of selected item
• SelectedIndices – collection of selected indices
• SelectedItem – the selected item
C# by Dagne w 29
ListBox
• SelectedItems – collection of selected items
• SelectionMode – how items can be selected
• None – no selection
• One – single selection
• MultiSimple – each click selects additional item
• MultiExtended – uses shift and control keys
• Sorted – if true the items will be sorted alphabetically
30
C# by Dagne w 35
ListBox
• Items – a collection of items in the list box
• ClearSelected – method to clear selection
• GetSelected – returns true if the parameter passed is selected
• SelectedIndexChanged – event when selection changes
C# by Dagne w 31
Populating a ListBox
• Any object can be placed into a ListBox
• The display is generated by ToString()
C# by Dagne w 32
ComboBox
• A combo box is like a list but lets you displays a selected value.
• The list pulls down when a selection is being made.
• Options allow the selected text to be editable or to require it to
be selected from the drop-down list
C# by Dagne w 33
ComboBox
• DropDownStyle –
• Simple – text is editable & list always visible
• DropDown – default indicating text is editable & user must click to see list
• DropDownList – value is not editable & user must click to see list
• Items – the collection of items in the list
C# by Dagne w 34
ComboBox
• MaxDropDownItems – max number of items in pulldown before
scrollbar used
• SelectedIndex – index of selection
• SelectedItem – selected item
• Sorted – whether entries are sorted
• SelectedIndexChanged – event raised when selection
changes
C# by Dagne w 35
Code-behind
• Events are handled by methods that live behind GUI
• our job is to program these methods…
C# by Dagne w 36
Questions??