Top Banner
Programming Interface Controls ISYS 350
43

Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Dec 31, 2015

Download

Documents

Jade Horton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Programming Interface Controls

ISYS 350

Page 2: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

User Interface Controls• Form• MessageBox• Common Controls:

– Button, TextBox, MaskedTextBox, List Box, Option Button, Check Box, CheckedListBox, numericUpDown

• Container controls:– GroupBox, etc.

• Others:– Timer– ToolTip– Components

Page 3: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Working with Form

• To close a form:– this.Close();

• Events:– Load, Activated, DeActivate, Closing, Closed

Page 4: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Form Closing Eventprivate void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) == DialogResult.No) { e.Cancel = true; } }

Note: sender – object that triggers the event; e – event argument that provides actions to handle the event.

Page 5: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

MessageBoxMessageBox.Show(message)

MessageBox.Show(message, Caption)

MessageBox.Show(message, Caption, Buttons)

Note:

1. In each format, arguments are positional and required.

2. This object returns a DialogResult data type. Possible values for a DialogResult data type are: Abort, Cancel, Ignore, No, None, OK, ReTry, and Yes. To test the return value:

Dim ReturnVal as DialogResult

ReturnVal=MessageBox(“hello”, …..)

If ReturnVal=DialogResult.OK…

Page 6: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Text Box

• Useful properties– BackColor, BorderStyle– ReadOnly– Enable– Visible– Password Character– Multiline– ScrollBar– Text

• Useful events– TextChanged: default event– Validating – useful for validating data entered in the box

Page 7: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Input Validation• Numbers are checked to ensure they are:

– Within a range of possible values– Reasonableness– Not causing problems such as division by 0.– Containing only digits

• IsNumeric

• Texts are checked to ensure correct format.– Phone #, SSN.

• Required field• Textbox:

– Set CauseValidation property to true.– Use the Validating event:

• Triggered just before the focus shifts to other control.

Page 8: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Data Entered in Textbox1 Must Be between 10 and 30

private void textBox1_Validating(object sender, CancelEventArgs e) { double enteredData; enteredData = double.Parse(textBox1.Text); if (enteredData<10 || enteredData>30) { MessageBox.Show("Pls enter a number between 10 and 30"); e.Cancel=true; } }

Page 9: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Testing for digits onlyThis example uses the Double.Parse method trying to convert the data entered in the box to double. If fail then it is not numeric.

private void textBox1_Validating(object sender, CancelEventArgs e) { try { double.Parse(textBox1.Text); e.Cancel = false; } catch { e.Cancel = true; MessageBox.Show("Enter digits only"); } }

Note: VB has an IsNumeric function and Excel has an ISNumber function.

Page 10: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Testing for Blank (required field)private void textBox1_Validating(object sender, CancelEventArgs e) { string str = textBox1.Text; bool blank = false; if (str == "") //Method 1 { blank = true; } if (string.Equals(str, "")) //Method 2 { blank = true; } if (string.IsNullOrEmpty(str)) //Method 3 { blank = true; } if (blank) { MessageBox.Show("Textbox cannot be blank"); e.Cancel = true; } }

Page 11: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Group Box

• It is a container control.

• Controls in a Group Box should move with the box.

Page 12: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Radio Button

• Radio buttons must be grouped together inside a container such as a GroupBox or a form.

• When the user selects an option all other options in the same group are deselected.

• Properties:– Checked: True/False.

• Default button: Set the Checked property to true at the design time.

• Events: – CheckedChanged

Page 13: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Example

private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) MessageBox.Show("Radiobutton1 checked"); else MessageBox.Show("Radiobutton1 unchecked"); }private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) MessageBox.Show("Radiobutton2 checked"); else MessageBox.Show("Radiobutton2 unchecked"); }private void radioButton3_CheckedChanged(object sender, EventArgs e) { if (radioButton3.Checked) MessageBox.Show("Radiobutton3 checked"); else MessageBox.Show("Radiobutton3 unchecked"); }

Page 14: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Tuition Rules

For undergraduate: If total units <= 12, then tuition = 1200; Otherwise, tuition = 1200 + 200 per

additional unitFor graduate:

If total units <= 9, then tuition = 1500; Otherwise, tuition = 1500 + 400 per

additional unit

Page 15: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Nested Decision Structures• You can create nested decision structures to test more than one condition.

• Nested means “one inside another”

• In C#, a generic format is:if (expression){ if (expression) { statements; } else { statements; }}else { statements}

Page 16: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

private void button1_Click(object sender, EventArgs e) { double units, tuition; units=double.Parse(textBox1.Text); if (radioButton1.Checked) { if (units <= 12) tuition = 1200; else tuition = 1200 + 200 * (units - 12); } else { if (units <= 9) tuition = 1500; else tuition = 1500 + 400 * (units - 9); } textBox1.Text=tuition.toString();}

Page 17: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Check Box

• Check boxes do not belong to a group even when they are grouped in a Group Box.

• Checked property and checkedChanged event

Page 18: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Which CheckBoxes are cheked?

