using System; using System.Windows.Forms; class MainClass { static void Main(string[] args) { SaveFileDialog dlg = new SaveFileDialog(); dlg.CheckFileExists = true; if (dlg.ShowDialog() == DialogResult.OK) { Console.WriteLine(dlg.FileName); } } }
GUI Windows Form
Set InitialDirectory
using System; using System.Windows.Forms; class MainClass { static void Main(string[] args) { OpenFileDialog dlg = new OpenFileDialog(); dlg.InitialDirectory = Application.StartupPath; if (dlg.ShowDialog() == DialogResult.OK) { Console.WriteLine(dlg.FileName); } } }
Call ShowDialog() to display an OpenFileDialog
using System; using System.Windows.Forms; class MainClass { static void Main(string[] args) { OpenFileDialog dlg = new OpenFileDialog(); if (dlg.ShowDialog() == DialogResult.OK) { Console.WriteLine(dlg.FileName); } } }
Subclass NativeWindow
using System; using System.Drawing; using System.Windows.Forms; class CaptureLoss: Form { public static void Main() { Application.Run(new CaptureLoss()); } public CaptureLoss() { Text = "Capture Loss"; CaptureLossWindow win = new CaptureLossWindow(); win.form = this; win.AssignHandle(Handle); } protected override void OnMouseDown(MouseEventArgs mea) { Invalidate(); } public void OnLostCapture() { Invalidate(); } protected override void OnPaint(PaintEventArgs pea) { Graphics grfx = pea.Graphics; if (Capture) grfx.FillRectangle(Brushes.Red, ClientRectangle); else grfx.FillRectangle(Brushes.Gray, ClientRectangle); } } class CaptureLossWindow : NativeWindow { public CaptureLoss form; protected override void WndProc(ref Message message) { if (message.Msg == 533) // WM_CAPTURECHANGED form.OnLostCapture(); base.WndProc(ref message); } }
Mouse action: left, right, middle button and cursor position
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; public class MainForm : System.Windows.Forms.Form { private System.ComponentModel.Container components; public MainForm() { Top = 100; Left = 75; Height = 100; Width = 500; MessageBox.Show(Bounds.ToString(), "Current rect"); this.MouseUp += new MouseEventHandler(OnMouseUp); this.MouseMove += new MouseEventHandler(OnMouseMove); this.KeyUp += new KeyEventHandler(OnKeyUp); InitializeComponent(); CenterToScreen(); } protected override void Dispose( bool disposing ) { if( disposing ) { if (components != null) { components.Dispose(); } } base.Dispose( disposing ); MessageBox.Show("Disposing this Form"); } private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.Size = new System.Drawing.Size(300,300); this.Text = "Form1"; } [STAThread] static void Main() { Application.Run(new MainForm()); } protected void OnMouseUp(object sender, MouseEventArgs e) { if(e.Button == MouseButtons.Left) MessageBox.Show("Left click!"); else if(e.Button == MouseButtons.Right) MessageBox.Show("Right click!"); else if(e.Button == MouseButtons.Middle) MessageBox.Show("Middle click!"); } protected void OnMouseMove(object sender, MouseEventArgs e) { this.Text = "Current Pos: (" + e.X + ", " + e.Y + ")"; } public void OnKeyUp(object sender, KeyEventArgs e) { MessageBox.Show(e.KeyCode.ToString(), "Key Pressed!"); } }
Mouse event on a control
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.Reflection; namespace Events { /// <summary> /// Summary description for FormEvents. /// </summary> public class FormEvents : System.Windows.Forms.Form { private System.Windows.Forms.Button btnOK; private System.Windows.Forms.Button btnCancel; private System.Windows.Forms.TextBox textBox1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public FormEvents() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // // Add the following line to the constructor. Application.Idle += new System.EventHandler(OnIdle); } private void OnIdle (object sender, EventArgs e) { btnOK.Enabled = textBox1.Text.Length > 0; } /// <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.btnOK = new System.Windows.Forms.Button(); this.btnCancel = new System.Windows.Forms.Button(); this.textBox1 = new System.Windows.Forms.TextBox(); this.SuspendLayout(); // // btnOK // this.btnOK.BackColor = System.Drawing.SystemColors.Control; this.btnOK.Location = new System.Drawing.Point(56, 192); this.btnOK.Name = "btnOK"; this.btnOK.Size = new System.Drawing.Size(80, 24); this.btnOK.TabIndex = 0; this.btnOK.Text = "OK"; this.btnOK.Click += new System.EventHandler(this.btnOK_OnClick); this.btnOK.MouseEnter += new System.EventHandler(this.OnMouseEnter); this.btnOK.MouseLeave += new System.EventHandler(this.OnMouseLeave); // // btnCancel // this.btnCancel.BackColor = System.Drawing.SystemColors.Control; this.btnCancel.Location = new System.Drawing.Point(160, 192); this.btnCancel.Name = "btnCancel"; this.btnCancel.Size = new System.Drawing.Size(88, 24); this.btnCancel.TabIndex = 1; this.btnCancel.Text = "Cancel"; this.btnCancel.Click += new System.EventHandler(this.btnCancel_OnClick); this.btnCancel.MouseEnter += new System.EventHandler(this.OnMouseEnter); this.btnCancel.MouseLeave += new System.EventHandler(this.OnMouseLeave); // // textBox1 // this.textBox1.Location = new System.Drawing.Point(48, 144); this.textBox1.Name = "textBox1"; this.textBox1.Size = new System.Drawing.Size(200, 20); this.textBox1.TabIndex = 2; this.textBox1.Text = ""; // // FormEvents // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(292, 273); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.textBox1, this.btnCancel, this.btnOK}); this.Name = "FormEvents"; this.Text = "FormEvents"; this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new FormEvents()); } private void btnOK_OnClick(object sender, System.EventArgs e) { MessageBox.Show ("OK button clicked", "Events"); } private void btnCancel_OnClick(object sender, System.EventArgs e) { MessageBox.Show ("Cancel button clicked", "Events"); } private void OnMouseEnter(object sender, System.EventArgs e) { // Test whether the sender is a button if (sender is Button) { // Change the OK button style to Flat, but the // change the background color of the Cancel button if (sender == btnOK) ((Button) sender).FlatStyle = FlatStyle.Flat; else if (sender == btnCancel) ((Button) sender).BackColor = SystemColors.ControlLightLight; return; } // Add code here to handle MouseEnter event for other controls } private void OnMouseLeave(object sender, System.EventArgs e) { // Test whether the sender is a button if (sender is Button) { // Change the OK button style to Flat, but the // change the background color of the Cancel button if (sender == btnOK) ((Button) sender).FlatStyle = FlatStyle.Standard; else if (sender == btnCancel) ((Button) sender).BackColor = SystemColors.Control; return; } // Add code here to handle MouseLeave event for other controls } } }
A message box
/* C# Programming Tips & Techniques by Charles Wright, Kris Jamsa Publisher: Osborne/McGraw-Hill (December 28, 2001) ISBN: 0072193794 */ namespace nsFirst { using System; using System.Windows.Forms; public class MessageBoxShowExclamation { static public void Main() { MessageBox.Show ("Hello, C# World!", "Howdy", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } }