using System; using System.Drawing; using System.Windows.Forms; class NotepadCloneNoMenu: Form { protected TextBox txtbox; public static void Main() { Application.Run(new NotepadCloneNoMenu()); } public NotepadCloneNoMenu() { Text = "Notepad Clone No Menu"; txtbox = new TextBox(); txtbox.Parent = this; txtbox.Dock = DockStyle.Fill; txtbox.BorderStyle = BorderStyle.None; txtbox.Multiline = true; txtbox.ScrollBars = ScrollBars.Both; txtbox.AcceptsTab = true; } }
GUI Windows Form
TabControl
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Windows.Forms; using System.Text; class Form1 : Form { public Form1() { this.tabPage2 = new System.Windows.Forms.TabPage(); this.textBoxMessage = new System.Windows.Forms.TextBox(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.buttonShowMessage = new System.Windows.Forms.Button(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage2.SuspendLayout(); this.tabPage1.SuspendLayout(); this.tabControl1.SuspendLayout(); this.SuspendLayout(); this.tabPage2.Controls.Add(this.textBoxMessage); this.tabPage2.Location = new System.Drawing.Point(4, 22); this.tabPage2.Name = "tabPage2"; this.tabPage2.Padding = new System.Windows.Forms.Padding(3); this.tabPage2.Size = new System.Drawing.Size(259, 37); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "Tab Two"; this.textBoxMessage.Location = new System.Drawing.Point(72, 7); this.textBoxMessage.Name = "textBoxMessage"; this.textBoxMessage.Size = new System.Drawing.Size(100, 20); this.textBoxMessage.TabIndex = 0; this.tabPage1.Controls.Add(this.buttonShowMessage); this.tabPage1.Location = new System.Drawing.Point(4, 22); this.tabPage1.Name = "tabPage1"; this.tabPage1.Padding = new System.Windows.Forms.Padding(3); this.tabPage1.Size = new System.Drawing.Size(259, 37); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "Tab One"; this.buttonShowMessage.Location = new System.Drawing.Point(74, 7); this.buttonShowMessage.Name = "buttonShowMessage"; this.buttonShowMessage.Size = new System.Drawing.Size(107, 24); this.buttonShowMessage.TabIndex = 0; this.buttonShowMessage.Text = "Show Message"; this.buttonShowMessage.Click += new System.EventHandler(this.buttonShowMessage_Click); this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Controls.Add(this.tabPage2); this.tabControl1.Location = new System.Drawing.Point(13, 13); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(267, 63); this.tabControl1.TabIndex = 0; this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 83); this.Controls.Add(this.tabControl1); this.tabPage2.ResumeLayout(false); this.tabPage2.PerformLayout(); this.tabPage1.ResumeLayout(false); this.tabControl1.ResumeLayout(false); this.ResumeLayout(false); } private void buttonShowMessage_Click(object sender, EventArgs e) { MessageBox.Show(this.textBoxMessage.Text); } private System.Windows.Forms.TabPage tabPage2; private System.Windows.Forms.TextBox textBoxMessage; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.Button buttonShowMessage; private System.Windows.Forms.TabControl tabControl1; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new Form1()); } }
System Tray App
/* User Interfaces in C#: Windows Forms and Custom Controls by Matthew MacDonald Publisher: Apress ISBN: 1590590457 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace SystemTrayApp { /// <summary> /// Summary description for SystemTrayApp. /// </summary> public class SystemTrayApp : System.Windows.Forms.Form { internal System.Windows.Forms.Label Label1; internal System.Windows.Forms.Button cmdClose; internal System.Windows.Forms.ListBox lstFiles; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public SystemTrayApp() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if(components != null) { components.Dispose(); } } base.Dispose( disposing ); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Label1 = new System.Windows.Forms.Label(); this.cmdClose = new System.Windows.Forms.Button(); this.lstFiles = new System.Windows.Forms.ListBox(); this.SuspendLayout(); // // Label1 // this.Label1.Location = new System.Drawing.Point(10, 7); this.Label1.Name = "Label1"; this.Label1.Size = new System.Drawing.Size(140, 16); this.Label1.TabIndex = 5; this.Label1.Text = "Recently created files:"; // // cmdClose // this.cmdClose.Anchor = (System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right); this.cmdClose.FlatStyle = System.Windows.Forms.FlatStyle.System; this.cmdClose.Location = new System.Drawing.Point(162, 203); this.cmdClose.Name = "cmdClose"; this.cmdClose.Size = new System.Drawing.Size(88, 24); this.cmdClose.TabIndex = 4; this.cmdClose.Text = "Close"; this.cmdClose.Click += new System.EventHandler(this.cmdClose_Click); // // lstFiles // this.lstFiles.Anchor = (((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right); this.lstFiles.IntegralHeight = false; this.lstFiles.Location = new System.Drawing.Point(10, 27); this.lstFiles.Name = "lstFiles"; this.lstFiles.Size = new System.Drawing.Size(240, 168); this.lstFiles.TabIndex = 3; // // SystemTrayApp // this.AutoScaleBaseSize = new System.Drawing.Size(5, 14); this.ClientSize = new System.Drawing.Size(260, 234); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.Label1, this.cmdClose, this.lstFiles}); this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Name = "SystemTrayApp"; this.Text = "SystemTrayApp"; this.ResumeLayout(false); } #endregion private void cmdClose_Click(object sender, System.EventArgs e) { this.Close(); } public void FillList(ArrayList list) { lstFiles.DataSource = list; } } } //==================================================================== //==================================================================== using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.IO; namespace SystemTrayApp { public class App { // Define the system tray icon control. private NotifyIcon appIcon = new NotifyIcon(); // Define the menu. private ContextMenu sysTrayMenu = new ContextMenu(); private MenuItem displayFiles = new MenuItem("Display New Files"); private MenuItem exitApp = new MenuItem("Exit"); // Define the file system watcher, and a list to store filenames. private FileSystemWatcher watch = new FileSystemWatcher(); private ArrayList newFiles = new ArrayList(); public void Start() { // Configure the system tray icon. Icon ico = new Icon("icon.ico"); appIcon.Icon = ico; appIcon.Text = "My .NET Application"; // Place the menu items in the menu. sysTrayMenu.MenuItems.Add(displayFiles); sysTrayMenu.MenuItems.Add(exitApp); appIcon.ContextMenu = sysTrayMenu; // Show the system tray icon. appIcon.Visible = true; // Hook up the file watcher. watch.Path = "c:"; watch.IncludeSubdirectories = true; watch.EnableRaisingEvents = true; // Attach event handlers. watch.Created += new FileSystemEventHandler(FileCreated); displayFiles.Click += new EventHandler(DisplayFiles); exitApp.Click += new EventHandler(ExitApp); } private void FileCreated(object sender, System.IO.FileSystemEventArgs e) { newFiles.Add(e.Name); } private void ExitApp(object sender, System.EventArgs e) { Application.Exit(); } private void DisplayFiles(object sender, System.EventArgs e) { FileList frmFileList = new FileList(); frmFileList.FillList(newFiles); frmFileList.Show(); } public static void Main() { App app = new App(); app.Start(); // Because no forms are being displayed, you need this // statement to stop the application from automatically ending. Application.Run(); } } } SystemTrayApp.zip( 26 k)
Set Text to Statusbar
using System; using System.Drawing; using System.Windows.Forms; class SimpleStatusBar: Form { public static void Main() { Application.Run(new SimpleStatusBar()); } public SimpleStatusBar() { Text = "Simple Status Bar"; ResizeRedraw = true; StatusBar sb = new StatusBar(); sb.Parent = this; sb.Text = "My initial status bar text"; } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; Pen pen = new Pen(ForeColor); grfx.DrawLine(pen, 0, 0, ClientSize.Width, ClientSize.Height); grfx.DrawLine(pen, ClientSize.Width, 0, 0, ClientSize.Height); } }
Add StatusPanels to StatusBar
using System; using System.Drawing; using System.Windows.Forms; class TwoStatusBarPanels: Form { public static void Main() { Application.Run(new TwoStatusBarPanels()); } public TwoStatusBarPanels() { Text = "Two Status Bar Panels"; StatusBar sb = new StatusBar(); sb.Parent = this; sb.ShowPanels = true; StatusBarPanel sbpanel1 = new StatusBarPanel(); sbpanel1.Text = "Panel 1"; StatusBarPanel sbpanel2 = new StatusBarPanel(); sbpanel2.Text = "Panel 2"; sb.Panels.Add(sbpanel1); sb.Panels.Add(sbpanel2); } }
Use StatusBarPanel
using System; using System.Drawing; using System.Windows.Forms; class MenuHelpFirstTry: Form { StatusBarPanel sbpMenuHelp; string strSavePanelText; public static void Main() { Application.Run(new MenuHelpFirstTry()); } public MenuHelpFirstTry() { StatusBar sb = new StatusBar(); sb.Parent = this; sb.ShowPanels = true; sbpMenuHelp = new StatusBarPanel(); sbpMenuHelp.Text = "Ready"; sbpMenuHelp.AutoSize = StatusBarPanelAutoSize.Spring; sb.Panels.Add(sbpMenuHelp); Menu = new MainMenu(); EventHandler ehSelect = new EventHandler(MenuOnSelect); MenuItem mi = new MenuItem("File"); mi.Select += ehSelect; Menu.MenuItems.Add(mi); mi = new MenuItem("Open"); mi.Select += ehSelect; Menu.MenuItems[0].MenuItems.Add(mi); mi = new MenuItem("Close"); mi.Select += ehSelect; Menu.MenuItems[0].MenuItems.Add(mi); mi = new MenuItem("Save"); mi.Select += ehSelect; Menu.MenuItems[0].MenuItems.Add(mi); mi = new MenuItem("Edit"); mi.Select += ehSelect; Menu.MenuItems.Add(mi); mi = new MenuItem("Cut"); mi.Select += ehSelect; Menu.MenuItems[1].MenuItems.Add(mi); mi = new MenuItem("Copy"); mi.Select += ehSelect; Menu.MenuItems[1].MenuItems.Add(mi); mi = new MenuItem("Paste"); mi.Select += ehSelect; Menu.MenuItems[1].MenuItems.Add(mi); } protected override void OnMenuStart(EventArgs ea) { strSavePanelText = sbpMenuHelp.Text; } protected override void OnMenuComplete(EventArgs ea) { sbpMenuHelp.Text = strSavePanelText; } void MenuOnSelect(object obj, EventArgs ea) { MenuItem mi = (MenuItem) obj; string str; switch (mi.Text) { case "File": str = "file"; break; case "Open": str = "Open"; break; case "Close": str = "Close"; break; case "Save": str = "Save"; break; case "Edit": str = "edit"; break; case "Cut": str = "cut"; break; case "Copy": str = "Copy"; break; case "Paste": str = "Paste"; break; default: str = ""; break; } sbpMenuHelp.Text = str; } }
Status Strip Example
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; enum DateTimeFormat { ShowClock, ShowDay } public class MainWindow : Form { DateTimeFormat dtFormat = DateTimeFormat.ShowClock; private ToolStripMenuItem currentCheckedItem; public MainWindow() { InitializeComponent(); BackColor = Color.CadetBlue; currentCheckedItem = currentTimeToolStripMenuItem; currentCheckedItem.Checked = true; } private void timerDateTimeUpdate_Tick(object sender, EventArgs e) { string panelInfo = ""; if (dtFormat == DateTimeFormat.ShowClock) panelInfo = DateTime.Now.ToLongTimeString(); else panelInfo = DateTime.Now.ToLongDateString(); toolStripStatusLabelClock.Text = panelInfo; } private void currentTimeToolStripMenuItem_Click(object sender, EventArgs e) { currentCheckedItem.Checked = false; dtFormat = DateTimeFormat.ShowClock; currentCheckedItem = currentTimeToolStripMenuItem; currentCheckedItem.Checked = true; } private void dayoftheWeekToolStripMenuItem_Click(object sender, EventArgs e) { currentCheckedItem.Checked = false; dtFormat = DateTimeFormat.ShowDay; currentCheckedItem = dayoftheWeekToolStripMenuItem; currentCheckedItem.Checked = true; } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void aboutToolStripMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("My StatusStripApp!"); } private void exitToolStripMenuItem_MouseHover(object sender, EventArgs e) { toolStripStatusLabelMenuState.Text = "Exits the app."; } private void aboutToolStripMenuItem_MouseHover(object sender, EventArgs e) { toolStripStatusLabelMenuState.Text = "Shows about box."; } private void dayoftheWeekToolStripMenuItem_MouseHover(object sender, EventArgs e) { toolStripStatusLabelMenuState.Text = "Shows the day of the week."; } private void currentTimeToolStripMenuItem_MouseHover(object sender, EventArgs e) { toolStripStatusLabelMenuState.Text = "Shows the current time."; } private void SetReadyPrompt(object sender, EventArgs e) { toolStripStatusLabelMenuState.Text = "Ready."; } private void InitializeComponent() { this.mainStatusStrip = new System.Windows.Forms.StatusStrip(); this.toolStripStatusLabelMenuState = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabelClock = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripDropDownButtonDateTime = new System.Windows.Forms.ToolStripDropDownButton(); this.dayoftheWeekToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.currentTimeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.menuStrip1 = new System.Windows.Forms.MenuStrip(); this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.aboutToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.timerDateTimeUpdate = new System.Windows.Forms.Timer(); this.mainStatusStrip.SuspendLayout(); this.menuStrip1.SuspendLayout(); this.SuspendLayout(); // // mainStatusStrip // this.mainStatusStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripStatusLabelMenuState, this.toolStripStatusLabelClock, this.toolStripDropDownButtonDateTime}); this.mainStatusStrip.LayoutStyle = System.Windows.Forms.ToolStripLayoutStyle.Table; this.mainStatusStrip.Location = new System.Drawing.Point(0, 78); this.mainStatusStrip.Name = "mainStatusStrip"; this.mainStatusStrip.Size = new System.Drawing.Size(361, 27); this.mainStatusStrip.TabIndex = 0; this.mainStatusStrip.Text = "statusStrip1"; // // toolStripStatusLabelMenuState // this.toolStripStatusLabelMenuState.Name = "toolStripStatusLabelMenuState"; this.toolStripStatusLabelMenuState.Spring = true; this.toolStripStatusLabelMenuState.TextAlign = System.Drawing.ContentAlignment.TopLeft; // // toolStripStatusLabelClock // this.toolStripStatusLabelClock.BorderSides = ((System.Windows.Forms.ToolStripStatusLabelBorderSides)((((System.Windows.Forms.ToolStripStatusLabelBorderSides.Left | System.Windows.Forms.ToolStripStatusLabelBorderSides.Top) | System.Windows.Forms.ToolStripStatusLabelBorderSides.Right) | System.Windows.Forms.ToolStripStatusLabelBorderSides.Bottom))); this.toolStripStatusLabelClock.Name = "toolStripStatusLabelClock"; // // toolStripDropDownButtonDateTime // this.toolStripDropDownButtonDateTime.DisplayStyle = System.Windows.Forms.ToolStripItemDisplayStyle.Image; this.toolStripDropDownButtonDateTime.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.dayoftheWeekToolStripMenuItem, this.currentTimeToolStripMenuItem}); this.toolStripDropDownButtonDateTime.ImageTransparentColor = System.Drawing.Color.Magenta; this.toolStripDropDownButtonDateTime.Name = "toolStripDropDownButtonDateTime"; this.toolStripDropDownButtonDateTime.Text = "toolStripDropDownButton1"; // // dayoftheWeekToolStripMenuItem // this.dayoftheWeekToolStripMenuItem.Name = "dayoftheWeekToolStripMenuItem"; this.dayoftheWeekToolStripMenuItem.Text = "Day of the Week"; this.dayoftheWeekToolStripMenuItem.MouseLeave += new System.EventHandler(this.SetReadyPrompt); this.dayoftheWeekToolStripMenuItem.MouseHover += new System.EventHandler(this.dayoftheWeekToolStripMenuItem_MouseHover); this.dayoftheWeekToolStripMenuItem.Click += new System.EventHandler(this.dayoftheWeekToolStripMenuItem_Click); // // currentTimeToolStripMenuItem // this.currentTimeToolStripMenuItem.Name = "currentTimeToolStripMenuItem"; this.currentTimeToolStripMenuItem.Text = "Current Time"; this.currentTimeToolStripMenuItem.MouseLeave += new System.EventHandler(this.SetReadyPrompt); this.currentTimeToolStripMenuItem.MouseHover += new System.EventHandler(this.currentTimeToolStripMenuItem_MouseHover); this.currentTimeToolStripMenuItem.Click += new System.EventHandler(this.currentTimeToolStripMenuItem_Click); // // menuStrip1 // this.menuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.fileToolStripMenuItem, this.helpToolStripMenuItem}); this.menuStrip1.Location = new System.Drawing.Point(0, 0); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(361, 24); this.menuStrip1.TabIndex = 1; this.menuStrip1.Text = "menuStrip1"; // // fileToolStripMenuItem // this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.exitToolStripMenuItem}); this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Text = "&File"; // // exitToolStripMenuItem // this.exitToolStripMenuItem.Name = "exitToolStripMenuItem"; this.exitToolStripMenuItem.Text = "E&xit"; this.exitToolStripMenuItem.MouseLeave += new System.EventHandler(this.SetReadyPrompt); this.exitToolStripMenuItem.MouseHover += new System.EventHandler(this.exitToolStripMenuItem_MouseHover); this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click); // // helpToolStripMenuItem // this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.aboutToolStripMenuItem}); this.helpToolStripMenuItem.Name = "helpToolStripMenuItem"; this.helpToolStripMenuItem.Text = "&Help"; // // aboutToolStripMenuItem // this.aboutToolStripMenuItem.Name = "aboutToolStripMenuItem"; this.aboutToolStripMenuItem.Text = "&About"; this.aboutToolStripMenuItem.MouseLeave += new System.EventHandler(this.SetReadyPrompt); this.aboutToolStripMenuItem.MouseHover += new System.EventHandler(this.aboutToolStripMenuItem_MouseHover); this.aboutToolStripMenuItem.Click += new System.EventHandler(this.aboutToolStripMenuItem_Click); // // timerDateTimeUpdate // this.timerDateTimeUpdate.Enabled = true; this.timerDateTimeUpdate.Interval = 1000; this.timerDateTimeUpdate.Tick += new System.EventHandler(this.timerDateTimeUpdate_Tick); // // MainWindow // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(361, 105); this.Controls.Add(this.mainStatusStrip); this.Controls.Add(this.menuStrip1); this.MainMenuStrip = this.menuStrip1; this.Name = "MainWindow"; this.Text = "Form1"; this.mainStatusStrip.ResumeLayout(false); this.menuStrip1.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } private System.Windows.Forms.StatusStrip mainStatusStrip; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelMenuState; private System.Windows.Forms.ToolStripStatusLabel toolStripStatusLabelClock; private System.Windows.Forms.ToolStripDropDownButton toolStripDropDownButtonDateTime; private System.Windows.Forms.MenuStrip menuStrip1; private System.Windows.Forms.ToolStripMenuItem fileToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem exitToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem helpToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem aboutToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem dayoftheWeekToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem currentTimeToolStripMenuItem; private System.Windows.Forms.Timer timerDateTimeUpdate; [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainWindow()); } }