private void button1_Click(object sender, EventArgs e) { string box=""; if (checkBox1.Checked) box += "box1"; if (checkBox2.Checked) box += "box2"; if (checkBox3.Checked) box += "box3"; MessageBox.Show(box); }

Note: Cannot use if-else-if

Page 19: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Rules for Discount

If total sales is greater than 1000, then the customer will get a 10% discount ; otherwise, the customer will get a 5% discount.

If the customer is a club member, then applies 20% off the discounted charges.

Page 20: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

private void button1_Click(object sender, EventArgs e) { try { double totalSales, discountRate, netPay; string myMsg; totalSales = double.Parse(textBox1.Text); if (totalSales <= 1000) discountRate = .05; else discountRate = .1; netPay = totalSales * (1 - discountRate); if (checkBox1.Checked) netPay = netPay * (1 - .20); textBox2.Text = netPay.ToString("C"); } catch (Exception ex) { MessageBox.Show(ex.Message); } }

Page 21: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

List Box

• Useful properties– Items: The items in the listBox. It is a collection

strcture. Items can be entered at the design time or entered in code.

• 0-based index– SelectionMode: one or multi selection– SelectedItem– SelectedIndex

• Methods– Add– Clear

• Event: SelectedIndexChange

Page 22: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

ListBox Items Collections• Methods:

– ADD: ListBox1.Items.Add("Apple")

– Item: Retrieve an object from Items• ListBox1.Items.Item(Index) or ListBox1.Items(Index)

• 0-based index

– Insert: ListBox.Items.Insert(Index, item)

– Remove: Delete an object with a position index or key.• ListBox.Items.Remove(Item)

• ListBox.Items.RemoveAt(Index)

– Clear: ListBox.Items.Clear()

– Count: Return the number of objects in a collection.• ListBox.Items.Count

Page 23: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Creating Listbox at the Design View• 1. Drag and drop a Listbox control to the design view.• 2. From the Listbox Property window, select the

Listbox Items property and click the Collection Editor button to open the Collection Editor.

• 3. Enter items.

Page 24: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

double pv, rate, years, fv; pv = double.Parse(textBox1.Text); years = double.Parse(textBox2.Text); if (listBox1.SelectedIndex == 0) { rate = .03; } else if (listBox1.SelectedIndex==1) {rate=.04;} else if (listBox1.SelectedIndex == 2) { rate = .05; } else if (listBox1.SelectedIndex == 3) { rate = .06; } else { rate = .07; }

if (listBox1.SelectedItem.ToString()== "3%") { rate = .03; } else if (listBox1.SelectedItem.ToString() == "4%") { rate = .04; } else if (listBox1.SelectedItem.ToString() == "5%") { rate = .05; } else if (listBox1.SelectedItem.ToString() == "6%") { rate = .06; } else { rate = .07; }

fv = pv * Math.Pow(1 + rate, years); textBox3.Text = fv.ToString("c");

Future Value Calculation

Page 25: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Adding Items Using Code

private void Form11_Load(object sender, EventArgs e) { listBox1.Items.Add("Apple"); listBox1.Items.Add("Orange"); listBox1.Items.Add("Banana"); listBox1.Items.Add("Strawberry"); listBox1.Items.Add("Kiwi"); }

private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show(listBox1.SelectedItem.ToString()); MessageBox.Show(listBox1.SelectedIndex.ToString()); }

Page 26: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Working with Radiobuttons, Listbox• Create a form with 2 radiobuttons. When radiobutton1

is selected, populate a listbox with fruit names.; otherwise populate the listbox with vegetable names. Then, dsplay the fruit or vegetable’s name in a textbox when user select an item from the listbox.

Page 27: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

private void radioButton1_CheckedChanged(object sender, EventArgs e) { if (radioButton1.Checked) { listBox1.Items.Clear(); listBox1.Items.Add("Apple"); listBox1.Items.Add("Orange"); listBox1.Items.Add("Banana"); listBox1.Items.Add("Strawberry"); listBox1.Items.Add("Kiwi"); } } private void radioButton2_CheckedChanged(object sender, EventArgs e) { if (radioButton2.Checked) { listBox1.Items.Clear(); listBox1.Items.Add("Spinach"); listBox1.Items.Add("Lettuce"); listBox1.Items.Add("Kale"); listBox1.Items.Add("Tomato"); listBox1.Items.Add("Carrot"); } } private void listBox1_SelectedIndexChanged(object sender, EventArgs e) { MessageBox.Show("You select: " + listBox1.SelectedItem.ToString()); }

Page 28: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Create a Loan Payment Form

Page 29: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Using VB.Net’s PMT Function

• Add a reference to Microsoft Visual Baisc– From the Solution Explorer, right-click the

References node, then click Add Reference– From the .Net tab, select Microsoft Visual

Baisc– Add this code to the form:

• using Microsoft.VisualBasic;

Page 30: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

