Sure!
Below, I have broken down the Windows Forms Application code into smaller
sections and explained each part separately. This will help you understand it more easily.
1️⃣ Setting Up the Form
This is the constructor of the form, where the form is initialized.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// Form Properties
this.Text = "Windows Forms Controls Example";
this.Size = new Size(800, 600);
}
}
Explanation:
● this.Text → Sets the form's title.
● this.Size → Defines the window size (Width = 800, Height = 600).
● Form1_Load → Executes when the form loads.
2️⃣ Creating a TabControl
A TabControl is added to allow multiple sections in the form.
// Create TabControl
TabControl tabControl = new TabControl
{
Size = new Size(780, 550)
};
this.Controls.Add(tabControl);
Explanation:
● A TabControl is created.
● Size = new Size(780, 550); → Sets the tab control's size.
● this.Controls.Add(tabControl); → Adds the tab control to the form.
3️⃣ Adding Tabs to the TabControl
We create three different tabs for different controls.
// Create Tabs
TabPage tabPage1 = new TabPage("Basic Controls");
TabPage tabPage2 = new TabPage("List & Tree View");
TabPage tabPage3 = new TabPage("PictureBox");
// Add Tabs to TabControl
tabControl.TabPages.Add(tabPage1);
tabControl.TabPages.Add(tabPage2);
tabControl.TabPages.Add(tabPage3);
Explanation:
● TabPage tabPage1 = new TabPage("Basic Controls"); → Creates a tab
with the title "Basic Controls".
● We add these tabs to the TabControl.
4️⃣ Adding Controls to Tab 1 (Basic Controls)
Now, we add controls like Label, TextBox, RichTextBox, RadioButtons, CheckBox, and
Button.
Adding Label & TextBox
Label label = new Label
{
Text = "Enter your Name:",
Location = new Point(20, 20)
};
tabPage1.Controls.Add(label);
TextBox textBox = new TextBox
{
Location = new Point(150, 20)
};
tabPage1.Controls.Add(textBox);
Adding RichTextBox
RichTextBox richTextBox = new RichTextBox
{
Location = new Point(150, 50),
Size = new Size(200, 50),
Text = "This is a RichTextBox."
};
tabPage1.Controls.Add(richTextBox);
Adding Radio Buttons
RadioButton radioButton1 = new RadioButton
{
Text = "Male",
Location = new Point(150, 110)
};
tabPage1.Controls.Add(radioButton1);
RadioButton radioButton2 = new RadioButton
{
Text = "Female",
Location = new Point(220, 110)
};
tabPage1.Controls.Add(radioButton2);
Adding CheckBox
CheckBox checkBox = new CheckBox
{
Text = "Subscribe to Newsletter",
Location = new Point(150, 140)
};
tabPage1.Controls.Add(checkBox);
Adding Button with Click Event
Button button = new Button
{
Text = "Submit",
Location = new Point(150, 170)
};
button.Click += (s, ev) =>
MessageBox.Show("Name: " + textBox.Text + "\nSubscribed: " + checkBox.Checked);
tabPage1.Controls.Add(button);
5️⃣ Adding Controls to Tab 2 (List & Tree View)
Now, we add ListBox, CheckedListBox, and TreeView.
Adding ListBox
ListBox listBox = new ListBox
{
Location = new Point(20, 20),
Size = new Size(200, 80)
};
listBox.Items.Add("Item 1");
listBox.Items.Add("Item 2");
listBox.Items.Add("Item 3");
tabPage2.Controls.Add(listBox);
Adding CheckedListBox
CheckedListBox checkedListBox = new CheckedListBox
{
Location = new Point(250, 20),
Size = new Size(200, 80)
};
checkedListBox.Items.Add("Option A");
checkedListBox.Items.Add("Option B");
checkedListBox.Items.Add("Option C");
tabPage2.Controls.Add(checkedListBox);
Adding TreeView
TreeView treeView = new TreeView
{
Location = new Point(20, 120),
Size = new Size(200, 100)
};
TreeNode rootNode = new TreeNode("Root");
rootNode.Nodes.Add("Child 1");
rootNode.Nodes.Add("Child 2");
treeView.Nodes.Add(rootNode);
tabPage2.Controls.Add(treeView);
6️⃣ Adding Controls to Tab 3 (PictureBox)
PictureBox pictureBox = new PictureBox
{
Location = new Point(20, 20),
Size = new Size(300, 200),
BorderStyle = BorderStyle.FixedSingle,
SizeMode = PictureBoxSizeMode.StretchImage
};
// Try loading an image from file
try
{
pictureBox.Image = Image.FromFile("imagepath.jpg");
}
catch (Exception)
{
MessageBox.Show("Image not found! Please check the path.");
}
tabPage3.Controls.Add(pictureBox);
Explanation:
● BorderStyle.FixedSingle; → Adds a border around the picture.
● SizeMode = PictureBoxSizeMode.StretchImage; → Ensures the image fits
the box.
● Try-Catch Block: If the image is not found, it shows an error message.
🎯 Summary of Code Sections
Section Description
1️⃣ Setting Up the Form Defines form properties (size, title).
2️⃣ Creating TabControl Creates and adds TabControl.
3️⃣ Adding Tabs Creates TabPage for different sections.
4️⃣ Basic Controls (Tab 1) Adds Label, TextBox, RichTextBox, RadioButton,
CheckBox, Button.
5️⃣ List & Tree View (Tab 2) Adds ListBox, CheckedListBox, TreeView.
6️⃣ PictureBox (Tab 3) Adds a PictureBox and loads an image.
🛠 Key Takeaways
✅ Organized into tabs to keep controls separate.
✅ Handles user input with TextBox, CheckBox, RadioButton.
✅ Interactivity: Button click event displays user input.
✅ List & Tree Controls allow structured data display.
✅ Image Handling using PictureBox with error handling.
Now, your code is well-structured and easy to understand! 🚀 Let me know if you need
further explanations or improvements. 😊