Home > Articles > Programming > Windows Programming

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Understanding Project Files

Each Visual Basic project is composed of several code files. Some of them are by default visible to the developer and are the ones that you need to edit to create your application. There are also some other files (which are hidden by default but that can be made visible manually) that we can consider as support files. To understand what kind of support these files offer, we have to think that most of the settings that we can provide to our applications via the My Project window are represented with Visual Basic code. Particularly, Visual Basic translates into code the content of the Application, Resources, Settings and My Extensions tabs. In this chapter you get a detailed description of files that represent the Application tab in My Project and then you get an overview of files that represent other tabs. Although you seldom edit these code files manually, because all of them have design time support from My Project (as detailed in Chapter 20, "The 'My' Namespace," when describing the My namespace), there could be some situations in which you need to manually edit them, so it's important to know something about them. Before going on, you need to click the Show All Files button in Solution Explorer. This gives visibility to several code files that are hidden by default and that provide the main infrastructure for each Visual Basic project.

Dissecting My Project

In Chapter 2 we introduced the My Project window and saw how it offers graphical tools for specifying some settings when developing applications, such as application information and compile options. Understanding My Project is important because it also provides the infrastructure of the My namespace, offering the ability for specifying important settings that are discussed in Chapter 20. For now, we need to know that My Project offers a graphical representation of information that is stored in some code files. In Solution Explorer you can notice an element named My Project. When you double-click this element, you are redirected to the My Project window. But when you enable the All Files view, you notice how the My Project element becomes a folder that can be expanded. Within this folder (which is physically stored inside the project's folder and contains all files described in this section), you can notice the presence of several files packaged into the assembly's metadata when you build the project. We now describe such files and how they work.

Application.MyApp

The Application.myapp file is an XML representation of the project's main properties. Listing 3.1 shows the content of this file as it becomes available when you create a new Console application.

Listing 3.1. The Content of Application.myapp

<?xml version="1.0" encoding="utf-8"?>
<MyApplicationData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <MySubMain>false</MySubMain>
  <SingleInstance>false</SingleInstance>
  <ShutdownMode>0</ShutdownMode>
  <EnableVisualStyles>true</EnableVisualStyles>
  <AuthenticationMode>0</AuthenticationMode>
  <ApplicationType>2</ApplicationType>
  <SaveMySettingsOnExit>true</SaveMySettingsOnExit>
</MyApplicationData>

XML elements in this file are self-explanatory, and you may notice how each of them represents a particular item on the Application tab. The Application.myapp file is the brother of another file named Application.Designer.vb. Such a file basically stores information related to Windows Forms applications such as the authentication mode and the shutdown mode. It is the complement for those application options that you can see in the Windows Application Framework Properties group in the Application tab. Listing 3.2 shows the content of the Application.Designer.vb as it is generated for a Windows Forms application.

Listing 3.2. The Content of Application.Designer.vb

Partial Friend Class MyApplication

        Public Sub New()
            MyBase.New(Global.Microsoft.VisualBasic.ApplicationServices.
    AuthenticationMode.Windows)
            Me.IsSingleInstance = false
            Me.EnableVisualStyles = true
            Me.SaveMySettingsOnExit = true
            Me.ShutDownStyle = Global.Microsoft.VisualBasic.ApplicationServices.
    ShutdownMode.AfterMainFormCloses
        End Sub

        Protected Overrides Sub OnCreateMainForm()
            Me.MainForm = Global.WindowsApplication1.Form1
        End Sub
End Class

For the sake of simplicity, in the preceding code some attributes are omitted that Visual Studio adds to class members and that are related to the debugger interaction. As you can see examining Listing 3.2, items in the Application tab of My Project have been mapped to Visual Basic properties. The Me identifier represents the instance of the current application. The OnCreateMainForm method establishes which window must be the startup one. In this case Form1 is the default name that Visual Studio assigns to the main window when a new project is created. If you also examine the code inside the IDE, you can notice how there are some comments in the code that advise that the code itself is auto-generated and that you should not edit it manually, because you can use the My Project designer that will automatically map changes to the Visual Basic code. You might need to set custom actions for application events (such as Startup or Shutdown, which are usually handled in the ApplicationEvents.vb file) and Application.designer.vb is the right place.

AssemblyInfo.vb

In Chapter 2 we discussed the Assembly Information dialog, describing how it is used for specifying information about applications. All the information is stored in a file named AssemblyInfo.vb. Listing 3.3 shows the content of this file as it is available when you create a new project.

Listing 3.3. AssemblyInfo.vb Content

Imports System
Imports System.Reflection
Imports System.Runtime.InteropServices

' General Information about an assembly is controlled through the following
' set of attributes. Change these attribute values to modify the information
' associated with an assembly.

' Review the values of the assembly attributes

<Assembly: AssemblyTitle("WindowsApplication1")>
<Assembly: AssemblyDescription("")>
<Assembly: AssemblyCompany("")>
<Assembly: AssemblyProduct("WindowsApplication1")>
<Assembly: AssemblyCopyright("Copyright ©  2009")>
<Assembly: AssemblyTrademark("")>

<Assembly: ComVisible(False)>

'The following GUID is for the ID of the typelib if this project is exposed to COM
<Assembly: Guid("5572d199-a7ca-48c3-98d3-56533cd6ba86")>

' Version information for an assembly consists of the following four values:
'
'      Major Version
'      Minor Version
'      Build Number
'      Revision
'
' You can specify all the values or you can default the Build and Revision Numbers
' by using the '*' as shown below:
' <Assembly: AssemblyVersion("1.0.*")>

<Assembly: AssemblyVersion("1.0.0.0")>
<Assembly: AssemblyFileVersion("1.0.0.0")>

As you can notice examining the Visual Basic code shown in Listing 3.3, there are several items whose identifier begins with the word Assembly, such as AssemblyTitle, AssemblyCompany, and so on. Each item is in relationship with fields of the Assembly Information dialog. Moreover such items are marked with an attribute named Assembly. Attributes are discussed in Chapter 48. The reason why it is useful knowing about the above file is that there are situations in which you need to edit this file manually. Examples are localization of WPF applications or marking an assembly as compliant to Microsoft Common Language Specifications.

Resources and the Resources.resx File

Visual Studio 2010 enables defining resources that you can embed in your assembly's metadata and use within your applications. Resources can include strings, icons, picture files, audio files, and so on. My Project offers a tab named Resources that provides a visual way for defining project level resources.

Figure 3.1 shows the Resources designer with the definition of a String resource named TextMessage that has a value and a description. We revisit the Resources tab in Chapter 20, where we discuss My namespace, but if you are curious, you can play with the designer to see what kind of resources you can add.

Figure 3.1

Figure 3.1 The My Resources designer.

Resources are supported by two files stored inside the My Project folder: Resources.resx and Resources.designer.vb. The first one is basically an XML schema used by Visual Studio for working with resources. Listing 3.4 shows the content of the schema.

Listing 3.4. The Content of Resources.resx

<root>
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0"
msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0"
msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required"
msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0"
msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms,
     Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Ver-
sion=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="TextMessage" xml:space="preserve">
    <value>Hello Visual Basic 2010!</value>
    <comment>This is a test string</comment>
  </data>
</root>

This schema establishes how a resource is defined, with names, values, comments, and a MIME type that identifies the file type. At the end of the XML markup code, you can see how resources are stored. Continuing with our example, you can see the name of the resource (inside the data element), its value, and the description we provided via the designer. This schema is used by Visual Studio for design time purposes. To work with resources in our applications, Visual Studio also needs to provide Visual Basic code support for resources. Such support is provided by a code file named Resources.designer.vb. This file handles a reference to a .NET object called ResourceManager that is responsible for managing resources in code. Listing 3.5 shows the content of Resources.designer.vb. (For the sake of simplicity, auto-generated attributes are not covered here.)

Listing 3.5. Content of Resources.designer.vb

Friend Module Resources

        Private resourceMan As Global.System.Resources.ResourceManager

        Private resourceCulture As Global.System.Globalization.CultureInfo

        Friend ReadOnly Property ResourceManager() As
     Global.System.Resources.ResourceManager
            Get
                If Object.ReferenceEquals(resourceMan, Nothing) Then
                    Dim temp As Global.System.Resources.ResourceManager =
     New Global.System.Resources.ResourceManager("MyFirst2010Program.Resources",
     GetType(Resources).Assembly)
                   resourceMan = temp
               End If
               Return resourceMan
            End Get
        End Property

        Friend Property Culture() As Global.System.Globalization.CultureInfo
            Get
                Return resourceCulture
            End Get
            Set(ByVal value As Global.System.Globalization.CultureInfo)
                resourceCulture = value
            End Set
        End Property

        Friend ReadOnly Property TextMessage() As String
            Get
                Return ResourceManager.GetString("TextMessage", resourceCulture)
            End Get
        End Property
    End Module

At this point in the book you don't effectively need to know what each type used in code refers to, whereas it is useful to know the existence of the ResourceManager property that points to the project resources. (See the declaration of the temp variable.) This handles a reference to the application-level ResourceManager that enables access to resources. There is another property named Culture that is of type System.Globalization.CultureInfo. This property sets or returns the current localization for resources. The last property in the code is named TextMessage and is the Visual Basic representation of the string resource defined in My Project. This is a read-only property, because you cannot change it in code (you can change it only via designer) and returns a localized version of the resource invoking the GetString method of the ResourceManager class. GetString requires an object of type CultureInfo (in our code it's resourceCulture) that represents the culture that the resource must be localized to. The following line of code shows how you can access the preceding defined resource, which is discussed further in Chapter 20:

Dim myString As String = My.Resources.TextMessage

When you access resources, as shown in Chapter 20, you do not need to manually invoke this background code, but you need to know how it is structured to better understand what's happening behind the scenes. Resources are not the only feature in My Project that is supported by Visual Basic code for design time features. Settings are another one of these features.

Application Settings

Settings in Visual Basic development are particular objects that provide a managed way for manipulating applications and user level settings. For example, you could provide users with the ability of customizing options in the user interface of your application. To save and read such customizations to and from disk, you can use .NET Settings. My Project provides a tab named Settings that enables specifying information at the application or user level. Figure 3.2 shows an example.

Figure 3.2

Figure 3.2 Settings Tab in My Project.

As you can see in Figure 3.2, you can specify an identifier for each setting, a type (which you can understand better by reading Chapter 4), the scope, and the value. For the scope, User means that only the user that runs the application can use that setting. Application means that the setting is available at the application level, independently from the user that logged into Windows (and therefore is available to all users). As with Resources, Settings will be also described in detail in Chapter 20. Settings are represented by a simple XML file, named Settings.settings. Listing 3.6 shows the content of Settings.settings after the addition of the sample setting.

Listing 3.6. Settings.settings Content

<?xml version='1.0' encoding='utf-8'?>
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings"
CurrentProfile="(Default)" GeneratedClassNamespace="My" GeneratedClass-
Name="MySettings" UseMySettingsClassName="true">
  <Profiles />
  <Settings>
    <Setting Name="StartState" Type="System.Boolean" Scope="User">
      <Value Profile="(Default)">True</Value>
    </Setting>
  </Settings>
