Intro to More Controls in C C Demonstration














- Slides: 14

Intro to More Controls in C#

C# Demonstration • We already touched on labels and buttons • Ad-hoc demo of controls – Textboxes • Multiline – Checkbox – Radiobutton – Picturebox – Group box • Tab order • Tooltips

Events • GUI applications are generally event-driven. – The application waits until some event occurs, and then executes some code to handle the event. – Typical user-initiated events • Pressing a key • Clicking the mouse – C# handle events by creating a method. • A method is a grouping of code that performs a common task – We’ll start by creating button click events

Creating an Event Handler • Double-click the control in the design view to bring up the most common event for that control • Select the control in the design view, then click on the lightning bolt in the properties window to find the event you’re interested in for the selected control

Event Example • For example, creating a button and then doubleclicking it results in the following event handler: • For now, don’t worry about the object sender, Event. Args e part. For now all you really need to know is that any code you enter between the curly braces will be executed when the button is clicked.

Event handler experimentation • Try adding some code to change the properties of other controls on the form • There assignment statements that copy the value on the right into the object on the left (not the same as mathematical equality) • Every statement ends in a semicolon

Methods • In our code we can also invoke methods associated with some object – Methods have ()’s at the end; later we will put things inside the parenthesis text. Box 1. Clear(); Method to clear the textbox text. Box 1. Text = ""; Manually set the textbox to a blank Both do the same thing

String concatenation • Text enclosed in "" are called strings • We can concatenate (or glue together) two strings with the + symbol • What will this do? text. Box 1. Text = text. Box 1. Text + " HELLO "; text. Box 2. Text = txt. Name. Text + " is your name ";

Comments • Commenting helps explain how your code works • Commenting can be used to disable code without deleting it if you want it back later • // – Everything from // to the end of the line is ignored // text. Box 1. Text = " HELLO "; • /* */ – Everything in between the /* and */ is ignored /* text. Box 1. Text = " HELLO "; */ • Can use these buttons on selected text

General Format of a C# Program using System; using System. Text; using. . . namespace Name. Of. Project { class Name. Of. Program { public or private variable declarations public or private method declarations public void method(arguments) { Code here for the method. . . } } } Other namespaces to use

Outputting Messages • Label or Text. Box • Sometimes the Message. Box method is more convenient, especially for outputting temporary information, no need to create the control – Message. Box. Show(Message); – Example: Message. Box. Show("Hello World!");

Message. Box • What’s wrong with this? Message. Box. Show("Britney Spears said, "I get to go to lots of overseas places, like Canada. "");

Escape Character • Solution: The Escape character says to interpret the next character literally n Newline t Tab \ Backslash character " Double quote character Message. Box. Show("Britney Spears said, "I get to go to lots of overseas places, like Canada. "");

Console • Sometimes it is also convenient to use Console. Write. Line – Not as intrusive as a Message. Box – Have to View the Output Window to see the results – Handy for quick debugging to print out values – Console. Write. Line("Hello world");