Presentation is loading. Please wait.

Presentation is loading. Please wait.

Controls, Events, and More

Similar presentations


Presentation on theme: "Controls, Events, and More"— Presentation transcript:

1 Controls, Events, and More
More Windows Material Controls, Events, and more November 7, 2018

2 AssemblyInfo.cs Separating version, title, product, and author information from the executable code Controls, Events, and more November 7, 2018

3 AssemblyInfo.cs Each C# project has a file named AssemblyInfo.cs in a project folder named Properties This file contains attributes in square brackets [ ] that give information about the project including Title Author Copyright Version And so forth Controls, Events, and more November 7, 2018

4 AssemblyInfo.cs Example
Author Version information Controls, Events, and more November 7, 2018

5 The Application Class The information from AssemblyInfo.cs is available to your Windows Forms application to use during execution Primarily accessed through a .NET class named Application using static methods and properties such as CompanyName CurrentCulture ProductName ProductVersion OpenForms And so forth If the AssemblyInfo.cs file is up-to-date with version information and so forth, the application code need not be updated when versions change, and so forth Controls, Events, and more November 7, 2018

6 Like Java’s JOptionPane dialogs
Example Like Java’s JOptionPane dialogs Controls, Events, and more November 7, 2018

7 Docking, anchoring, and resizing
Be in control of how windows look once resized Controls, Events, and more November 7, 2018

8 Docking and Anchoring Controls may be placed on a form at any point the designer wishes They can be dragged/dropped from the tool box They can be moved in the design view The buttons on the Layout toolbar may be used to align, center, and resize controls Panels can be used as in Java to isolate and group some components of a window from others The alignment and centering then applies to the panel Their properties can be set to change the position either in the design view or at run time Controls, Events, and more November 7, 2018

9 Docking and Anchoring When controls are placed in a fixed position on a form, they stay there even when the window is resized Controls, Events, and more November 7, 2018

10 Anchoring The client area of the window is the part of the window that does not include the caption bar, the borders around the window, … Controls may be anchored to the top, bottom, left, or right sides of the client area of the window Controls may be anchored to more than one side of the client area By default, most controls are anchored to both the top and the left sides of the client area When the window is resized, they stay in a fixed position relative to their anchors as illustrated on the previous slide Controls, Events, and more November 7, 2018

11 Anchoring In the design view, one may set the Anchor property as shown in this diagram … top, … … right, … Anchored on the left … … but not the bottom Clicking on one of the 4 rectangles creates or removes the respective anchor Controls, Events, and more November 7, 2018

12 Effects of Anchoring If the textboxes in this window are anchored to the top and right, resizing the window keeps them in a fixed position relative to their anchors Controls, Events, and more November 7, 2018

13 Effects of Anchoring If a control is anchored to opposite sides of the window (top and bottom or to the left and right), resizing the window in that direction stretches/shrinks the control in that direction if the control is stretchable in that direction – a label is not stretchable, for example Controls, Events, and more November 7, 2018

14 Docking When appropriate, some controls may be “docked” to one of the 4 sides of the client area of a window This attaches the docked control to that side The control is sized appropriately A control may be docked to Fill the remaining space as well When the window is resized, the docked controls may be resized as appropriate for their docked position Controls, Events, and more November 7, 2018

15 Five panel controls with different background colors shown docked
Docking Five panel controls with different background colors shown docked Controls, Events, and more November 7, 2018

16 Docking Docking properties may be set at design time using the properties tab Dock at top Fill remaining area Dock on right Dock at left Dock at bottom Do not dock Controls, Events, and more November 7, 2018

17 Prevent Resizing by User
There are two ways in which a user typically resizes a window Maximize button Drag the border of the window Each of these ways may be enabled or disabled At design time At run time Controls, Events, and more November 7, 2018

18 Disable the Maximize Button
At design time, select the form in the design view Open the properties tab Set the Maximize Box property to False May also disable the minimize button Controls, Events, and more November 7, 2018

19 Disable Resizing by Dragging Border
In the design view, select the form Open the properties tab Set the FormBorderStyle to one of the Fixed types such as Fixed3D Choose a Fixed Style Controls, Events, and more November 7, 2018

20 Disable Resizing: A Caveat
Often, windows can be made to look very unprofessional or even unusable if resized incorrectly by the user We can prevent the user from doing so We should be careful in deciding whether to do so There are MANY different monitors with MANY different resolutions A fixed size window may look great on the designer’s monitor but be totally unusable on another – because a button or a text box or a list control is off the visible window Be careful in choosing colors for the same reason – the designer may have a black wallpaper so a white font looks good while the user may have a white wallpaper, for example What looks good on one monitor may not be usable on another The solution: test, test, test on many different resolutions and color schemes Controls, Events, and more November 7, 2018

21 Controls Without Visual Representations
Controls that perform a service but do not have visual representations Controls, Events, and more November 7, 2018

22 Non-Visual Controls Most Windows controls have a visual representation on a Windows form Label TextBox Button And so forth A few controls, however, provide a service without having a visual representation One example is the timer control Controls, Events, and more November 7, 2018

23 Timer Control Many applications may use a timer
Network or database connection “times out” if inactive for some designated period An operating system “shares” processor time among multiple applications Educational software may give a timed test Some electronic card or board games may give each player a time limit in which to make a play Some applications may need a “clock” display The timer control may be used to facilitate some of these types of activities Controls, Events, and more November 7, 2018

24 Using the Timer Control
Drag the Timer Control from the Toolbox onto the form and drop it anywhere The timer control “bounces off” the form and lands in the tray below the form Drag and Drop Controls, Events, and more November 7, 2018

25 Using the Timer Control
Like other controls, the timer control has a set of properties that may be set/used Object name in program Must enable the timer to use it Interval until a “Tick” event occurs. Measured in milliseconds (5000 represents 5 seconds) Controls, Events, and more November 7, 2018

26 Using the Timer When the timer is enabled during program execution, the timer runs When the timer interval expires, a Tick event occurs This event may be handled or ignored like other events As long as the timer is enabled, it starts timing again immediately so that the Tick event will occur periodically – at the end of each specified interval Controls, Events, and more November 7, 2018

27 Tick Event Handler Double-clicking on the timer in the tray in the design view generates a skeleton Tick event handler One may add code to take appropriate action No need to reset the timer – that is automatic unless the timer is disabled or stopped by the program A digital clock application takes only a few lines of code Controls, Events, and more November 7, 2018

28 Digital Clock Application
Form below has one Label with a Blue Background and Gold Foreground (for ETSU) The Label is docked (its Dock property is set to Fill) to Fill the client area of the Form with the Form sized appropriately for the application Fill Blue area is the Label’s background Controls, Events, and more November 7, 2018

29 Digital Clock Application
Form_Load event initializes the clock: sets form caption with the date and sets clock to current time Update the date and time every time the timer expires (set to one second interval) Controls, Events, and more November 7, 2018

30 Digital Clock Application
Controls, Events, and more November 7, 2018

31 More on events and event handlers
Enter and KeyPress events; using the handler arguments; doing double duty Controls, Events, and more November 7, 2018

32 The Enter Event For some controls such as TextBox, the Enter event occurs when they gain the focus (i.e., when program control or the “input” focus “enters” them) The Leave event occurs when the control loses focus The handler for the Enter event may take appropriate actions such as SelectAll the text, Clear the text box, or validate its contents The Enter event is just one of many in the event list found in the Properties window for many controls Controls, Events, and more November 7, 2018

33 Double-click any event to generate a skeleton of its event-handler
Enter Event Return to properties Double-click any event to generate a skeleton of its event-handler Event List Enter Event Controls, Events, and more November 7, 2018

34 Now we can use TextBox methods on tb
Enter Event Handler The code below was intended for use with the TextBox named txtPoints as seen from the name of the handler However, we can make it more general by determining what control was “entered” from the sender argument As an “Object”, we have to cast the sender as a more specific type (TextBox) in this case before we can use it as such (using the unique methods of the more specific type, for example) Cast sender as TextBox Now we can use TextBox methods on tb Controls, Events, and more November 7, 2018

35 Kill 2 birds with one stone
In the previous example, we can make the same event handler handle the same event for other TextBoxes as well We wrote the body of the handler to be general enough to handle any TextBox in this case Now, we simply need to register this handler for other TextBox Enter events Go to the .Designer.cs file for this form Locate the txtPoints Enter event handler registration in the generated code Copy to any other TextBox control and rename as needed Controls, Events, and more November 7, 2018

36 … 2 birds … Copy this line Paste here
Now, one event handler handles the Enter event for both txtPoints and txtHours Change to txtHours Controls, Events, and more November 7, 2018

37 One handler worked for both TextBoxes
See Results After calculating one GPA, if we want to do another … it helps to have this value “selected” so that typing replaces what is here Both TextBox contents are selected as the focus “enters” them, allowing easy editing One handler worked for both TextBoxes Controls, Events, and more November 7, 2018

38 KeyPress Event One way to help avoid situations in which bad input causes program crashes is to limit what the user is allowed to type into a TextBox or other control For example, if a user is to type the number of credits earned, then only digits should be typed If a user is to enter a number, then only characters such as digits, +, -, and . are allowed Should also allow a backspace in case the user types something incorrect and wants to erase and correct it The KeyPress event occurs each time the user presses a key Controls, Events, and more November 7, 2018

