0% found this document useful (0 votes)
4 views4 pages

EventProg Reviewer

The document covers Event Driven Programming in C#, focusing on using timers and asynchronous programming for handling time-based actions and improving application responsiveness. It also discusses exception handling, detailing the try, catch, and finally blocks for managing runtime errors effectively. Additionally, it includes code examples demonstrating the implementation of these concepts in a user interface context.

Uploaded by

jabez john betet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

EventProg Reviewer

The document covers Event Driven Programming in C#, focusing on using timers and asynchronous programming for handling time-based actions and improving application responsiveness. It also discusses exception handling, detailing the try, catch, and finally blocks for managing runtime errors effectively. Additionally, it includes code examples demonstrating the implementation of these concepts in a user interface context.

Uploaded by

jabez john betet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Midterm: Event Driven Programming Reviewer M5 - M6

Module 5 Module 6: Exception Handling


such as those with user interfaces or
In C#, Timer and async events can work network operations.
together to handle time-based actions
asynchronously. Combining Timer and Async: You can
integrate timers with async event
Timer for Events The handling for advanced workflows. For
System.Timers.Timer class is instance, you can trigger events at
commonly used for creating time- regular intervals and handle them
based events. asynchronously to ensure efficient, non-
blocking execution.
Async with Tasks
In modern applications, you might prefer
asynchronous programming for better Combo box
responsiveness. - Drop down list allow users to
select one item from a list (save
Combining Timer and Async You can space)
also use async and await alongside (has drop down box)
Timer for more complex scenarios.
List box
Time-Based Actions in C# In C#, you - A UI that displays list items, users
can handle time-based events using the can at least one of multiple items
System.Timers.Timer class and
asynchronous programming with async Data Grid
and await. Here's an overview: - Display, edit, manipulate, and
tabular data
Timer: The Timer class allows you to
execute actions at regular intervals. You Asynchronous
can set the timer's interval and attach - Refer to a way of handling task
event handlers that respond whenever that allow your code to perform
the timer elapses. This is ideal for other operations without waiting
repetitive tasks like logging, polling data, for a task to finish (multi-tasking)
or updating the UI.
Event
Async Events: With asynchronous
- Features allow responding to
programming (async and await), you can
something that happens in your
perform time-based actions without
applications like when a user
blocking the program's execution. This is
clicks a button or moves the
useful for applications that require
mouse. Think of it as setting up a
responsiveness,
trigger for a specific
action
Exception handling is a structured way to manage runtime errors in a controlled manner
so that your application can recover or exit gracefully. It separates error-handling code
from regular logic using specific keywords and blocks.

try Block - Encapsulates code that might


throw an exception. Any operation that
could potentially fail (such as file I/O,
network calls, or mathematical operations)
is placed inside the try block.

catch Block - Handles exceptions thrown by


the try block. You can have multiple catch
blocks to capture different exception types,
providing specific handling logic. For instance,
you may catch a
DivideByZeroException or a
FormatException to display clear error
messages.

finally Block - Contains code that always


executes regardless of whether
an exception was thrown or caught. It is typically
used for cleanup activities (closing files, releasing
resources, etc.)
Code:

private void UpdateBtn_Click(object sender, EventArgs e)


{

DataGridViewRow updateData = dataGridView1.Rows[index];


updateData.Cells[0].Value = IDtb.Text;
updateData.Cells[1].Value = FirstTB.Text;
updateData.Cells[2].Value = LastNTB.Text;
updateData.Cells[3].Value = AgeTB.Text;

private void DeleteBtn_Click(object sender, EventArgs e)


{
index = dataGridView1.CurrentRow.Index;
index = dataGridView1.CurrentCell.RowIndex;
dataGridView1.Rows.RemoveAt(index);
}

private void SortBtn_Click(object sender, EventArgs e)


{
NameComboBox.Sorted = true;
}

private void DataGridBtn_Click(object sender, EventArgs e)


{
DataGrid dataGrid = new DataGrid();
dataGrid.Show();
}

protected override void Dispose(bool disposing)


{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}

try
{
Console.Write("Enter a number: ");
int divisor = int.Parse(Console.ReadLine());
Console.WriteLine("Result: " + (100 / divisor));
}
catch (DivideByZeroException)
{
Console.WriteLine("Error: Cannot divide by zero.");
}
catch (FormatException)
{
Console.WriteLine("Error: Please enter a valid
number.");
}
finally
{
Console.WriteLine("Execution completed.");
}

private void dataGridView1_CellClick(object sender,


DataGridViewCellEventArgs e)
{
index = e.RowIndex;
DataGridViewRow row = dataGridView1.Rows[index];
IDtb.Text = row.Cells[0].Value.ToString();
FirstTB.Text = row.Cells[1].Value.ToString();
LastNTB.Text = row.Cells[2].Value.ToString();
AgeTB.Text = row.Cells[3].Value.ToString();

You might also like