Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing with C# and the .NET Framework

Similar presentations


Presentation on theme: "Computing with C# and the .NET Framework"— Presentation transcript:

1 Computing with C# and the .NET Framework
User Interfaces

2 Controls A control is a component with a visual representation
A Form is a Control that can contain other controls Controls introduced in this chapter include Button, Label, TextBox, RadioButton, ComboBox, ListBox, PictureBox, and CheckBox

3 Configuring a Form .NET Framework – write code in constructor
(using Visual Studio .NET – change properties, drag controls) Text = “Button Press”; // text in form’s title Size = new Size(300, 200); // size of form Controls.Add(print); // add a button Controls.Add(message); // add a label

4 Configuring a control print.Text = “Print”; // set button label
print.Location = new Point(20, 30); //place button message.Text = “Message goes here”; // label Message.Size = new Size (message.PreferredWidth, message.PreferredHeight); // make label big enough to hold message (Using Visual Studio .NET, set properties to generate this code automatically)

5 Events A class may contain an event that allows it to provide notifications. A Button has a Click event that will occur when the user click the button. To make the button work, we register an EventHandler delegate with it. When the user clicks the button, the code in the registered event handler will be called

6 Handling a Click event private Button print = new Button() //the control print.Click += new EventHandler(Print_Click); // register Print_Click method, which must be // an EventHandler type with two parameters and no return value protected void Print_Click(Object sender, EventArgs e){ message.Text = "Hi there"; } // set message label when user clicks button

7 The .NET Button code Declares a Click event (inherited from Control).
Associates an EventHandler delegate as the type of method needed to handle a Click event. Raises the Click event when the user clicks the button, causing calls to all registered delegates. We program a ButtonPress application --

8 Our ButtonPress code Declares a Print_Handler method for the Print button that fits the definition of the EventHandler delegate. Registers Print_Handler as a new delegate to handle the Click event for the Print button.

9 Delegates A delegate defines a class that encapsulates one or more methods To make a button work we register an EventHandler delegate that .NET has predefined as public delegate void EventHandler(Object sender, EventArgs e) Print_Click must have these same parameters and return type

10 TextBox A TextBox lets the user enter data
By default it allows a single line Clicking EnterPrice adds value in text box to sum Clicking Average displays the average.

11 RadioButton and ComboBox
All radio buttons in a Form belong to one group by default. Selecting one deselects the others. A ComboBox contains a list of items, showing one, and allowing the user to pop up the rest. // Add items to combo box color.Items.Add("Red"); color.Items.Add("Green"); color.Items.Add("Blue");

12 Example 9.4 Handle the button Click event. When the user clicks the button, we determine which radio button is check and which item is selected in the combo box.

13 Example 9.5 Respond to radio buttons and combo box immediately.
Selecting a RadioButton generates a CheckedChanged event Selecting a ComboBox item generates a SelectedIndexChanged event.

14 StringBuilder A String cannot be changed. Repeated concatenation causes the allocation of new String objects. Using StringBuilder is more efficient. StringBuilder s = new StringBuilder(“The “); s.Append(animal1); s.Append(“ and “);

15 PictureBox Use PictureBox to display an image
private PictureBox photo = new PictureBox(); photo.Image = Image.FromFile(“gittleman.gif”);

16 ListBox Displays a list of items SelectionMode.One -- choose one item
SelectionMode.MultiSimple – select multiple items Selecting an item generates a SelectedIndexChanged event SelectedItem property -- returns Object selected SelectedItems returns a collection of items selected. Use array notation to access each

17 CheckBox Can select more than one CheckBox, or none
A user selection generates a CheckedChanged event Register an EventHandler delegate author.CheckedChanged += new EventHandler(Photo_Changed); Put event handling code in Photo_Changed method

18 Using Visual Studio .NET
Create a C# Windows Application project Click on a control in Toolbox and click on Form to position the control Visual Studio .NET generates all the initialization code in the InitializeComponent method. Double-click on a control to bring up a code template for its event handler. Fill in code.


Download ppt "Computing with C# and the .NET Framework"

Similar presentations


Ads by Google