Skip to content

Instantly share code, notes, and snippets.

Created September 1, 2017 12:09
Show Gist options
  • Save anonymous/64eb3f23c5edd54a3563cd73bd57f9b5 to your computer and use it in GitHub Desktop.
Save anonymous/64eb3f23c5edd54a3563cd73bd57f9b5 to your computer and use it in GitHub Desktop.
Таблица из usercontrol c

Таблица из usercontrol c - C# Corner


Таблица из usercontrol c



UserControl Class
Использовать ли UserControl в WPF MVVM?
Create a Windows Forms user control that supports simple data binding
how to access the textbox of dynamically created table in user control page?
Creating a Windows Form User Control
UserControl Class













Describes how to create a Windows Form User Control as well as useful enumerations, error messages, and verification. You will also learn how to add descriptions to your properties and methods using attributes. NET controls are all inherited from a common base class called UserControl. This class has all the basic functionality for a graphical control that will be used on a Windows Form. All of the built-in. NET controls inherit from this same base class. You can inherit from this same base class as well, and create your own controls. When you inherit from the UserControl class, you will be automatically supplied with certain properties and events. There are about 30 properties that you will inherit from the UserControl base class. These properties include all of the common properties you see on normal controls, such as the accessibility properties, backcolor , forecolor , anchoring , and docking. You can override these properties if you need to, but you will probably leave most of them as they are. A user control that you create inherits about 40 events from the UserControl base class. These events are the ones that are most common, such as Load , Resize , and Layout events. Your control will also include some of the low-level mouse and keyboard events, such as Click , DoubleClick , MouseUp , and MouseDown. There are many reasons why you might want to create your own controls even though Microsoft supplies a pretty rich set of controls right out of the box. One of the most common uses that you will find for creating your own controls is for individualizing input masks and business rules for specific types of identifiers used in your applications across your enterprise. A good example would be an employee number that is formatted in a special way and must be verified against a table in a database to make sure it exists. Instead of having each programmer create all of this logic from scratch, you might create a control that will perform the logic for validating the correct format and verifying its existence in the database. It will also check to make sure that the employee number exists in a database table. The EmployeeNumber control that you create will allow a user to input a number in the format AA-NNN. You must input two letters, followed by a hyphen, followed by three numbers. Figure 1 shows how this control might look. The control is made up of a label with the text "Employee Number" and a text box that can be filled in with an employee number. In addition to the built-in properties and events that are exposed automatically by the UserControl class, you add on properties, events, and methods to this user control. There are six properties for which you will need to write appropriate Property statements. The Text property will be a wrapper around the Text property on the TextBox on the User Control. You will also create additional properties to hold employee data that is returned from a database table. These properties are EmpID , LastName , FirstName , and Salary , and they are all created as wrappers around private variables. These properties will be filled in only after you make a call to the EmployeeIsValid method which will be discussed in the next section. One last property you will need is ConnectionString. This property will be used to make a connection to the appropriate data source to retrieve the employee information from the employee table. You need to create a method that can be called to retrieve information from a database table called Employees. You will name this method EmployeeIsValid. This optional method can be called by the user to validate that the employee number is contained within the Employees table. This method returns a True or False response based on whether or not the value typed into the control matches the employee number in the Employees table. If the employee number is found in the Employees table, the four employee properties are populated from other columns in this table. The BadEmployeeNumber event is raised when the user tries to move off the Employee Number control and the input format of the data is incorrect for some reason. If the user moves off the control and the data is correct, no event will be raised. There are many reasons why the format could be incorrect. These reasons will be enumerated within a class that is returned as an EmpNumEventArgs object. You will learn to build this class later in this document. You will create a class within your user control called EmpNumEventArgs. This class inherits from System. EventArgs and can thus be passed as the second argument to an event procedure. You will add two properties to this new class, Message and ErrorNumber , and you will use this class when you raise the BadEmployeeNumber event from your user control. The idea is to identify what is wrong with the data the user entered, assign a description of what is wrong to the Message property and an error number to the ErrorNumber property, and then send this object as the second parameter to the BadEmployeeNumber event. Instead of just arbitrarily assigning some error number to the ErrorNumber property of the EmpNumEventArgs , it is a good idea to create an enumerated type that has a list of the possible error numbers that could be generated. You will create an enumerated type named EmpNumErrors. This enumeration will be created with five values, NoError , NotLongEnough , LetterMissing , DashMissing , NumberMissing. The following section describes the reasons why the error number format could be wrong. To create this Employee Number User Control, you will need to add a label and text box to the User Control design surface. Add the controls and set the properties as listed in Table 1. When you are finished, you should have something that looks like Figure 2. Create the Employee Number User Control using one Label and one Text Box control. Controls and properties to create and set in building the Employee User Control. You will now build some additional properties that allow you to control the look of this User Control. You will create the following properties in this control:. You create the first five properties EmpID , LastName , FirstName , Salary , and ConnectionString using private member variables and property statements. The Text property is simply a property statement wrapped around the Text property of the txtEmpNum text box control. The steps are as follows:. Notice that the Text property must use the Overrides attribute because the default Text property on the UserControl class is used to display a title on the User Control. If the user types in an incorrect employee number format, you will want to raise an event back to the client application so that you can inform the user of the problem with the format. To raise an event from a control, you must first declare the event within the User Control. Use the Event declaration followed by the name of the event. You will also need to pass in the standard parameters you find on all event procedures— sender and e. The sender parameter is declared as an object data type. The e parameter must be declared as a System. EventArgs type, or some derived class from the System. In this case, you will want to create your own class that inherits from the System. EventArgs class because you want to add an error number property and a message property. These two properties allow you to fill in additional information about how the employee number is not in the correct format. Below is the declaration that you will need to write somewhere near the top of the EmployeeNumber User Control class. The EmpNumEventArgs class must be created prior to creating this event declaration. Before you can create the EmpNumEventArgs class, you first need to create an enumeration of error numbers. The ErrorNumber property within the EmpNumEventArgs class will be defined as this enumeration type. The enumeration you will now create, named EmpNumErrors, has five values declared in it. The declaration for this enumeration is as follows:. Each of these error numbers will be used to convey to the user of this control what the problem is with the employee number that was typed in. Of course there will be a corresponding message filled in with more descriptive information, but having an enumeration allows you to check for a specific error programmatically, and potentially ignore that error as well. For example, you may choose allow a blank employee number. If this is the case when the BadEmployeeNumber event is raised, you could check to see whether the ErrorNumber property contained the NumberMissing value. If so, you might not display the message to the user. Now you need to create the class that will be used to hold the error number and the message. This new class, named EmpNumEventArgs , must first inherit from the System. You must inherit from this class so you can pass this derived object to the event procedure as the second parameter. Other than the INHERITS statement, this class is a fairly straightforward class with just two property declarations: The ErrorNumber property is declared as the type enumerated type EmpNumErrors. You might notice some strange XML-looking stuff in the above class. These are called attributes, and will be covered later in this document. Now that you have created this class to use for the BadEmployeeNumber event procedure, you are ready to write the code that checks for the format of the employee number. Now we will write an event procedure that responds to the Validating event of the txtEmpNum text box. Because the purpose of this control is to allow only a certain format for an employee number, you want to check whether or not the value input is in the correct format when the user attempts to tab out of this control. The event procedure shown below has the code that will verify whether the data input is correct. The code in this event procedure should be very simple to understand. You just check all of the possible error conditions regarding the number input by the user. If a part of the employee number is invalid, you assign an error number to a local variable and an appropriate message to another local variable. At the end of this procedure, you create a new EmpNumEventArgs object, fill in the ErrorNumber and Message properties with the values in the local variables, and then raise the BadEmployeeNumber event, passing in the instance of the User Control and the EmpNumEventArgs object. An additional check you might wish to perform on an employee number is to see whether it exists within a table in a database. The following method shows how you might validate the data within an Employees table, and once you find a valid record, populate the additional properties on this control with other information about the employee. For example, you might fill in the properties from other columns in the table: Of course, this method assumes that you have a table called Employees in a database with the structure, as follows:. In the EmpNumEventArgs class, you created some attributes. They were the XML-like prefixes to the properties that looked like this:. These attributes can be read by any tool that will read meta-data about a class. These descriptions can be handy when you distribute a compiled DLL and you want to convey additional information about your class and properties. The attributes come from the System. ComponentModel NameSpace, which is why you must import that NameSpace within the file where you will use attributes. You can apply attributes to the definition of the User Control class itself. This can give you the option of setting the event to be the default when you double-click on the control. Be sure to either put it on the same line as the Public Class definition, or include the underscore character, as these attributes must be placed on the same line as the class definition. You can assign attributes to properties of the EmployeeNumber class. For example, you can have the attributes of properties such as EmpID, LastName, and FirstName appear in the Properties Window by using the Browsable True attribute. You can assign a category to the property by using the Category "Employee" attribute. You can also put in a description attribute that will show up in the description window below the property window. It is time to test the control that you have created. To do this, you need to close all of the windows for your User Control, build the project, and add a new Windows Application project. You can now run the application and see what happens when you put in both a valid and an invalid number. Next, try out the EmployeeIsValid method on the EmployeeNumber control. Write the following code under the Click event procedure for the Save button. You will need to create the table in SQL Server and add some valid employee information to the table. Then you can click on the Save button to test out this functionality. There are some new techniques, such as using attributes, that make adding comments to your properties and methods a lot easier. Creating properties, methods, and events in Visual Basic. NET is done in the same way as in Visual Basic 6. NET will be very familiar to you. In this document, you learned how to create a User Control that you can use and reuse on any Windows form in any application. To reuse this control in any other project, simply customize the toolbox and point to the DLL that is created. Passing information back in your own custom EventArgs object is an excellent way to convey additional information to the user of this control. Adding attributes to your control will give users better information about how to use your control. Sheriff is the owner of PDSA, Inc. Paul is the MSDN Regional Director for Southern California, is the author of a book on Visual Basic 6. NET, and Web Development for Keystone Learning Systems. Paul has co-authored a book entitled ASP. Visit the PDSA, Inc. Web site for more information. Informant Communications Group, Inc. Specializing in software development publications, conferences, catalog publishing, and Web sites, ICG was founded in With offices in the United States and the United Kingdom, ICG has served as a respected media and marketing content integrator, satisfying the burgeoning appetite of IT professionals for quality technical information. Downloads Visual Studio SDKs Trial software Free downloads Office resources SharePoint Server resources SQL Server Express resources Windows Server resources Programs Subscriptions Overview Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups Events Community Magazine Forums Blogs Channel 9 Documentation APIs and reference Dev centers Samples Retired content. The content you requested has been removed. NET Development Articles and Overviews Upgrading to Microsoft. NET Creating a Windows Form User Control. Creating a Windows Form User Control. NET for the ADO Programmer. Building an N-Tier Application in. NET Component from a COM Component. Calling COM Components from. NET Libraries for Developers. Xml in Visual Studio. XMLDOM in Visual Studio 6. Converting ASP to ASP. Creating Classes in Visual Basic. Data Binding with Windows Forms and ADO. Designing for Web or Desktop? Determining When to Use Windows Installer Versus XCOPY. Differences Between Visual Basic 6. Distributed Transactions in Visual Basic. Error Handling in Visual Basic. Getting Started with Windows Forms. Inheritance from a Base Class in Microsoft. Interacting with Message Queues. NET and Web Forms. Introduction to Visual Studio. Managing Versions of an Application. Migrating from the SOAP Toolkit to Web Services. Overloading Methods in Visual Basic. Raising Events and Responding to Events. Replacing API Calls with. NET Application For Easy Deployment. Understanding and Using Assemblies and Namespaces in. Using ActiveX Controls with Windows Forms in Visual Studio. Using Web Services Instead of DCOM. Variable and Method Scope in Microsoft. Working with MDI Applications and Creating Menus. Collapse the table of content. This documentation is archived and is not being maintained. Inherited Properties There are about 30 properties that you will inherit from the UserControl base class. Inherited Events A user control that you create inherits about 40 events from the UserControl base class. Creating Your Own Controls vs. Using Microsoft Controls There are many reasons why you might want to create your own controls even though Microsoft supplies a pretty rich set of controls right out of the box. Overview of the Employee Number Control The EmployeeNumber control that you create will allow a user to input a number in the format AA-NNN. An employee number control on a Windows Form Description of the Employee Number User Control In addition to the built-in properties and events that are exposed automatically by the UserControl class, you add on properties, events, and methods to this user control. Properties There are six properties for which you will need to write appropriate Property statements. Methods You need to create a method that can be called to retrieve information from a database table called Employees. Events The BadEmployeeNumber event is raised when the user tries to move off the Employee Number control and the input format of the data is incorrect for some reason. Classes You will create a class within your user control called EmpNumEventArgs. Enumerations Instead of just arbitrarily assigning some error number to the ErrorNumber property of the EmpNumEventArgs , it is a good idea to create an enumerated type that has a list of the possible error numbers that could be generated. Create the User Control To create any User Control, you follow a series of steps. The major steps are as follows. Create a new Windows Control Library project. Draw the user interface by selecting other controls from the toolbox that define how you want your control to work. Create any additional properties. Create any events that you want. Write any event procedures for constituent controls. Create any methods that you want. Build the control project. Add a new Windows Application project so you can test your control. Choose the Windows Control Library template. Set the Name of the project to PKUserControls. You should now have a User Control design surface on which you can draw your user interface. Create the User Interface To create this Employee Number User Control, you will need to add a label and text box to the User Control design surface. ComponentModel Public Class EmployeeNumber Inherits System. Public Overrides Property Text As String Get Return txtEmpNum. Text End Get Set ByVal Value As String txtEmpNum. Public Class EmpNumEventArgs Inherits System. Public Function EmployeeIsValid As Boolean Dim oCmd As OleDb. OleDbCommand Dim oDR As OleDb. CloseConnection End With If oDR. Show "Valid Number for: Show "Invalid Employee Number" End If Catch oException As Exception MessageBox. Message End Try End Sub. Is this page helpful? We appreciate your feedback. Dev centers Windows Office Visual Studio Microsoft Azure More Learning resources Microsoft Virtual Academy Channel 9 MSDN Magazine. Community Forums Blogs Codeplex. Programs BizSpark for startups Microsoft Imagine for students.


Как сделать ремонт в ванной женщине
Образец заполнения енвдза 1 квартал 2017
Каталог автомобилей субару
Продать аккаунт game of war
Как сделать кондиционер на воздухе
Описание сорта томатов кардинал
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment