+ All documents
Home > Documents > Lecture Eleven Third Stage First Course 2021 - 2022

Lecture Eleven Third Stage First Course 2021 - 2022

Date post: 22-Nov-2023
Category:
Upload: khangminh22
View: 0 times
Download: 0 times
Share this document with a friend
12
University of Anbar College of Computer Science and Information Technology Computer Network Systems Department Lecture Eleven Third Stage First Course 2021 - 2022 Seddiq Qais Abd Al-Rahman MSc Computer Science [email protected]
Transcript

University of Anbar

College of Computer Science

and Information Technology

Computer Network Systems Department

Lecture Eleven

Third Stage

First Course 2021 - 2022

Seddiq Qais Abd Al-Rahman

MSc Computer Science

[email protected]

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

2

Visual Programming

RadioButton

The radio button is another control in Visual Basic 2010 that allows selection of choices.

However, it operates differently from the check box. While the check boxes allow the user

to select one or more items, radio buttons are mutually exclusive, which means the user can

only choose one item only out of a number of choices. Here is an example which allows the

user to select one color only.

The Code:

private string strColor;

private void RadioButton8_CheckedChanged(System.Object sender,

System.EventArgs e)

{

strColor = "Red";

}

private void RadioButton7_CheckedChanged(System.Object sender,

System.EventArgs e)

{

strColor = "Green";

}

private void RadioYellow_CheckedChanged(System.Object sender,

System.EventArgs e)

{

strColor = "Yellow";

}

private void Button1_Click(System.Object sender, System.EventArgs e)

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

3

{

Label2.Text = strColor;

}

Although the user may only select one item at a time, he may make more than one selection

if those items belong to different categories. For example, the user wishes to choose T-shirt

size and color, he needs to select one color and one size, which means one selection in each

category. This is easily achieved in Visual Basic 2010 by using the Groupbox control under

the containers categories. After inserting the Groupbox into the form, you can proceed to

insert the radio buttons into the Groupbox. Only the radio buttons inside the Groupbox are

mutually exclusive, they are not mutually exclusive with the radio buttons outside the

Groupbox. In Example below, the user can select one color and one size of the T-shirt.

Example:

THE CODE

private string strColor;

private string strSize;

private void RadioButton8_CheckedChanged(System.Object sender,

System.EventArgs e)

{

strColor = "Red";

}

private void RadioButton7_CheckedChanged(System.Object sender,

System.EventArgs e)

{

strColor = "Green";

}

private void RadioYellow_CheckedChanged(System.Object sender,

System.EventArgs e)

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

4

{

strColor = "Yellow";

}

private void Button1_Click(System.Object sender, System.EventArgs e)

{

Label2.Text = strColor;

Label4.Text = strSize;

}

private void RadioXL_CheckedChanged(System.Object sender, System.EventArgs e)

{

RadioXL.CheckedChanged();

strSize = "XL";

}

private void RadioL_CheckedChanged(System.Object sender, System.EventArgs e)

{

strSize = "L";

}

private void RadioM_CheckedChanged(System.Object sender, System.EventArgs e)

{

strSize = "M";

}

private void RadioS_CheckedChanged(System.Object sender, System.EventArgs e)

{

strSize = "S";

}

Demonstrate the use of ListBox in C#

Here, we are implementing a windows application that will demonstrate example of ListBox

in C# with some of the basic operations like Add, Remove, Clear, Get Selected Items etc.

Submitted by IncludeHelp, on September 18, 2018

Following operations are performing on the ListBox:

1. Add

2. Remove

3. Clear

4. Get selected items

5. etc...

Follow controls are using in the application:

• txtInput (TextBox) : To take user input.

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

5

• lblCount (Label) : To show count of list-box items.

• lstItem (ListBox) : List-box to contain list of items.

• btnAdd (Button) : To add entered item into list.

• btnRemove (Button) : To remove selected item from list.

• btnShow (Button) : To show selected item in message-box.

• btnClear (Button) : To clear complete list.

Example (form design):

C# Source Code:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

6

using System.Windows.Forms;

namespace MyWinApp

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

private void btnAdd_Click(object sender, EventArgs e)

{

lstItem.Items.Add(txtInput.Text);

txtInput.Text = "";

lblCount.Text = "Count: " + lstItem.Items.Count;

}

private void btnRmv_Click(object sender, EventArgs e)

{

lstItem.Items.RemoveAt(lstItem.SelectedIndex);

lblCount.Text = "Count: " + lstItem.Items.Count;

}

private void btnShow_Click(object sender, EventArgs e)

{

MessageBox.Show(lstItem.SelectedItem.ToString());

}

private void btnClr_Click(object sender, EventArgs e)

{

lstItem.Items.Clear();

lblCount.Text = "Count: " + lstItem.Items.Count;

}

private void Form1_Load(object sender, EventArgs e)

{

lblCount.Text = "Count: " + lstItem.Items.Count;

}

}

}

In the above code, we used button click events for performing tasks. We used following

methods:

1. Listbox.Itmes.Add(text)

2. Listbox.Itmes.RemoveAt(index)

3. Listbox.Itmes.Clear()

We used some properties like:

1. lstItem.Items.Count

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

7

2. lstItem.SelectedIndex

3. lstItem.SelectedItem

Application – Currency Converter

After we've done some exercises on conditional statements (checks), now let's do

something more interesting: an application with a graphical user interface (GUI) for

converting currencies. We will use the knowledge from this chapter to choose from several

available currencies and make calculations at different rate to the selected currency.

Now let's see how to create a graphical (GUI) app for currency conversion. The app will

look like the picture below:

It converts Bulgarian levs (BGN) to Euro (EUR), US Dollars (USD) or Great Britain Pounds

(GBP).

This time we create a new Windows Forms Application with name “Currency-Converter”:

We order the following controls in the form:

• One box for entering a number (NumericUpDown)

• One drop-down list with currencies (ComboBox)

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

8

• Text block for the result (Label)

• Several inscriptions (Label)

We set the sizes and their properties to look like the picture below:

Configuring the UI Controls

We apply the following settings for the UI controls:

• For the main form (Form) that contains all the controls:

• (name) = FormConverter

• Text = "Currency Converter"

• Font.Size = 12

• MaximizeBox = False

• MinimizeBox = False

• FormBorderStyle = FixedSingle

• For the field for entering a number (NumericUpDown):

• (name) = numericUpDownAmount

• Value = 1

• Minimum = 0

• Maximum = 1000000

• TextAlign = Right

• DecimalPlaces = 2

• For the drop-down list of currencies (ComboBox):

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

9

• (name) = comboBoxCurrency

• DropDownStyle = DropDownList

• Items =

• EUR

• USD

• GBP

• For the result text block (Label):

• (name) = labelResult

• AutoSize = False

• BackColor = PaleGreen

• TextAlign = MiddleCenter

• Font.Size = 14

• Font.Bold = True

Events and Event Handlers

We need to take the following events to write the C# code that will be executed upon their

occurrence:

The event ValueChanged of numeric entry control numericUpDownAmount:

The event Load of the form FormConverter

The event SelectedIndexChanged of the drop-down list for choosing the

currency comboBoxCurrency

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

10

We will use the following C# code for event handling:

private void FormConverter_Load(object sender, EventArgs e)

{

this.comboBoxCurrency.SelectedItem = "EUR";

}

private void numericUpDownAmount_ValueChanged(object sender, EventArgs e)

{

ConvertCurrency();

}

private void comboBoxCurrency_SelectedIndexChanged(object sender, EventArgs e)

{

ConvertCurrency();

}

Our task is to select the currency "EUR" when we start the program and change the values

in the sum or currency field then calculating the result by calling

the ConvertCurrency() method.

Writing the Program Code

We have to write the event ConvertCurrency() to convert the BGN amount into the selected

currency:

private void ConvertCurrency()

{

var originalAmount = this.numericUpDownAmount.Value;

var convertedAmount = originalAmount;

if (this.comboBoxCurrency.SelectedItem.ToString() == "EUR")

{

convertedAmount = originalAmount / 1.95583m;

}

else if (this.comboBoxCurrency.SelectedItem.ToString() == "USD")

{

convertedAmount = originalAmount / 1.80810m;

}

else if (this.comboBoxCurrency.SelectedItem.ToString() == "GBP")

{

convertedAmount = originalAmount / 2.54990m;

}

this.labelResult.Text = originalAmount + " BGN = " +

Math.Round(convertedAmount, 2) + " " + this.comboBoxCurrency.SelectedItem;

}

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

11

The above code takes the amount for converting the field numericUpDownAmount and the

selected currency for the result from the field comboBoxCurrency. Then with a conditional

statement, according to the selected currency, the amount is divided by the exchange

rate (which is fixed in the source code). Finally, a text message with the result (rounded to

the second digit after the decimal point) is generated and recorded in the green

box labelResult. Try it!

Lecture 11:RadioButton, ListBox, Currency Converter

3rd Stage, 1st Course, 2021-2022

Visual Programming I

Seddiq Abd Al-Rahman

12

References:

• Tony Gaddis, “Starting out with Visual C#.”, Fourth edition, Boston,

Pearson Inc., 2017.

• Salvatore A. Buono, “C# and Game Programming: A Beginner's Guide.”

Second Edition, Boca Raton, CRC Press Inc., 2019.

• Eric Butow and Tommy Ryan, "C#: Your Visual Blueprint for Building

.NET Applications.", Hungry Minds Inc., New York, 2002.

• Faraz Rasheed, “Programmer's Heaven: C# School.”, First Edition,

Fuengirola, Synchron Data, 2006.


Recommended