CODE
namespace Definitions
{
public partial class MDIParentForm : Form
{
public MDIParentForm()
{
InitializeComponent();
// This property makes this form a parent container
this.IsMdiContainer = true;
}
private void newWindowToolStripMenuItem_Click(object sender, EventArgs e)
{
// Creates and displays a new child form inside the parent
Form childForm = new Form();
childForm.MdiParent = this;
childForm.Text = "New Child Document";
childForm.Show();
}
private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
{
// Loops through and closes all open child forms
foreach (Form child in this.MdiChildren)
{
child.Close();
}
}
}
}
OUTPUT
CODE
namespace Definitions
{
public partial class Form62 : Form
{
public Form62()
{
InitializeComponent();
}
private void Form62_Load(object sender, EventArgs e)
{
// Adding parent node
TreeNode rootNode = new TreeNode("C:\\");
treeView1.Nodes.Add(rootNode);
// Adding child nodes
TreeNode windowsNode = new TreeNode("Windows");
TreeNode usersNode = new TreeNode("Users");
TreeNode programFilesNode = new TreeNode("Program Files");
rootNode.Nodes.Add(windowsNode);
rootNode.Nodes.Add(usersNode);
rootNode.Nodes.Add(programFilesNode);
// Adding sub-child node
usersNode.Nodes.Add(new TreeNode("User"));
// Expand the entire tree automatically
treeView1.ExpandAll();
}
}
}
OUTPUT
TreeView Example
×
- 🖴 C:\
+ 📁 Windows
- 📁 Users
👤 User
+ 📁 Program Files
CODE
using System.Data;
namespace Definitions
{
public partial class Form63 : Form
{
public Form63()
{
InitializeComponent();
}
private void Form63_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("ID", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Course", typeof(string));
// Adding rows of data to the table
table.Rows.Add(1, "John", "CS");
table.Rows.Add(2, "Rahul", "BBA");
table.Rows.Add(3, "Priya", "B.Com");
// Bind the table to the DataGridView
dataGridView1.DataSource = table;
}
}
}
OUTPUT
DataGridView Form
×
| ID | Name | Course |
|---|---|---|
| 1 | John | CS |
| 2 | Rahul | BBA |
| 3 | Priya | B.Com |
CODE
namespace Definitions
{
public partial class Form64 : Form
{
public Form64()
{
InitializeComponent();
}
private void btnSubmit_Click(object sender, EventArgs e)
{
// Checks if the textbox is empty or only contains whitespace
if (string.IsNullOrWhiteSpace(txtName.Text))
{
MessageBox.Show("Name cannot be empty!", "Validation Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
// Focus returns the cursor to the textbox so the user can fix the error
txtName.Focus();
}
else
{
MessageBox.Show("Form submitted successfully!");
}
}
}
}
OUTPUT
Validation Form
×
Enter Name:
!
* Name is required
CODE
namespace Definitions
{
public partial class LoginForm : Form
{
public LoginForm()
{
InitializeComponent();
// Masks the password characters with an asterisk
txtPassword.PasswordChar = '*';
}
private void btnSubmit_Click(object sender, EventArgs e)
{
string username = txtUsername.Text;
string password = txtPassword.Text;
// Simple validation check for demonstration
if (username == "admin" && password == "admin123")
{
MessageBox.Show("Login Successful!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("Invalid Username or Password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
OUTPUT
Login
×
Username:
admin
Password:
********
Success
×
ℹ️
Login Successful!
CODE
namespace Definitions
{
public partial class Form66 : Form
{
public Form66()
{
InitializeComponent();
}
private void Form66_Load(object sender, EventArgs e)
{
// The ToolTip component must be added from the toolbox first
toolTip1.SetToolTip(btnSave, "Click here to save your work.");
toolTip1.SetToolTip(txtEmail, "Enter a valid email address.");
}
}
}
OUTPUT
Tooltip Example
×
Click here to save your work.
🖱️
CODE
namespace Definitions
{
public partial class Form67 : Form
{
public Form67()
{
InitializeComponent();
}
private void checkedListBox1_ItemCheck(object sender, ItemCheckEventArgs e)
{
// Get the text of the item currently being checked or unchecked
string selectedItem = checkedListBox1.Items[e.Index].ToString();
// If the user checks the box, add it to the normal ListBox
if (e.NewValue == CheckState.Checked)
{
if (!listBox1.Items.Contains(selectedItem))
{
listBox1.Items.Add(selectedItem);
}
}
// If the user unchecks the box, remove it from the normal ListBox
else if (e.NewValue == CheckState.Unchecked)
{
if (listBox1.Items.Contains(selectedItem))
{
listBox1.Items.Remove(selectedItem);
}
}
}
}
}
OUTPUT
CheckedListBox Sync
×
Available Courses
Selected Courses
C# Programming
Web Design
CODE
namespace Definitions
{
public partial class Form68 : Form
{
public Form68()
{
InitializeComponent();
// Start with the button disabled
btnNext.Enabled = false;
}
private void chkAgree_CheckedChanged(object sender, EventArgs e)
{
// The button's enabled state will directly match whether the box is checked
btnNext.Enabled = chkAgree.Checked;
}
private void btnNext_Click(object sender, EventArgs e)
{
MessageBox.Show("Proceeding to next step...");
}
}
}
OUTPUT
Terms and Conditions
×
CODE
namespace Definitions
{
public partial class Form69 : Form
{
public Form69()
{
InitializeComponent();
}
// Moves a single selected item from ListBox1 to ListBox2
private void btnMoveRight_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
listBox2.Items.Add(listBox1.SelectedItem);
listBox1.Items.Remove(listBox1.SelectedItem);
}
}
// Moves ALL items from ListBox1 to ListBox2
private void btnMoveAllRight_Click(object sender, EventArgs e)
{
foreach (var item in listBox1.Items)
{
listBox2.Items.Add(item);
}
listBox1.Items.Clear();
}
// Moves a single selected item from ListBox2 back to ListBox1
private void btnMoveLeft_Click(object sender, EventArgs e)
{
if (listBox2.SelectedItem != null)
{
listBox1.Items.Add(listBox2.SelectedItem);
listBox2.Items.Remove(listBox2.SelectedItem);
}
}
// Moves ALL items from ListBox2 back to ListBox1
private void btnMoveAllLeft_Click(object sender, EventArgs e)
{
foreach (var item in listBox2.Items)
{
listBox1.Items.Add(item);
}
listBox2.Items.Clear();
}
}
}
OUTPUT
Item Mover
×
Item A
Item D
Item B
Item C
CODE
namespace Definitions
{
public partial class Form70 : Form
{
public Form70()
{
InitializeComponent();
}
private void btnRegister_Click(object sender, EventArgs e)
{
string name = txtName.Text;
string course = txtCourse.Text;
// Format the string exactly how we want it to look in the box
string output = "--- Registration Summary ---\n" +
"Student Name: " + name + "\n" +
"Course: " + course + "\n" +
"Status: Registered Successfully\n\n";
// Append allows us to add multiple students without erasing previous ones
richTextBox1.AppendText(output);
// Clear inputs so it is ready for the next student
txtName.Clear();
txtCourse.Clear();
txtName.Focus();
}
}
}
OUTPUT
Student Registration
×
Name:
Course:
--- Registration Summary ---
Student Name: John Doe
Course: CS
Status: Registered Successfully
Student Name: John Doe
Course: CS
Status: Registered Successfully
No comments:
Post a Comment