CODE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
// Note: Requires reference to Microsoft.VisualBasic for InputBox
using Microsoft.VisualBasic;
namespace Definitions
{
public partial class Form71 : Form
{
// List to hold the entered numbers
List<double> numbersList = new List<double>();
public Form71()
{
InitializeComponent();
}
private void btnStart_Click(object sender, EventArgs e)
{
numbersList.Clear();
double num = -1;
// Keep prompting until 0 is entered
while (num != 0)
{
string input = Interaction.InputBox("Enter a number (0 to stop):", "Input Box", "0");
if (double.TryParse(input, out num))
{
if (num != 0)
{
numbersList.Add(num);
}
}
else
{
MessageBox.Show("Invalid input. Please enter a number.");
}
}
MessageBox.Show("Reading complete. " + numbersList.Count + " numbers entered.");
}
private void btnCalculate_Click(object sender, EventArgs e)
{
if (numbersList.Count == 0)
{
MessageBox.Show("No numbers to calculate!");
return;
}
double sum = numbersList.Sum();
double avg = numbersList.Average();
string result = "Results:\n";
if (chkSum.Checked)
{
result += "Sum = " + sum + "\n";
}
if (chkAverage.Checked)
{
result += "Average = " + avg + "\n";
}
if (!chkSum.Checked && !chkAverage.Checked)
{
result = "Please select Sum and/or Average.";
}
MessageBox.Show(result, "Calculation Result");
}
private void btnExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
OUTPUT
Number Calculator
×
Calculation Result
×
Results:
Sum = 150
Average = 30
Sum = 150
Average = 30
CODE
namespace Definitions
{
public partial class Form72 : Form
{
public Form72()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string city = txtCity.Text;
string age = txtAge.Text;
// Ensure no fields are left blank
if (string.IsNullOrWhiteSpace(name) || string.IsNullOrWhiteSpace(city) || string.IsNullOrWhiteSpace(age))
{
MessageBox.Show("Please fill out all fields.");
return;
}
string welcomeMessage = $"Welcome {name}!\n" +
$"It is great to see someone from {city} here.\n" +
$"You are {age} years old.";
MessageBox.Show(welcomeMessage, "Welcome", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
OUTPUT
User Details Form
×
Name:
Manthan
City:
Rajkot
Age:
20
Welcome
×
ℹ️
Welcome Manthan!
It is great to see someone from Rajkot here.
You are 20 years old.
It is great to see someone from Rajkot here.
You are 20 years old.
CODE
namespace Definitions
{
public partial class Form73 : Form
{
public Form73()
{
InitializeComponent();
}
private void btnAddItem_Click(object sender, EventArgs e)
{
string newItem = txtItem.Text.Trim();
if (!string.IsNullOrEmpty(newItem))
{
// Check if item already exists to avoid duplicates
if (!checkedListBox1.Items.Contains(newItem))
{
checkedListBox1.Items.Add(newItem);
txtItem.Clear();
txtItem.Focus();
}
else
{
MessageBox.Show("Item already exists in the list.");
}
}
else
{
MessageBox.Show("Please enter an item to add.");
}
}
}
}
OUTPUT
Add to CheckedList
×
New Task...|
CODE
namespace Definitions
{
public partial class Form74 : Form
{
public Form74()
{
InitializeComponent();
// Set trackbar ranges 0-255 in constructor or designer
trackBarRed.Maximum = 255;
trackBarGreen.Maximum = 255;
trackBarBlue.Maximum = 255;
}
// Assign this same event handler to the 'Scroll' event of ALL 3 TrackBars
private void trackBar_Scroll(object sender, EventArgs e)
{
int r = trackBarRed.Value;
int g = trackBarGreen.Value;
int b = trackBarBlue.Value;
// Change Label background color dynamically
lblColorPreview.BackColor = Color.FromArgb(r, g, b);
// Update text to show current RGB values
lblColorPreview.Text = $"RGB ({r}, {g}, {b})";
}
}
}
OUTPUT
RGB Color Mixer
×
RGB (150, 80, 200)
R
G
B
CODE
namespace Definitions
{
public partial class Form75 : Form
{
public Form75()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtName.Text))
{
listBox1.Items.Add(txtName.Text);
txtName.Clear();
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
}
else
{
MessageBox.Show("Select a name to delete.");
}
}
private void btnFont_Click(object sender, EventArgs e)
{
if (fontDialog1.ShowDialog() == DialogResult.OK)
{
// Apply font to listbox items
listBox1.Font = fontDialog1.Font;
}
}
private void btnColor_Click(object sender, EventArgs e)
{
if (colorDialog1.ShowDialog() == DialogResult.OK)
{
// Apply background color to listbox
listBox1.BackColor = colorDialog1.Color;
}
}
}
}
OUTPUT
Advanced ListBox Control
×
Name...
Alice
Bob
Charlie
CODE
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Definitions
{
public partial class Form76 : Form
{
// Connection string (Update with actual Server/DB details)
string connString = "Data Source=SERVER_NAME;Initial Catalog=MyDatabase;Integrated Security=True";
public Form76()
{
InitializeComponent();
}
private void btnLoadData_Click(object sender, EventArgs e)
{
// Use DataTable to structure data for the DataGridView
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(connString))
{
string query = "SELECT Id, Name, Department FROM Employees";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
try
{
conn.Open();
// ExecuteReader retrieves data stream from DB
using (SqlDataReader reader = cmd.ExecuteReader())
{
// Load data stream directly into DataTable
dt.Load(reader);
}
// Bind to DataGridView
dataGridView1.DataSource = dt;
}
catch (Exception ex)
{
MessageBox.Show("Error loading data: " + ex.Message);
}
}
}
}
}
}
OUTPUT
DataReader to DataGridView
×
CODE
using System;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Definitions
{
public partial class CRUDForm : Form
{
string connStr = "Data Source=SERVER;Initial Catalog=DB;Integrated Security=True";
public CRUDForm()
{
InitializeComponent();
}
private void btnAdd_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
string query = "INSERT INTO Customers (Id, Name, Email) VALUES (@Id, @Name, @Email)";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Id", txtId.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
conn.Open();
cmd.ExecuteNonQuery(); // Used for Insert, Update, Delete
MessageBox.Show("Record Added!");
}
}
}
private void btnUpdate_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
string query = "UPDATE Customers SET Name=@Name, Email=@Email WHERE Id=@Id";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Id", txtId.Text);
cmd.Parameters.AddWithValue("@Name", txtName.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Updated!");
}
}
}
private void btnDelete_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
string query = "DELETE FROM Customers WHERE Id=@Id";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
cmd.Parameters.AddWithValue("@Id", txtId.Text);
conn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Deleted!");
}
}
}
}
}
OUTPUT
Customer Management (CRUD)
×
Cust ID:
105
Name:
Sneha Joshi
Email:
sneha@email.com
CODE
using System;
using System.Data;
using System.Windows.Forms;
namespace Definitions
{
public partial class Form79 : Form
{
DataTable dt = new DataTable();
int currentIndex = 0;
public Form79()
{
InitializeComponent();
}
private void Form79_Load(object sender, EventArgs e)
{
// Simulating Database Load
dt.Columns.Add("Id");
dt.Columns.Add("Name");
dt.Rows.Add("1", "First Record");
dt.Rows.Add("2", "Middle Record");
dt.Rows.Add("3", "Last Record");
ShowRecord(currentIndex);
}
private void ShowRecord(int index)
{
if (dt.Rows.Count > 0 && index >= 0 && index < dt.Rows.Count)
{
txtId.Text = dt.Rows[index]["Id"].ToString();
txtName.Text = dt.Rows[index]["Name"].ToString();
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
currentIndex = 0;
ShowRecord(currentIndex);
}
private void btnPrev_Click(object sender, EventArgs e)
{
if (currentIndex > 0)
{
currentIndex--;
ShowRecord(currentIndex);
}
}
private void btnNext_Click(object sender, EventArgs e)
{
if (currentIndex < dt.Rows.Count - 1)
{
currentIndex++;
ShowRecord(currentIndex);
}
}
private void btnLast_Click(object sender, EventArgs e)
{
currentIndex = dt.Rows.Count - 1;
ShowRecord(currentIndex);
}
}
}
OUTPUT
Record Navigator
×
ID:
2
Name:
Middle Record
CODE
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Definitions
{
public partial class Form80 : Form
{
string connStr = "Data Source=SERVER;Initial Catalog=DB;Integrated Security=True";
SqlDataAdapter adapter;
DataSet dataSet;
public Form80()
{
InitializeComponent();
}
private void btnLoad_Click(object sender, EventArgs e)
{
string query = "SELECT * FROM Employees";
adapter = new SqlDataAdapter(query, connStr);
dataSet = new DataSet();
// Fills the DataSet (Memory representation of data) and closes connection
adapter.Fill(dataSet, "Employees");
// Bind disconnected data to grid
dataGridView1.DataSource = dataSet.Tables["Employees"];
}
private void btnUpdateDB_Click(object sender, EventArgs e)
{
try
{
// Automatically generates Insert/Update/Delete commands
// based on the SELECT query provided to the adapter
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
// Pushes changes made in the grid/dataset back to the actual database
adapter.Update(dataSet, "Employees");
MessageBox.Show("Database updated successfully from DataSet!");
}
catch (Exception ex)
{
MessageBox.Show("Update failed: " + ex.Message);
}
}
}
}
OUTPUT
Disconnected Data Architecture
×
No comments:
Post a Comment