39 KeyPress Event The KeyPressEventArgs argument e to the handler for this event has a string property named KeyChar that identifies the key pressed One may interrogate this value to determine whether to process or ignore the pressed key - typically, an if statement is used To ignore a character, set the (already) Handled property of e to true The character will not even be echoed to the screen since the event has been marked as completely handled in this case Controls, Events, and more November 7, 2018

40 KeyPress Event Handler Example
If character typed is less than ‘0’ … … or greater than ‘9’ ( then it cannot be a digit) … … and it is not a backspace … … THEN we mark it as completely handled since we are discarding the offending character Controls, Events, and more November 7, 2018

41 Register KeyPress Handler for Other TextBoxes
Copy this line … … to here and change object name to txtHours Register this handler for any other TextBoxes to which it applies Controls, Events, and more November 7, 2018

42 More Complex KeyPress Event Handler
Should be == instead of != Controls, Events, and more November 7, 2018

43 Other controls Many different controls are available …
Controls, Events, and more November 7, 2018

44 Some other controls Datetime picker Month Calendar Menu Strip Groupbox
Checkboxes Numeric up-down Combo Box Progress Bar Picture Box List Box Status Strip Controls, Events, and more November 7, 2018

45 For a “tabbed” interface on a form
Controls Windows Forms Controls listed in the Toolbar … others can be added to the Toolbar For a “tabbed” interface on a form Controls, Events, and more November 7, 2018

46 MenuStrip MenuStrip One can add a new Menu item by simply typing it here An item can be added to the drop down by typing it here From the right-click context menu, one can add a separator line, a TextBox, or a ComboBox to the menu Once all items have been added, double click on a menu item to generate skeleton of an event handler for it Controls, Events, and more November 7, 2018

47 A ListBox is designed to display a list of strings
An item may be selected by clicking on it. Depending on ListBox properties, one may select multiple items Scroll bar is only visible if there are too many items in the list to show all at one time A ListBox is designed to display a list of strings Controls, Events, and more November 7, 2018

48 ListBox One may manually add items to the ListBox in the code (or in the design view) This example shows adding items in code Programmatically selects the first item in the list – exception if list is empty Controls, Events, and more November 7, 2018

49 List Contents at Design Time
ListBox Initialize items in the ListBox in the design view Collection Editor List Contents at Design Time Select Items Controls, Events, and more November 7, 2018

50 Set the DataSource for MyListBox to be the array named MyData
ListBox – Data Binding One may bind the contents of a ListBox to a collection object such as an array or List<type> Set the DataSource for MyListBox to be the array named MyData Controls, Events, and more November 7, 2018

51 Changing the DataSource
Controls, Events, and more November 7, 2018

52 Selection Changed Handling
When an item in a ListBox is selected by clicking on it, a SelectedIndexChanged event occurs It may be handled to take some action Retrieve the selected item Controls, Events, and more November 7, 2018

53 This picture is displayed in a PictureBox control
The PictureBox control is designed to make it easy to display a picture as illustrated here PictureBox control This picture is displayed in a PictureBox control Controls, Events, and more November 7, 2018

54 Using a Picture Box Control
The image may be set at design time using a property Some PictureBox properties are shown here This picture comes from the Resources file Set the image property Controls, Events, and more November 7, 2018

55 Resources Can be Set from Project Properties
May Add new Resources Resources tab of the Project Properties Dialog Controls, Events, and more November 7, 2018

56 Adding an Image to a Resource
Can add existing files Can add new pictures in any of these formats Controls, Events, and more November 7, 2018

57 Internationalizing a Program
Controls, Events, and more November 7, 2018

58 Internationalizing a Program
Replace EVERY literal string in the code by a reference to a string resource Replace the resources but not the code to have the program run in a different language Controls, Events, and more November 7, 2018

59 Setting the Picture from Code
One may also set the Image property of the control at runtime Controls, Events, and more November 7, 2018

60 Create Your Own Web Browser
You may use the WebBrowser control to create your own web browser or to create web browsing capability as part of a larger desktop application Controls, Events, and more November 7, 2018

61 The Web Browser Application
WebBrowser control – docked to fill rest of the form Panel control with a Label, TextBox, and Button on it – docked at the top of the form Controls, Events, and more November 7, 2018

62 Code for the WebBrowser App
This is all of the code required… When user types a new URL and clicks Go, update the WebBrowser’s URL from the TextBox When user clicks a link, update the TextBox Controls, Events, and more November 7, 2018


Download ppt "Controls, Events, and More"

Similar presentations


Ads by Google