Your First Windows Forms Data Application in VS .NET

Encoder ANSI/AIM Code 128 in VS .NET Your First Windows Forms Data Application
Your First Windows Forms Data Application
Print Code 128 Code Set C In .NET Framework
Using Barcode generator for Visual Studio .NET Control to generate, create Code 128 Code Set C image in Visual Studio .NET applications.
This first application will take you a small step beyond a "Hello World" application; you'll create a very simple application that loads some data and displays it in a grid I explain the steps to create the application from a pure code perspective, and in the next section you will repeat the creation of the application using Visual Studio This way you can get a sense of both what is going on behind the scenes and how to do things the quick and easy way with the Visual Studio designer Then we will dig into what is going on under the covers so that you understand where all the functionality in the Windows Forms Framework comes from Okay, let's get started with the pure code approach
Code 128 Code Set A Reader In .NET
Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in Visual Studio .NET applications.
Open a text file and name itDataAppFormcs
Barcode Generator In .NET
Using Barcode drawer for .NET framework Control to generate, create bar code image in VS .NET applications.
Declare a class that derives from SystemWindowsFormsForm :
Recognizing Bar Code In VS .NET
Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications.
using System; using SystemWindowsForms; public class DataAppForm : Form { }
Code-128 Maker In Visual C#
Using Barcode generation for Visual Studio .NET Control to generate, create Code 128 Code Set C image in .NET framework applications.
This code includes the using statement for the SystemWindowsForms namespace, which is where the Form andApplication classes are defined, as well as the controls that you will be adding shortly
Code 128A Encoder In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create Code 128 image in ASP.NET applications.
You also need a class that will contain the start-up code for the program For this simple example, you can place this code in the same file:
Making Code 128 Code Set C In VB.NET
Using Barcode printer for VS .NET Control to generate, create Code-128 image in .NET applications.
static class Program { [STAThread] static void Main() { ApplicationEnableVisualStyles(); ApplicationRun(new DataAppForm()); } }
Painting ECC200 In Visual Studio .NET
Using Barcode creator for Visual Studio .NET Control to generate, create ECC200 image in .NET framework applications.
The Main method is the entry point for any NET application In Visual StudioNET 2003, this was always placed in the first Form class added to a project In Visual Studio 2005, the Program class is added to the project as a separate code file so that the Main method doesn't get lost if the first form is deleted from the project We will mirror that approach here, but just put it in the same file for simplicity The STAThread attribute is needed if the application will use any ActiveX controls, the Clipboard, or any single-threaded apartment COM objects, so it is added by default to any Visual Studio Windows application projects, and in general, you always need it there You need to bring in the System namespace with a using statement to make that attribute type available
Code 128 Code Set A Maker In VS .NET
Using Barcode printer for .NET framework Control to generate, create Code 128 Code Set A image in VS .NET applications.
This document was created by an unregistered ChmMagic, please go to http://wwwbisentercom to register it Thanks
Make Bar Code In .NET Framework
Using Barcode creation for .NET framework Control to generate, create barcode image in VS .NET applications.
4 To compile this file using the C# command line compiler, open a Visual Studio command prompt (located in the Start menu under the Microsoft Visual Studio grouping, Visual Studio Tools submenu), switch to the folder where the file resides, and type the command:
Draw Bar Code In VS .NET
Using Barcode drawer for Visual Studio .NET Control to generate, create bar code image in VS .NET applications.
csc DataAppFormcs
USPS PLANET Barcode Creation In .NET Framework
Using Barcode maker for Visual Studio .NET Control to generate, create USPS Confirm Service Barcode image in .NET framework applications.
Note that you need to have the environment variables set so that Windows can find the command line compiler (cscexe) if you don't use the Visual Studio command prompt You should now have an executable named DataAppFormexe that you can run If you do so, you will get a blank, square window
Scanning Data Matrix In .NET Framework
Using Barcode reader for Visual Studio .NET Control to read, scan read, scan image in .NET framework applications.
That's a little boring, so let's add a grid and a button to the form Listing C1 shows the changes (in bold) to the class that you need to add the button and grid and to position them reasonably
Bar Code Printer In VB.NET
Using Barcode generation for .NET framework Control to generate, create barcode image in VS .NET applications.
Listing C1 Adding a Button and Grid to the Form
Scan USS Code 39 In .NET
Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications.
using System; using SystemWindowsForms;
Print UPC Code In Java
Using Barcode maker for Java Control to generate, create Universal Product Code version A image in Java applications.
using SystemDrawing;
Generate Barcode In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create bar code image in ASP.NET applications.
public class DataAppForm : Form {
Barcode Creation In Visual Studio .NET
Using Barcode creator for ASP.NET Control to generate, create barcode image in ASP.NET applications.
Button m_LoadButton; DataGridView m_CustomersGrid;
Data Matrix 2d Barcode Drawer In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create ECC200 image in ASP.NET applications.
public DataAppForm() {
Barcode Creator In Java
Using Barcode encoder for Java Control to generate, create barcode image in Java applications.
// Change the form size Size = new Size(500,400); // Set the button properties m_LoadButton = new Button(); m_LoadButtonLocation = new Point(10,10); m_LoadButtonText = "Load"; // Set the grid properties m_CustomersGrid = new DataGridView(); m_CustomersGridLocation = new Point(10,50); m_CustomersGridSize = new Size(480,250); // Add the controls to the child controls collection thisControlsAdd(m_LoadButton); thisControlsAdd(m_CustomersGrid);
The code in Listing C1 is a little more involved, so let's walk through it First you need an additional namespace, SystemDrawing, for the Size and Point classes you use to specify the size and location of the controls You then need to declare class members for each control that you are going to add to the form as a child control In this case, you add a Button and a DataGridView member, which are defined as
This document was created by an unregistered ChmMagic, please go to http://wwwbisentercom to register it Thanks
control classes in the SystemWindowsForms namespace Then, in the constructor for the class, you create the instances of those controls, set the appropriate properties for them, and then add them to the child controls collection that form maintains For the form, you change its size by accessing the Size property of the base Form class and setting it to 500 x 400 pixels Next you create an instance of the Button class and set its location and text The location you specify is relative to the upper left portion of the form's client area, with positive x-axis values increasing to the right and positive y-axis values increasing down toward the bottom of the screen The client area starts just under the title bar and inside the border that is drawn around the window The text is what will be presented on the button's face when rendered on the form The default size will be fine for this button because the text length is short If you need to specify longer strings for the button text, you will need to adjust the size of the button to an appropriate width to fit the text, or better yet, set the AutoSize property to TRue For the grid, you create an instance of it and specify both the size and location to get it positioned underneath the button but within the bounds of the form There are actually a lot of different options for laying out controls on a form (they will be covered later in this appendix), and the grid itself is a very complex type But it is well designed so that for simple usage, you only need a few lines of code Once you have all the controls created and their properties set, you add them to the form's Controls collection using the Add method from the collection class This makes them child controls on the form When the form is rendered, it will walk through the list of child controls, asking each to paint itself on the form using its properties and built-in painting behavior Each control encapsulates its own state and behavior, both in the form in the way it paints itself, and in terms of what events it will fire based on user or system interaction When you compile and run this application again from the command line, you will now get a larger form with a button and empty grid on the form's surface For the final modification of this sample, you need to add an event handler for the button Click event and load some data in that handler and bind it to the grid In the constructor, add the following line of code to wire up a method named OnLoadData as an event handler for theClick event of the m_LoadButton button:
m_LoadButtonClick += new EventHandler(OnLoadData);
Then add the corresponding handler method to the class:
public void OnLoadData(object sender, EventArgs e) { DataSet ds = new DataSet(); dsReadXml("CustomersDataSetxml"); m_CustomersGridDataSource = dsTables[0]; }
Finally, you will need to add one more using statement at the top to bring in the SystemData namespace:
using SystemData;
The OnLoadData handler method has a signature (parameter and return types) defined by theEventHandler delegate that is used by many Windows Forms control events In this example you can ignore the arguments that are passed to the method, but the method still must declare those arguments to match the event's delegate type definition You can use whatever method name you like for the event handler, but I suggest you have a consistent, easy-to-read convention for what you name your event handlers I usually name them with an On<action> convention as in this example You will often create these handlers through the designer, and it applies a slightly different naming convention, as shown in the next section, so I usually set my event handler names using the Properties window to get the method names I want The code in the event handler creates a data set object and loads some data into it using the ReadXml method as shown in Appendix D The source XML can be any valid XML that is suitable for loading into a data set with at least one table of data in it This case uses the
This document was created by an unregistered ChmMagic, please go to http://wwwbisentercom to register it Thanks same simplified version of the Customers data set XML that is shown in the Loading Data Sets from a File section in Appendix D Once the data set has been populated, you bind the first table (index zero) in the data set to the grid using the grid's DataSource property The chapters in this book go into more detail on the mechanics of the data-binding process, but for the purposes of this appendix, suffice it to say that when you set the grid's DataSource property to a data table, the grid will extract the schema information and create a column for each column in the table, and then will iterate through each row in the table and add a corresponding row to the grid containing the data To get the sample running, place the XML file in the same directory as the DataAppFormcs file and compile at the command line as described before Then run the resulting DataAppFormexe file and click the Load button, and presto, you have your first running data-bound Windows Forms application There are a couple of key things to notice about the code you have written so far The first is that it is just code There is no magic being done on your behalf by an integrated development environment You just need a text file containing a few lines of source, an XML file containing some data, and a command line compiler to have a fairly rich presentation of bound data Another thing to notice is that if you had a bunch of controls on a form, the code for initializing the properties could become very tedious very quickly Luckily the Windows Forms designer of Visual Studio will write all this code for you, and Visual Studio gives a much more intuitive environment for visually laying out the controls on a form rather than trying to figure out what code to write yourself So you will rarely hand-code that initialization code for the controls as you have done here Finally, the complexity of data binding and rendering the data in the form is completely encapsulated for you in the grid control, and happens just from setting the DataSource property on the control to a valid source of data
This document was created by an unregistered ChmMagic, please go to http://wwwbisentercom to register it Thanks