0% found this document useful (0 votes)
4 views

Classroom Exercises on Visual Studio

vb.net

Uploaded by

josephsesay1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Classroom Exercises on Visual Studio

vb.net

Uploaded by

josephsesay1997
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Classroom Exercise 1: Creating a Windows Forms Application with Visual Studio

Objective:
Learn to create a simple Windows Forms Application using Visual Studio. This program will demonstrate
the use of common controls like ListBox, CheckBox, Button, and TextBox.

Step 1: Setting up the project

1. Create a New Project

o Click on File > New > Project.

o Name the project ControlDemo and choose a save location.

o Click Create.

Step 2: Designing the Form

1. Add Controls to the Form


Open the Toolbox (usually on the left-hand side) and drag the following controls onto the form:

o ListBox

▪ Place it on the left side of the form.

▪ Name it listBoxItems.

o CheckBoxes

▪ Drag and drop three CheckBox controls onto the form, aligned vertically.

▪ Name them checkBoxOption1, checkBoxOption2, and checkBoxOption3.

▪ Set their Text properties to Option 1, Option 2, and Option 3.

o Button

▪ Place a button below the CheckBox controls.

▪ Name it buttonAddItems.

▪ Set its Text property to Add Items.

o TextBox

▪ Place a TextBox to the right of the ListBox.

▪ Name it textBoxInput.

▪ Set its PlaceholderText property (or Text to a default value like "Enter text").

o Label

▪ Add a label near the top of the form.

1
▪ Name it labelMessage.

▪ Set its Text property to an empty string.

Step 3: Writing the Code

1. Double-click the button (buttonAddItems) to create a click event handler.

2. Inside the buttonAddItems_Click method, add the following code:

csharp

Copy code

private void buttonAddItems_Click(object sender, EventArgs e)

// Clear the label message

labelMessage.Text = "";

// Add the text from the TextBox to the ListBox if not empty

string inputText = textBoxInput.Text.Trim();

if (!string.IsNullOrEmpty(inputText))

listBoxItems.Items.Add(inputText);

labelMessage.Text = "Item added to the list.";

else

labelMessage.Text = "Please enter a valid text.";

// Check which CheckBoxes are selected and add their text to the ListBox

if (checkBoxOption1.Checked)

listBoxItems.Items.Add(checkBoxOption1.Text);

2
if (checkBoxOption2.Checked)

listBoxItems.Items.Add(checkBoxOption2.Text);

if (checkBoxOption3.Checked)

listBoxItems.Items.Add(checkBoxOption3.Text);

// Clear the TextBox after adding

textBoxInput.Clear();

Step 4: Testing the Application

1. Run the Application

o Press F5 or click on the Start button in Visual Studio to build and run the application.

2. Test the Features

o Enter text in the TextBox and click the Add Items button. The entered text should appear
in the ListBox.

o Select one or more CheckBox options and click the button again. The selected options
should also appear in the ListBox.

o Observe the label for success or error messages.

Step 5: Enhancing the Exercise (Optional)

• Add a Clear Button: Include a button to clear all items in the ListBox.

• Enable Selection: Allow the user to remove selected items from the ListBox using another
button.

• Customize Appearance: Change properties like colors, fonts, and sizes for better aesthetics.

Outcome:
Students will create a functional Windows Forms application demonstrating the usage of ListBox,
CheckBox, Button, and TextBox, enhancing their understanding of basic controls and event handling in
Visual Studio.

3
Classroom Exercise 2: Windows Forms Application with ComboBox,
RadioButton, and ProgressBar
Objective:
Learn to create a Windows Forms Application using Visual Studio that incorporates the use of
ComboBox, RadioButton, and ProgressBar. The program will demonstrate how these controls can
interact dynamically.

Step 1: Setting up the Project

1. Open Visual Studio


Launch Visual Studio.

2. Create a New Project

o Click on File > New > Project.

o Name the project ControlExercise2 and choose a save location.

o Click Create.

Step 2: Designing the Form

1. Add Controls to the Form


Open the Toolbox and drag the following controls onto the form:

o ComboBox

▪ Place it near the top of the form.

▪ Name it comboBoxColors.

▪ Set its DropDownStyle property to DropDownList (so users can only select
predefined options).

o RadioButtons

▪ Add three RadioButton controls and arrange them vertically below the
ComboBox.

▪ Name them radioButtonSmall, radioButtonMedium, and radioButtonLarge.

▪ Set their Text properties to Small, Medium, and Large, respectively.

▪ Group them by placing them inside a GroupBox control for logical grouping
(optional).

o ProgressBar

▪ Drag and drop a ProgressBar onto the form, below the RadioButton group.

▪ Name it progressBarTask.

▪ Set its Minimum property to 0 and Maximum property to 100.

4
o Button

▪ Add a button below the ProgressBar.

▪ Name it buttonStart.

▪ Set its Text property to Start Task.

o Label

▪ Add a label above the ProgressBar.

▪ Name it labelStatus.

▪ Set its Text property to an empty string.

Step 3: Writing the Code

1. Initialize ComboBox Items


Double-click on the form to create a Form_Load event handler and populate the ComboBox:

csharp

Copy code

private void Form1_Load(object sender, EventArgs e)

// Populate ComboBox with color options

comboBoxColors.Items.Add("Red");

comboBoxColors.Items.Add("Green");

comboBoxColors.Items.Add("Blue");

2. Handle the Button Click Event


Double-click on the button to create a Click event handler. Add the following code:

csharp

Copy code

private void buttonStart_Click(object sender, EventArgs e)

// Update ProgressBar based on selected size

if (radioButtonSmall.Checked)

5
progressBarTask.Value = 25;

labelStatus.Text = "Task size: Small (25%)";

else if (radioButtonMedium.Checked)

progressBarTask.Value = 50;

labelStatus.Text = "Task size: Medium (50%)";

else if (radioButtonLarge.Checked)

progressBarTask.Value = 100;

labelStatus.Text = "Task size: Large (100%)";

else

labelStatus.Text = "Please select a task size.";

return;

// Change ProgressBar color based on ComboBox selection

if (comboBoxColors.SelectedItem != null)

string selectedColor = comboBoxColors.SelectedItem.ToString();

switch (selectedColor)

case "Red":

progressBarTask.ForeColor = System.Drawing.Color.Red;

break;

case "Green":

6
progressBarTask.ForeColor = System.Drawing.Color.Green;

break;

case "Blue":

progressBarTask.ForeColor = System.Drawing.Color.Blue;

break;

else

labelStatus.Text = "Please select a color.";

Step 4: Testing the Application

1. Run the Application

o Press F5 or click the Start button.

2. Test the Features

o Select a color from the ComboBox.

o Choose a task size by selecting a RadioButton.

o Click the Start Task button to see the ProgressBar update dynamically based on your
selections.

o Observe the label for feedback messages.

Step 5: Enhancing the Exercise (Optional)

• Add a Reset Button: Include a button to reset the ProgressBar and selections.

• Dynamic ComboBox Items: Allow users to add new colors to the ComboBox using a TextBox and
Button.

• Advanced ProgressBar Behavior: Implement a timer to animate the ProgressBar filling gradually.

Outcome:
Students will gain experience with ComboBox, RadioButton, and ProgressBar controls, along with event
handling and conditional logic in Visual Studio. This exercise also introduces dynamic UI updates based
on user interaction.

You might also like