</SettingsFile>

In the XML markup you can see the presence of a Settings node that stores as many Setting elements and as many settings that you specify in My Project. In our example there is just one Setting element that contains the name of the setting, the data type, the scope, and the default value (which means the value you specify in My Project). The Settings.settings file also has Visual Basic support, which is represented by another file named Settings.designer.vb. We do not need to examine all the content of this file when just a couple of parts of the code can be interesting for us. First, this file implements a property named Settings that is accessible via the My namespace, as detailed in Chapter 20. Listing 3.7 shows the definition of this property.

Listing 3.7. Definition of the Settings Property

   Friend ReadOnly Property Settings() As
Global.MyFirst2010Program.My.MySettings
       Get
          Return Global.MyFirst2010Program.My.MySettings.Default
       End Get
   End Property

The Settings property represents the active instance of the Settings object that you can use in your applications. How the active instance is defined is beyond the scope of this chapter, but now you know that the Settings tab in My Project also has a counterpart in two support files. Just for your convenience, the following line of code shows how you can access settings and how we set them before:

Dim currentValue As Boolean = My.Settings.StartState

The value stored in the StartState setting will be assigned to a variable named currentValue. Examining My namespace in Chapter 20 can clarify the usage of Settings and Resources and of many other interesting features.

  • + Share This
  • 🔖 Save To Your Account

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020