Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo...

14
Intro to More Controls in C#

Transcript of Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo...

Page 1: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

Intro to More Controls in C#

Page 2: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

C# Demonstration• We already touched on labels and buttons• Ad-hoc demo of controls

– Textboxes• Multiline

– Checkbox– Radiobutton– Picturebox– Group box

• Tab order• Tooltips

Page 3: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

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

Page 4: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

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

Page 5: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

Event Example• For example, creating a button and then double-

clicking it results in the following event handler:

• For now, don’t worry about the object sender, EventArgs 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.

Page 6: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

Event handler experimentation

• Try adding some code to change the properties of other controls on the form

• There are 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

Page 7: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

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

textBox1.Clear();

textBox1.Text = "";

Method to clear the textbox

Manually set the textbox to a blank

Both do the same thing

Page 8: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

String concatenation

• Text enclosed in "" are called strings• We can concatenate (or glue together) two

strings with the + symbol• What will this do?

textBox1.Text = textBox1.Text + " HELLO ";

textBox2.Text = txtName.Text + " is your name ";

Page 9: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

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

• /* */– Everything in between the /* and */ is ignored

• Can use these buttons on selected text

// textBox1.Text = " HELLO ";

/* textBox1.Text = " HELLO "; */

Page 10: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

General Format of a C# Programusing System;using System.Text;using... namespace NameOfProject{ class NameOfProgram { public or private variable declarations public or private method declarations  public void method(arguments) { Code here for the method... } }}

Other namespaces to use

Page 11: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

Outputting Messages

• Label or TextBox• Sometimes the MessageBox method is more

convenient, especially for outputting temporary information, no need to create the control– MessageBox.Show(Message);– Example:

MessageBox.Show("Hello World!");

Page 12: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

MessageBox

• What’s wrong with this?

MessageBox.Show("Britney Spears said, "I get to go to lots of overseas places, like Canada."");

Page 13: Intro to More Controls in C#. C# Demonstration We already touched on labels and buttons Ad-hoc demo of controls – Textboxes Multiline – Checkbox – Radiobutton.

Escape Character

• Solution: The Escape character says to interpret the next character literally

\n Newline\t Tab

\\ Backslash character \" Double quote character

MessageBox.Show("Britney Spears said, \"I get to go to lots of overseas places, like Canada.\"");

Page 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.

Console

• Sometimes it is also convenient to use Console.WriteLine– Not as intrusive as a MessageBox– Have to View the Output Window to see the

results– Handy for quick debugging to print out values

– Console.WriteLine("Hello world");