private void button1_Click(object sender, EventArgs e) { double loan, term, rate, payment; int myIndex; loan = Double.Parse(textBox1.Text); if (radioButton1.Checked) { term = 15; } else { term = 30; } myIndex = listBox1.SelectedIndex; if (myIndex == 0) { rate = .05; } else if (myIndex == 1) { rate = .06; } else if (myIndex == 2) { rate = .07; } else if (myIndex == 3) { rate = .08; } else { rate = .09; } payment = Financial.Pmt(rate / 12, term * 12, -loan); textBox2.Text = payment.ToString(); }

Page 31: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

The switch Statement• The switch statement lets the value of a variable or an

expression determine which path of execution the program will take

• It is a multiple-alternative decision structure• It can be used as an alternative to an if-else-if

statement that tests the same variable or expression for several different values

Page 32: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Generic Format of switch Statementswtich (testExpression)

{

case value_1:

statements;

break;

case value_2:

statements;

break;

case value_n:

statements;

break;

default:

statements;

break;

}

• The testExpression is a variable or an expression that given an integer, string, or bool value. Yet, it cannot be a floating-point or decimal value.

• Each case is an individual subsection containing one or more statements, followed by a break statement

• The default section is optional and is designed for a situation that the testExpression will not match with any of the case

Page 33: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Sample switch Statementswitch (month)

{

case 1:

MessageBox.Show(“January”);

break;

case 2:

MessageBox.Show(“February”);

break;

case 3:

MessageBox.Show(“March”);

break;

default:

MessageBox.Show(“Error: Invalid month”);

break;

}

month

Display “January”

Display “February”

Display “March”

Display “Error: Invalid month”

Page 34: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

private void button1_Click(object sender, EventArgs e) { double loan, term, rate, payment; loan = Double.Parse(textBox1.Text); if (radioButton1.Checked) { term = 15; } else { term = 30; } switch (listBox1.SelectedIndex) { case 0: rate=.05; break; case 1: rate=.06; break; case 2: rate = .07; break; case 3: rate = .08; break; case 4: rate = .09; break; default: rate = 0.05; break; } payment = Financial.Pmt(rate / 12, term * 12, -loan); textBox2.Text = payment.ToString(); }

Page 35: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

How to Use VB’s IsNumeric Function

• Add a reference to Microsoft VisualBasic • Then, add this code to the form:

– using Microsoft.VisualBasic;

• Microsoft.VisualBasic.Information class contains the IsNumeric function

if (! Information.IsNumeric(textBox1.Text)) { e.Cancel = true; MessageBox.Show("Enter digits only"); } else { e.Cancel=false; }

Page 36: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Switch section’s ends with a break statementExample:Enter a digit and test its value

int myInt = int.Parse(textBox1.Text); switch (myInt) { case 0: case 1: case 2: MessageBox.Show(" 0,1, or 2"); break; case 3: case 4: case 5: MessageBox.Show(" 3,4, or 5"); break; default: MessageBox.Show("between 6 and 9"); break; }

Page 37: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

ComboBox• Allows the user to type text directly into the

combo box.• Use the Text property to get entered item:

– ComboBox1.Text– The index for an entered item is –1.

• Search an item in the list: ComboBox1.Items.IndexOf(“search text”)– Found: return the index of the search text.– Not found: return –1.

• How to add an entered item to the list?

Page 38: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Working with Form

• To close a form:– this.Close();

• Events:– Load, Activated, DeActivate, Closing, Closed

Page 39: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Form Closing Eventprivate void Form2_FormClosing(object sender, FormClosingEventArgs e) { if (MessageBox.Show("Are you sure?", "Warning", MessageBoxButtons.YesNo) == DialogResult.Yes) { e.Cancel = false; } else { e.Cancel = true; } }

Note: sender – object that triggers the event; e – event argument that provides actions to handle the event.

Page 40: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

MessageBoxMessageBox.Show(message)

MessageBox.Show(message, Caption)

MessageBox.Show(message, Caption, Buttons)

Note:

1. In each format, arguments are positional and required.

2. This object returns a DialogResult data type. Possible values for a DialogResult data type are: Abort, Cancel, Ignore, No, None, OK, ReTry, and Yes. To test the return value:

Dim ReturnVal as DialogResult

ReturnVal=MessageBox(“hello”, …..)

If ReturnVal=DialogResult.OK…

Page 41: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Timer• Properties:

• Enabled -- must set to True.• Interval

• Tick Event

private void timer1_Tick(object sender, EventArgs e) { textBox1.Text = System.DateTime.Now.ToString(); }

Page 42: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Use a Timer to Close a Form

int counter = 0; private void timer1_Tick(object sender, EventArgs e) { counter+=1;

if (counter > 50) { this.Close(); } }

Page 43: Programming Interface Controls ISYS 350. User Interface Controls Form MessageBox Common Controls: –Button, TextBox, MaskedTextBox, List Box, Option Button,

Close a form after 10 seconds:Set interval to 1000

int counter = 0; private void timer1_Tick(object sender, EventArgs e) { label2.Text= System.DateTime.Now.ToString(); ++counter; if (counter > 10) this.Close(); }