Winforms.Pdf
Total Page:16
File Type:pdf, Size:1020Kb
winforms #winforms Table of Contents About 1 Chapter 1: Getting started with winforms 2 Remarks 2 See also: 2 Examples 2 Creating a Simple WinForms Application using Visual Studio 2 Create Windows Forms Project 2 Add Controls to the Form 3 Write Code 4 Run and Test 5 Creating a Simple C# WinForms Application using a Text Editor 5 Creating a Simple VB.NET WinForms Application using a Text Editor 6 Chapter 2: Basic controls 9 Examples 9 Button 9 TextBox 9 ComboBox 10 CheckBox 11 ListBox 12 NumericUpDown 16 Useful Events 18 Chapter 3: Databinding 19 Parameters 19 Remarks 19 Examples 19 Binding controls to data objects 19 Chapter 4: Help Integration 21 Remarks 21 HelpProvider Component 21 Help Class 21 HelpRequested Event 21 Help Button of Form 21 Help button of MessgeBox and CommonDialogs 21 ToolTip Component 22 Examples 22 Show help file 22 Show Help for MessageBox 22 Show a CHM file and navigate to a keyword (index) 23 Show a CHM file and navigate to a topic 23 Show a CHM file and navigate first help page in table of contents 23 Open default browser and navigate to a URL 23 Perform custom action on when pressing Help Button or F1 Key 23 Show Help for CommonDialogs 24 Handling HelpRequested event of Controls and Form 25 Show Help using Help class 26 Show Help pop-up window 26 Show CHM Help file 26 Show Help Table of Content 26 Show Help for specific Keyword (index) 26 Show Help for specific Topic 27 Show Url 27 Show Help Button on Titlebar of Form 27 Create custom Help button which acts like standard Form HelpButton 27 Handling HelpButtonClicked event of Form 28 Chapter 5: Inheriting Controls 29 Remarks 29 Examples 29 Application wide Settings 29 NumberBox 32 Chapter 6: Showing a form 40 Introduction 40 Examples 40 Show a modeless or a modal form 40 Closing a modeless form 40 Closing a modal form 41 Chapter 7: TextBox 42 Examples 42 Auto completion from a collection of strings 42 Allow only digits in the text 42 How to scroll to the end 42 Adding a Placeholder to textbox 43 Credits 44 About You can share this PDF with anyone you feel could benefit from it, downloaded the latest version from: winforms It is an unofficial and free winforms ebook created for educational purposes. All the content is extracted from Stack Overflow Documentation, which is written by many hardworking individuals at Stack Overflow. It is neither affiliated with Stack Overflow nor official winforms. The content is released under Creative Commons BY-SA, and the list of contributors to each chapter are provided in the credits section at the end of this book. Images may be copyright of their respective owners unless otherwise specified. All trademarks and registered trademarks are the property of their respective company owners. Use the content presented in this book at your own risk; it is not guaranteed to be correct nor accurate, please send your feedback and corrections to [email protected] https://riptutorial.com/ 1 Chapter 1: Getting started with winforms Remarks Windows Forms ("WinForms" for short) is a GUI class library included with the .NET Framework. It is a sophisticated object-oriented wrapper around the Win32 API, allowing the development of Windows desktop and mobile applications that target the .NET Framework. WinForms is primarily event-driven. An application consists of multiple forms (displayed as windows on the screen), which contain controls (labels, buttons, textboxes, lists, etc.) that the user interacts with directly. In response to user interaction, these controls raise events that can be handled by the program to perform tasks. Like in Windows, everything in WinForms is a control, which is itself a type of window. The base Control class provides basic functionality, including properties for setting text, location, size, and color, as well as a common set of events that can be handled. All controls derive from the Control class, adding additional features. Some controls can host other controls, either for reusability (Form , UserControl) or layout (TableLayoutPanel, FlowLayoutPanel). WinForms has been supported since the original version of the .NET Framework (v1.0), and is still available in modern versions (v4.5). However, it is no longer under active development, and no new features are being added. According to 9 Microsoft developers at the Build 2014 conference: Windows Forms is continuing to be supported, but in maintenance mode. They will fix bugs as they are discovered, but new functionality is off the table. The cross-platform, open-source Mono library provides a basic implementation of Windows Forms, supporting all of the features that Microsoft's implementation did as of .NET 2.0. However, WinForms is not actively developed on Mono and a complete implementation is considered impossible, given how inextricably linked the framework is with the native Windows API (which is unavailable in other platforms). See also: • Windows Forms documentation on MSDN Examples Creating a Simple WinForms Application using Visual Studio This example will show you how to create a Windows Forms Application project in Visual Studio. Create Windows Forms Project 1. Start Visual Studio. https://riptutorial.com/ 2 2. On the File menu, point to New, and then select Project. The New Project dialog box appears. 3. In the Installed Templates pane, select "Visual C#" or "Visual Basic". 4. Above the middle pane, you can select the target framework from the drop-down list. 5. In the middle pane, select the Windows Forms Application template. 6. In the Name text box, type a name for the project. 7. In the Location text box, choose a folder to save the project. 8. Click OK. 9. The Windows Forms Designer opens and displays Form1 of the project. Add Controls to the Form 1. From the Toolbox palette, drag a Button control onto the form. 2. Click the button to select it. In the Properties window, set the Text property to Say Hello. https://riptutorial.com/ 3 Write Code 1. Double-click the button to add an event handler for the Click event. The Code Editor will open with the insertion point placed within the event handler function. 2. Type the following code: C# MessageBox.Show("Hello, World!"); VB.NET MessageBox.Show("Hello, World!") https://riptutorial.com/ 4 Run and Test 1. Press F5 to run the application. 2. When your application is running, click the button to see the "Hello, World!" message. 3. Close the form to return to Visual Studio. Creating a Simple C# WinForms Application using a Text Editor 1. Open a text editor (like Notepad), and type the code below: using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace SampleApp { public class MainForm : Form { private Button btnHello; // The form's constructor: this initializes the form and its controls. public MainForm() { // Set the form's caption, which will appear in the title bar. this.Text = "MainForm"; // Create a button control and set its properties. btnHello = new Button(); btnHello.Location = new Point(89, 12); https://riptutorial.com/ 5 btnHello.Name = "btnHello"; btnHello.Size = new Size(105, 30); btnHello.Text = "Say Hello"; // Wire up an event handler to the button's "Click" event // (see the code in the btnHello_Click function below). btnHello.Click += new EventHandler(btnHello_Click); // Add the button to the form's control collection, // so that it will appear on the form. this.Controls.Add(btnHello); } // When the button is clicked, display a message. private void btnHello_Click(object sender, EventArgs e) { MessageBox.Show("Hello, World!"); } // This is the main entry point for the application. // All C# applications have one and only one of these methods. [STAThread] static void Main() { Application.EnableVisualStyles(); Application.Run(new MainForm()); } } } 2. Save the file to a path you have read/write access to. It is conventional to name the file after the class that it contains—for example, X:\MainForm.cs. 3. Run the C# compiler from the command line, passing the path to the code file as an argument: %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\csc.exe /target:winexe "X:\MainForm.cs" Note: To use a version of the C# compiler for other .NET framework versions, take a look in the path, %WINDIR%\Microsoft.NET and modify the example above accordingly. For more information on compiling C# applications, see Compile and run your first C# program. 4. After compilation has completed, an application called MainForm.exe will be created in the same directory as your code file. You can run this application either from the command line or by double-clicking on it in Explorer. Creating a Simple VB.NET WinForms Application using a Text Editor 1. Open a text editor (like Notepad), and type the code below: Imports System.ComponentModel Imports System.Drawing Imports System.Windows.Forms https://riptutorial.com/ 6 Namespace SampleApp Public Class MainForm : Inherits Form Private btnHello As Button ' The form's constructor: this initializes the form and its controls. Public Sub New() ' Set the form's caption, which will appear in the title bar. Me.Text = "MainForm" ' Create a button control and set its properties. btnHello = New Button() btnHello.Location = New Point(89, 12) btnHello.Name = "btnHello" btnHello.Size = New Size(105, 30) btnHello.Text = "Say Hello" ' Wire up an event handler to the button's "Click" event ' (see the code in the btnHello_Click function below). AddHandler btnHello.Click, New EventHandler(AddressOf btnHello_Click) ' Add the button to the form's control collection, ' so that it will appear on the form. Me.Controls.Add(btnHello) End Sub ' When the button is clicked, display a message. Private Sub btnHello_Click(sender As Object, e As EventArgs) MessageBox.Show("Hello, World!") End Sub ' This is the main entry point for the application.