CODE
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Definitions
{
public partial class Form81 : Form
{
string connStr = "Data Source=SERVER;Initial Catalog=DB;Integrated Security=True";
public Form81()
{
InitializeComponent();
}
private void Form81_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
// Create DataAdapters for both Master (Departments) and Detail (Employees) tables
SqlDataAdapter daMaster = new SqlDataAdapter("SELECT * FROM Departments", conn);
SqlDataAdapter daDetail = new SqlDataAdapter("SELECT * FROM Employees", conn);
DataSet ds = new DataSet();
// Fill the DataSet with both tables
daMaster.Fill(ds, "Departments");
daDetail.Fill(ds, "Employees");
// Create the DataRelation linking the Primary Key to the Foreign Key
DataColumn masterColumn = ds.Tables["Departments"].Columns["DeptId"];
DataColumn detailColumn = ds.Tables["Employees"].Columns["DeptId"];
DataRelation relation = new DataRelation("DeptEmpRelation", masterColumn, detailColumn);
ds.Relations.Add(relation);
// Bind Master DataGridView to the Departments table
dgvMaster.DataSource = ds;
dgvMaster.DataMember = "Departments";
// Bind Detail DataGridView to the RELATIONSHIP itself to auto-filter child records
dgvDetail.DataSource = ds;
dgvDetail.DataMember = "Departments.DeptEmpRelation";
}
}
}
}
OUTPUT
Master-Detail View
×
Master: Departments
| DeptId | DeptName |
|---|---|
| 10 | IT |
| 20 | HR |
Detail: Employees in IT (Dept 10)
| EmpId | EmpName | DeptId |
|---|---|---|
| 101 | Alex | 10 |
| 103 | Sara | 10 |
CODE
namespace Definitions
{
public partial class Form82 : Form
{
public Form82()
{
InitializeComponent();
}
// CREATE: Add item to ListBox
private void btnAdd_Click(object sender, EventArgs e)
{
if (!string.IsNullOrWhiteSpace(txtData.Text))
{
listBox1.Items.Add(txtData.Text);
txtData.Clear();
txtData.Focus();
}
}
// READ: Populate textbox when an item is selected from ListBox
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
txtData.Text = listBox1.SelectedItem.ToString();
}
}
// UPDATE: Replace the currently selected item
private void btnUpdate_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1 && !string.IsNullOrWhiteSpace(txtData.Text))
{
listBox1.Items[listBox1.SelectedIndex] = txtData.Text;
txtData.Clear();
}
else
{
MessageBox.Show("Please select an item to update.");
}
}
// DELETE: Remove the selected item
private void btnDelete_Click(object sender, EventArgs e)
{
if (listBox1.SelectedIndex != -1)
{
listBox1.Items.RemoveAt(listBox1.SelectedIndex);
txtData.Clear();
}
else
{
MessageBox.Show("Please select an item to delete.");
}
}
}
}
OUTPUT
ListBox CRUD
×
Data:
Updated Item Text|
Item One
Updated Item Text
Item Three
CODE
// --- CODE FOR FORM 1 (Sender) ---
namespace Definitions
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btnSendData_Click(object sender, EventArgs e)
{
// Read data from textbox on Form1
string dataToPass = textBox1.Text;
// Create instance of Form2 and pass data via its constructor
Form2 form2 = new Form2(dataToPass);
form2.Show();
}
}
}
// --- CODE FOR FORM 2 (Receiver) ---
namespace Definitions
{
public partial class Form2 : Form
{
// Constructor updated to receive a string parameter
public Form2(string receivedData)
{
InitializeComponent();
// Assign the received data to a Label on Form2
lblReceivedData.Text = "Received: " + receivedData;
}
}
}
OUTPUT
Form 1 (Sender)
×
Hello from Form 1!
→
Form 2 (Receiver)
×
Received: Hello from Form 1!
CODE
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace Definitions
{
// A reusable custom control consisting of a Label and a Button
public partial class MyCustomControl : UserControl
{
// 1. Custom Event definition
public event EventHandler CustomButtonClicked;
public MyCustomControl()
{
InitializeComponent();
}
// 2. Custom Property definition (Shows up in Visual Studio Properties Window)
[Category("Custom Properties")]
[Description("Sets the title text of the control.")]
public string TitleText
{
get { return lblTitle.Text; }
set { lblTitle.Text = value; }
}
// Internal button click triggers the public event
private void btnAction_Click(object sender, EventArgs e)
{
// Trigger the event if anyone is subscribed to it
CustomButtonClicked?.Invoke(this, EventArgs.Empty);
}
}
}
OUTPUT
UserControl Designer View
[TitleText Property]
CODE
namespace Definitions
{
public partial class Form85 : Form
{
public Form85()
{
InitializeComponent();
}
private void Form85_Load(object sender, EventArgs e)
{
// 1. Instantiate the custom UserControl programmatically
MyCustomControl customUC = new MyCustomControl();
// 2. Set its custom property
customUC.TitleText = "Dynamic Dashboard Widget";
// 3. Set location on the form
customUC.Location = new System.Drawing.Point(20, 20);
// 4. Subscribe to its custom event
customUC.CustomButtonClicked += CustomUC_CustomButtonClicked;
// 5. Add it to the Form's Controls collection so it becomes visible
this.Controls.Add(customUC);
}
// Event handler that runs when the UserControl's button is clicked
private void CustomUC_CustomButtonClicked(object sender, EventArgs e)
{
MessageBox.Show("The custom event was caught by the main form!");
}
}
}
OUTPUT
Main Window
×
Dynamic Dashboard Widget
CODE
namespace Definitions
{
public partial class MDIParent86 : Form
{
public MDIParent86()
{
InitializeComponent();
this.IsMdiContainer = true;
}
// Triggers when File -> Open File is clicked from the MenuStrip
private void openFileToolStripMenuItem_Click(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
openDialog.Filter = "Text Files|*.txt|All Files|*.*";
openDialog.Title = "Open a File";
if (openDialog.ShowDialog() == DialogResult.OK)
{
// Create a new child form to display the selection
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "File: " + openDialog.SafeFileName;
Label lbl = new Label() { Text = "Opened: " + openDialog.FileName, AutoSize = true, Top = 20, Left = 20 };
childForm.Controls.Add(lbl);
childForm.Show();
}
}
// Triggers when File -> Select Color is clicked
private void selectColorToolStripMenuItem_Click(object sender, EventArgs e)
{
ColorDialog colorDialog = new ColorDialog();
if (colorDialog.ShowDialog() == DialogResult.OK)
{
// Changes the MDI background color
foreach (Control c in this.Controls)
{
if (c is MdiClient)
{
c.BackColor = colorDialog.Color;
}
}
}
}
}
}
OUTPUT
CODE
using System;
using System.Windows.Forms;
// Requires CrystalDecisions references
using CrystalDecisions.CrystalReports.Engine;
namespace Definitions
{
public partial class Form87 : Form
{
public Form87()
{
InitializeComponent();
}
private void Form87_Load(object sender, EventArgs e)
{
try
{
// Instantiate the ReportDocument
ReportDocument cryRpt = new ReportDocument();
// Load the physical .rpt file created in the Crystal Reports designer
// Assume EmployeeReport.rpt is in the project output directory
string reportPath = Application.StartupPath + "\\EmployeeReport.rpt";
cryRpt.Load(reportPath);
// (Optional) If report requires database login credentials:
// cryRpt.SetDatabaseLogon("username", "password", "server", "database");
// Assign the report to the viewer control placed on the form
crystalReportViewer1.ReportSource = cryRpt;
crystalReportViewer1.Refresh();
}
catch (Exception ex)
{
MessageBox.Show("Error loading report: " + ex.Message);
}
}
}
}
OUTPUT
Report Viewer Form
×
🖨️ 📤 ⏮️ ◀️ 1/1 ▶️ ⏭️ 🔍 100% ▼
Employee Data Report
| Emp ID | Full Name | Department |
| 101 | Alex Johnson | IT |
| 102 | Sara Smith | HR |
| 103 | David Lee | Finance |
Generated on: 16-Mar-2026
CODE
namespace Definitions
{
public partial class Form88 : Form
{
public Form88()
{
InitializeComponent();
}
private void btnChangeBg_Click(object sender, EventArgs e)
{
// Instantiate the built-in ColorDialog
ColorDialog colorDialog = new ColorDialog();
// Allow user to define custom colors in the dialog
colorDialog.AllowFullOpen = true;
// Show the dialog and check if the user clicked "OK"
if (colorDialog.ShowDialog() == DialogResult.OK)
{
// Apply the selected color to the current form's background
this.BackColor = colorDialog.Color;
}
}
}
}
OUTPUT
Theme Changer
×
CODE
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
namespace Definitions
{
public partial class Form89 : Form
{
string connStr = "Data Source=SERVER;Initial Catalog=DB;Integrated Security=True";
public Form89()
{
InitializeComponent();
}
private void btnSearch_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection(connStr))
{
// SQL query targeting the specific fields and the condition (salary < 3000)
string query = "SELECT empno, ename, salary FROM Emp WHERE salary < 3000";
using (SqlCommand cmd = new SqlCommand(query, conn))
{
try
{
conn.Open();
SqlDataReader reader = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(reader);
// Bind results to grid
dataGridView1.DataSource = dt;
if(dt.Rows.Count == 0)
{
MessageBox.Show("No employees found with salary less than 3000.");
}
}
catch (Exception ex)
{
MessageBox.Show("Database Error: " + ex.Message);
}
}
}
}
}
}
OUTPUT
Employee Search
×
Filter: Salary < 3000
No comments:
Post a Comment