1. Introduction
Controls are UI elements that users interact with in desktop applications, allowing text input, commands, data display, and navigation. Common C# GUI frameworks include Windows Forms and WPF.
2. Controls: Common Control Group
Basic elements mostly used for interaction and display like buttons, labels, and input fields.
Examples:
-
Button: Triggers actions on clicks.
-
Label: Displays static text.
-
TextBox: Accepts text input.
-
CheckBox: Toggles option selection.
-
RadioButton: Selects exclusive options.
-
ListBox: Lists multiple selectable items.
-
ComboBox: Dropdown list for choices.
3. Data Control Group
Specialized controls designed to display and manipulate data sets, such as DataGridView or ListView.
4. Dialog Control Group
UI elements presenting modal or non-modal dialogs like file open/save dialogs, font/color choosers, and message boxes.
5. Container Control Group
Controls that organize and contain other controls to manage layout, such as Panels, GroupBoxes, TabControls, and SplitContainers.
6. Menus and Context Menus
-
MenuStrip: Provides a menu bar with dropdown menus.
-
ToolStrip: Toolbar with buttons and dropdown commands.
7. SDI and MDI Applications
-
SDI: Single document per window.
-
MDI: Parent window hosting multiple child windows.
8. Developing Custom Controls
Extend or create controls by overriding painting methods and adding custom functionality.
9. Composite and Extended Controls
Combine multiple existing controls into a reusable single component.
10. WPF (Windows Presentation Foundation)
Modern UI framework employing XAML for UI design and C# for logic, supporting advanced data binding and styling.
11. Developing WPF Application
Stepwise create apps using XAML design, event handling in code-behind, and MVVM pattern.
Code Demonstrations
Common Controls Demonstration
using System;
using System.Windows.Forms;
using System.Drawing;
public class CommonControlsDemo : Form
{
public CommonControlsDemo()
{
Text = "Common Controls Demo";
Size = new Size(400, 300);
Button btn = new Button() { Text = "Button", Location = new Point(10, 10) };
Label lbl = new Label() { Text = "Label", Location = new Point(10, 50) };
TextBox txt = new TextBox() { Text = "TextBox", Location = new Point(10, 90), Width = 150 };
CheckBox chk = new CheckBox() { Text = "CheckBox", Location = new Point(10, 130) };
RadioButton rb = new RadioButton() { Text = "RadioButton", Location = new Point(10, 160) };
ListBox listBox = new ListBox() { Location = new Point(200, 10), Height = 80 };
listBox.Items.AddRange(new object[] { "Item1", "Item2", "Item3" });
ComboBox comboBox = new ComboBox() { Location = new Point(200, 110) };
comboBox.Items.AddRange(new object[] { "Option1", "Option2", "Option3" });
Controls.AddRange(new Control[] { btn, lbl, txt, chk, rb, listBox, comboBox });
}
}
Container Controls Demonstration
public class ContainersDemo : Form
{
public ContainersDemo()
{
Text = "Container Controls Demo";
Size = new Size(400, 300);
GroupBox groupBox = new GroupBox() { Text = "GroupBox", Location = new Point(10, 10), Size = new Size(150, 100) };
groupBox.Controls.Add(new Label() { Text = "Inside GroupBox", Location = new Point(10, 20) });
Panel panel = new Panel() { Location = new Point(200, 10), Size = new Size(150, 100), BorderStyle = BorderStyle.FixedSingle };
panel.Controls.Add(new Button() { Text = "Panel Button", Location = new Point(10, 10) });
TabControl tabControl = new TabControl() { Location = new Point(10, 130), Size = new Size(350, 100) };
TabPage tab1 = new TabPage("Tab 1");
TabPage tab2 = new TabPage("Tab 2");
tabControl.TabPages.AddRange(new TabPage[] { tab1, tab2 });
Controls.AddRange(new Control[] { groupBox, panel, tabControl });
}
}
Menus and Toolbars Demonstration
public class MenusDemo : Form
{
private MenuStrip menuStrip;
private ToolStrip toolStrip;
public MenusDemo()
{
Text = "Menus and Toolbars Demo";
Size = new Size(400, 300);
menuStrip = new MenuStrip();
var fileMenu = new ToolStripMenuItem("File");
var openMenu = new ToolStripMenuItem("Open");
var exitMenu = new ToolStripMenuItem("Exit");
exitMenu.Click += (s, e) => Close();
fileMenu.DropDownItems.AddRange(new ToolStripItem[] { openMenu, exitMenu });
menuStrip.Items.Add(fileMenu);
MainMenuStrip = menuStrip;
Controls.Add(menuStrip);
toolStrip = new ToolStrip();
var saveButton = new ToolStripButton("Save");
saveButton.Click += (s, e) => MessageBox.Show("Save clicked");
toolStrip.Items.Add(saveButton);
Controls.Add(toolStrip);
}
}
SDI and MDI Demonstration
public class MdiDemoParent : Form
{
public MdiDemoParent()
{
Text = "MDI Parent";
IsMdiContainer = true;
Size = new Size(600, 400);
var child = new Form()
{
Text = "Child Form",
MdiParent = this,
Size = new Size(200, 150)
};
child.Show();
}
}
public class SdiDemo : Form
{
public SdiDemo()
{
Text = "SDI Form";
Size = new Size(400, 300);
}
}
Custom and Composite Controls Demonstration
public class MyCustomButton : Button
{
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
pevent.Graphics.DrawEllipse(Pens.Red, 0, 0, Width - 1, Height - 1);
}
}
public class MyCompositeControl : UserControl
{
public Button button;
public TextBox textBox;
public MyCompositeControl()
{
button = new Button() { Text = "Click Me", Location = new Point(10, 10) };
textBox = new TextBox() { Top = 50, Width = 100 };
Controls.Add(button);
Controls.Add(textBox);
button.Click += (s, e) => { textBox.Text = "Button clicked!"; };
}
}
WPF Application Demonstration
using System.Windows;
public partial class WpfDemoWindow : Window
{
public WpfDemoWindow()
{
Content = System.Windows.Markup.XamlReader.Parse(
@"<Grid xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'>
<Button Width='120' Height='40' HorizontalAlignment='Center' VerticalAlignment='Center' Content='Click Me' Click='Button_Click'/>
</Grid>") as UIElement;
Width = 300;
Height = 200;
Title = "WPF Button Demo";
AddHandler(System.Windows.Controls.Button.ClickEvent, new RoutedEventHandler(Button_Click));
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("WPF Button Clicked!");
}
}
Main Program Entry Point
using System;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Threading;
static class Program
{
[STAThread]
static void Main()
{
Console.WriteLine("Starting Windows Forms Demo...");
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
var form = new WinFormsDemo();
form.Show();
Thread wpfThread = new Thread(() =>
{
var app = new System.Windows.Application();
app.Run(new WpfDemoWindow());
});
wpfThread.SetApartmentState(ApartmentState.STA);
wpfThread.Start();
Application.Run(form);
}
}