0% found this document useful (0 votes)
6 views15 pages

Windows Properties

The document provides examples of accessing and modifying various Windows properties using C#. It covers changing window title, size, position, state, style, and background color, as well as handling events and creating modal and modeless windows. Additional topics include displaying system tray icons, customizing window appearance with themes, and working with multiple document interface (MDI) applications.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views15 pages

Windows Properties

The document provides examples of accessing and modifying various Windows properties using C#. It covers changing window title, size, position, state, style, and background color, as well as handling events and creating modal and modeless windows. Additional topics include displaying system tray icons, customizing window appearance with themes, and working with multiple document interface (MDI) applications.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 15

Handout One: Windows properties Examples

Here are a few examples of accessing and modifying Windows properties using C#:

Changing Window Title:

You can change the title of a window using the Text property of a Form object. Here's an example:

using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

Text = "Original Window Title";

public static void Main()

Application.Run(new MainForm());

Changing Window Size:

You can modify the size of a window using the Width and Height properties of a Form object. Here's an
example:

using System;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

Width = 800;

Dr. Ahmed Alnasheri 1/15


Height = 600;

public static void Main()

Application.Run(new MainForm());

Changing Window Position:

You can modify the position of a window using the Location property of a Form object. Here's an

using System;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

Location = new Point(200, 200);

public static void Main()

Application.Run(new MainForm());

Maximizing and Minimizing Window:

You can maximize or minimize a window using the WindowState property of a Form object. Here's an
example:

Dr. Ahmed Alnasheri 2/15


using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

WindowState = FormWindowState.Maximized; // or FormWindowState.Minimized

public static void Main()

Application.Run(new MainForm());

Changing Window Style:

You can modify the window style using the FormBorderStyle property of a Form object. Here's an
example:

using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

FormBorderStyle = FormBorderStyle.FixedSingle; // or other values like Fixed3D, None, etc.

public static void Main()

Application.Run(new MainForm());

Dr. Ahmed Alnasheri 3/15


Here are a few more examples and concepts related to working with Windows properties using C#:

Changing Window Icon:

You can set a custom icon for a window using the Icon property of a Form object. Here's an example:

using System;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

Icon = new Icon("icon.ico"); // Specify the path to your custom icon file

public static void Main()

Application.Run(new MainForm());

Displaying System Tray Icon:

You can display a system tray icon for your application using the NotifyIcon class. Here's an example:

using System;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

private NotifyIcon trayIcon;

public MainForm()

// Initialize the form

Dr. Ahmed Alnasheri 4/15


Text = "Main Form";

Width = 300;

Height = 200;

// Create the tray icon

trayIcon = new NotifyIcon();

trayIcon.Icon = new Icon("icon.ico");

trayIcon.Text = "My Application";

trayIcon.Visible = true;

// Add a context menu to the tray icon

ContextMenu trayMenu = new ContextMenu();

trayMenu.MenuItems.Add("Exit", OnExit);

trayIcon.ContextMenu = trayMenu;

private void OnExit(object sender, EventArgs e)

// Clean up resources and exit the application

trayIcon.Visible = false;

Application.Exit();

public static void Main()

Application.Run(new MainForm());

Dr. Ahmed Alnasheri 5/15


Changing Window Background Color:

You can modify the background color of a window using the BackColor property of a Control object.
Here's an example:

using System;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

BackColor = Color.LightGray;

public static void Main()

Application.Run(new MainForm());

Hiding and Showing Window Borders:

You can hide or show the window borders using the FormBorderStyle property of a Form object. Here's
an example:

using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

FormBorderStyle = FormBorderStyle.None; // To hide the borders

// or

FormBorderStyle = FormBorderStyle.FixedSingle; // To show the borders

Dr. Ahmed Alnasheri 6/15


}

public static void Main()

Application.Run(new MainForm());

Here are a few more examples and concepts related to working with Windows properties using C#:

Changing Window Transparency:

You can adjust the transparency of a window using the Opacity property of a Form object. The value
ranges from 0.0 (completely transparent) to 1.0 (fully opaque). Here's an example:

using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

Opacity = 0.75; // Set the window opacity to 75%

public static void Main()

Application.Run(new MainForm());

Resizing and Moving Windows:

You can programmatically resize and move a window using the Size and Location properties of a Form
object. Here's an example that resizes and centers the window on the screen:

Dr. Ahmed Alnasheri 7/15


using System;

using System.Drawing;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

Size = new Size(400, 300); // Set the window size

// Center the window on the screen

Rectangle screenBounds = Screen.PrimaryScreen.Bounds;

Location = new Point(

(screenBounds.Width - Size.Width) / 2,

(screenBounds.Height - Size.Height) / 2

);

public static void Main()

Application.Run(new MainForm());

Changing Window State Dynamically:

You can change the window state dynamically during runtime. For example, you can maximize or
minimize the window based on user actions or application logic. Here's an example that toggles the
window state when a button is clicked:

using System;

using System.Windows.Forms;

Dr. Ahmed Alnasheri 8/15


public class MainForm : Form

private Button toggleButton;

public MainForm()

// Create a button to toggle the window state

toggleButton = new Button();

toggleButton.Text = "Toggle Window State";

toggleButton.Click += ToggleButton_Click;

Controls.Add(toggleButton);

private void ToggleButton_Click(object sender, EventArgs e)

// Toggle the window state

if (WindowState == FormWindowState.Normal)

WindowState = FormWindowState.Maximized;

else

WindowState = FormWindowState.Normal;

public static void Main()

Application.Run(new MainForm());

Handling Window Events:

Dr. Ahmed Alnasheri 9/15


Windows forms provide various events that you can handle to respond to specific window-related
actions. For example, you can handle the FormClosing event to perform cleanup operations before the
window is closed. Here's an example:

using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

// Attach an event handler to the FormClosing event

FormClosing += MainForm_FormClosing;

private void MainForm_FormClosing(object sender, FormClosingEventArgs e)

// Perform cleanup operations before the window is closed

// For example, prompt the user to save unsaved changes

DialogResult result = MessageBox.Show(

"Do you want to save your changes?",

"Confirm",

MessageBoxButtons.YesNoCancel,

MessageBoxIcon.Question

);

if (result == DialogResult.Yes)

// Save changes

Dr. Ahmed Alnasheri 10/15


else if (result == DialogResult.Cancel)

// Cancel the window closing operation

e.Cancel = true;

public static void Main()

Application.Run(new MainForm());

Here are a few more examples and concepts related to working with Windows properties using C#:

Displaying Modal and Modeless Windows:

You can create modal and modeless windows in your application. Modal windows block interaction with
other windows until they are closed, while modeless windows allow interaction with other windows
while they are open. Here's an example of creating a modal window:

using System;

using System.Windows.Forms;

public class MainForm : Form

private Button openModalButton;

public MainForm()

// Create a button to open the modal window

openModalButton = new Button();

Dr. Ahmed Alnasheri 11/15


openModalButton.Text = "Open Modal Window";

openModalButton.Click += OpenModalButton_Click;

Controls.Add(openModalButton);

private void OpenModalButton_Click(object sender, EventArgs e)

using (var modalForm = new ModalForm())

modalForm.ShowDialog(); // Display the modal window

public static void Main()

Application.Run(new MainForm());

public class ModalForm : Form

public ModalForm()

Text = "Modal Window";

Setting Window Topmost:

You can set a window to be always on top of other windows using the TopMost property of a Form
object. Here's an example:

Dr. Ahmed Alnasheri 12/15


using System;

using System.Windows.Forms;

public class MainForm : Form

private Button setTopmostButton;

public MainForm()

// Create a button to toggle the TopMost property

setTopmostButton = new Button();

setTopmostButton.Text = "Toggle TopMost";

setTopmostButton.Click += SetTopmostButton_Click;

Controls.Add(setTopmostButton);

private void SetTopmostButton_Click(object sender, EventArgs e)

TopMost = !TopMost; // Toggle the TopMost property

public static void Main()

Application.Run(new MainForm());

Working with Multiple Document Interface (MDI) Windows:

If your application requires a multiple document interface, where multiple child windows can be
displayed within a parent window, you can use the Form and MenuStrip controls to create an MDI
application. Here's an example:

using System;

using System.Windows.Forms;

public class MainForm : Form

Dr. Ahmed Alnasheri 13/15


{

private MenuStrip menuStrip;

private ToolStripMenuItem newToolStripMenuItem;

public MainForm()

// Create a menu strip with a "New" menu item

menuStrip = new MenuStrip();

newToolStripMenuItem = new ToolStripMenuItem("New");

newToolStripMenuItem.Click += NewToolStripMenuItem_Click;

menuStrip.Items.Add(newToolStripMenuItem);

MainMenuStrip = menuStrip;

Controls.Add(menuStrip);

// Enable MDI functionality

IsMdiContainer = true;

private void NewToolStripMenuItem_Click(object sender, EventArgs e)

// Create a new child window

var childForm = new ChildForm();

childForm.MdiParent = this;

childForm.Show();

public static void Main()

Application.Run(new MainForm());

public class ChildForm : Form

Dr. Ahmed Alnasheri 14/15


public ChildForm()

Text = "Child Window";

Customizing Window Appearance with Themes:

You can customize the appearance of your windows using themes and visual styles. Windows Forms
provides support for visual styles through the Application.EnableVisualStyles method. Here's an
example:

using System;

using System.Windows.Forms;

public class MainForm : Form

public MainForm()

// Enable visual styles

Application.EnableVisualStyles();

// Create your window and controls

Text = "Themed Window";

// ...

public static void Main()

Application.Run(new MainForm());

Dr. Ahmed Alnasheri 15/15

You might also like