.Net
.Net
2. Label
• Description: Displays text, typically as a caption for
other controls.
• Key Properties:
o Text: The text displayed in the label.
o Font: Defines the font style and size.
• Key Events:
o Click: Triggered when the label is clicked.
• Example:
Label label1 = new Label();
label1.Text = "Enter your name:";
label1.Location = new Point(20, 20);
this.Controls.Add(label1);
3. TextBox
• Description: Allows text input or displays text.
• Key Properties:
o Text: Gets or sets the content.
o Multiline: Enables multi-line input.
o ScrollBars: Adds scrollbars (Both, Horizontal,
Vertical).
o ReadOnly: Prevents editing.
• Key Events:
o TextChanged: Triggered when text changes.
• Example:
TextBox textBox1 = new TextBox();
textBox1.Multiline = true;
textBox1.ScrollBars = ScrollBars.Vertical;
textBox1.Location = new Point(20, 50);
textBox1.Size = new Size(200, 100);
this.Controls.Add(textBox1);
4. GroupBox
• Description: Groups related controls with a border and
caption.
• Key Properties:
o Text: Sets the group box title.
• Example:
GroupBox groupBox1 = new GroupBox();
groupBox1.Text = "User Details";
groupBox1.Location = new Point(10, 10);
groupBox1.Size = new Size(300, 200);
RadioButton radioButton1 = new RadioButton();
radioButton1.Text = "Male";
radioButton1.Location = new Point(20, 30);
RadioButton radioButton2 = new RadioButton();
radioButton2.Text = "Female";
radioButton2.Location = new Point(20, 60);
groupBox1.Controls.Add(radioButton1);
groupBox1.Controls.Add(radioButton2);
this.Controls.Add(groupBox1);
5. Panel
• Description: A container for other controls with optional
scrollbars.
• Key Properties:
o AutoScroll: Enables scrolling.
• Example:
Panel panel1 = new Panel();
panel1.Size = new Size(300, 200);
panel1.Location = new Point(10, 10);
panel1.AutoScroll = true;
Label label1 = new Label();
label1.Text = "This is a panel.";
label1.Location = new Point(10, 10);
panel1.Controls.Add(label1);
this.Controls.Add(panel1);
6. RadioButton
• Description: Allows users to select one option from a
group.
• Key Properties:
o Checked: Indicates if the radio button is selected.
• Key Events:
o CheckedChanged: Triggered when the state changes.
• Example:
RadioButton radioButton1 = new RadioButton();
radioButton1.Text = "Option 1";
radioButton1.Location = new Point(20, 20);
radioButton1.CheckedChanged += (sender, e) =>{
if (radioButton1.Checked)
MessageBox.Show("Option 1 Selected!");
};
this.Controls.Add(radioButton1);
7. ListBox
• Description: Displays a list of items for selection.
• Key Properties:
o Items: The collection of items in the ListBox.
o SelectionMode: Specifies single or multiple selection.
• Key Events:
o SelectedIndexChanged: Triggered when the selection
changes.
• Example:
ListBox listBox1 = new ListBox();
listBox1.Location = new Point(20, 20);
listBox1.Size = new Size(200, 100);
listBox1.Items.Add("Item 1");
listBox1.Items.Add("Item 2");
listBox1.Items.Add("Item 3");
listBox1.SelectedIndexChanged += (sender, e) => {
string selectedItem = listBox1.SelectedItem?.ToString();
MessageBox.Show("You selected: " + selectedItem);
};
this.Controls.Add(listBox1);
8. MessageBox
• Description: Displays a modal dialog with a message and
buttons.
• Key Methods:
o MessageBox.Show: Displays the MessageBox.
• Example:
• MessageBox.Show("Hello, World!", "Greeting",
MessageBoxButtons.OK, MessageBoxIcon.Information);
What is a Menu?
In a Windows-based application, a menu is a graphical control
element that provides users with a list of commands or options
to perform actions or navigate the application. Menus are an
essential part of the user interface in desktop applications,
allowing users to interact with the software in an organized and
intuitive way.
Menus are usually located at the top of a window and are
structured hierarchically with menu headers (e.g., File, Edit,
View) and sub-menu items (e.g., New, Open, Save).
Characteristics of a Menu:
• Organized Commands: Menus group related functionalities
under headers.
• Interactive: Menus are activated through mouse clicks or
keyboard shortcuts.
• Expandable: Menus can have nested submenus, offering a
deeper structure.
Example of a Basic Menu Layout:
File Edit View Help
-----------------------------------
New Undo Zoom In About
Open Redo Zoom Out
Save
Exit
Types of Menus in Windows Forms:
1. Main Menu: The primary menu bar at the top of the form
(e.g., File, Edit).
2. Submenus: Nested items under a main menu header.
3. Context Menu (Popup Menu): A menu that appears upon right-
clicking a specific control or area in the application.
4. Tool Strip Menu (MenuStrip): An enhanced menu introduced in
.NET 4.0 that provides more functionality and modern
design.
Menus in Programming:
Menus in a Windows Forms application are typically created using
the MainMenu class in .NET (prior to .NET 4.0) or the MenuStrip
control (in .NET 4.0 and later). These classes allow developers
to define menus, add items, and handle user interactions.
ToolTip in C#
In Windows Forms, a ToolTip is a small pop-up that appears when
the cursor hovers over a control, providing a brief description.
The ToolTip class, found in System.Windows.Forms, allows the
creation and customization of these tooltips.
Two Methods to Create ToolTips:
1. Design-Time:
o Drag and drop the ToolTip control from the toolbox to
the form.
o Modify its properties in the properties window.
2. Run-Time:
o Create a ToolTip control programmatically.
o Example:
csharp
Copy code
ToolTip t_Tip = new ToolTip();
t_Tip.SetToolTip(box1, "Name should start with a capital
letter");
t_Tip.SetToolTip(box2, "Password should be at least 8
characters");
Key Properties:
• Active: Activates or deactivates the ToolTip.
• AutoPopDelay: Time the ToolTip stays visible.
• InitialDelay: Time before the ToolTip appears.
• IsBalloon: Displays the tooltip as a balloon.
• ToolTipIcon: Adds an icon to the ToolTip.
Example:
ToolTip t_Tip = new ToolTip();
t_Tip.Active = true;
t_Tip.AutoPopDelay = 4000;
t_Tip.InitialDelay = 600;
t_Tip.IsBalloon = true;
t_Tip.ToolTipIcon = ToolTipIcon.Info;
t_Tip.SetToolTip(box1, "Name should start with Capital letter");
t_Tip.SetToolTip(box2, "Password should be greater than 8
words");
Conclusion:
ToolTip provides a simple way to add brief, helpful descriptions
to your form controls, improving user experience by giving them
hints about input requirements or actions.