Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘project’

How To Compile VB Programs with Debug Symbols Embedded

Symptoms
The “Compile to Native Code” option introduced in Visual Basic versions 5.0 and 6.0 not only improves performance of Visual Basic programs and components, but also makes it possible to debug them with the Visual C++ debugger.
This article shows you how to compile a Visual Basic program or componentwith embedded debug symbols. By doing so, you simplify the debuggingprocess and avoid the problems associated with mismatched and improperlyplaced symbols.
Resolution
All it takes to compile a Visual Basic program or component with embeddeddebug symbols is a properly set environment variable, LINK. The key is toset it in such a way that Visual Basic inherits the setting when it islaunched. To do this, follow the steps below:
There are several ways to set the LINK environment variable, each of thesemethods is outlined below:
Windows NT or Windows 95/98/MeOpen an MS-DOS Prompt.Navigate to the folder containing Visual Basic.Execute the following command, “set link=/pdb:none” without quotes.Start Visual Basic from the MS-DOS command prompt.
Windows NT OnlyFrom the Control Panel, select the System icon.Select the Environment tab.In the Variable entry, enter “link” without quotes.In the Value entry, enter the following:
/pdb:nonePress Set, then Apply.Start Visual Basic from the MS-DOS command prompt.
Windows 95/98/Me OnlyMake a backup copy of the AutoExec.Bat file.Open the Autoexec.bat file with Notepad.Exe or any text editor.Add the following entry to the AutoExec.Bat file:
set link=/pdb:noneSave the AutoExec.Bat file.Execute the AutoExec.Bat file or reboot the machine.Start Visual Basic.
Once the LINK environment variable is set, a Visual Basic project can becompiled with embedded debug symbols. The following steps describe howto create a test program and compile it with embedded debug symbols:
Step-by-Step ExampleCreate a new Standard EXE project in Visual Basic. Form1 is created bydefault.Add a CommandButton (Command1) to Form1.Add the following code to Form1:

Private Sub Command1_Click()Dim s As Strings = App.EXENamePrint sEnd Sub Select Project1 Properties from the Project menu.Select the Compile tab.Select Compile to Native Code, check Create Symbolic Debug Info, andselect No Optimization.Save the project and create Project1.EXE. Note that the EXE is createdwith embedded debug symbols.

How To Associate a Custom Icon with a Formless Visual Basic Application

Symptoms
For Visual Basic applications that do not contain forms, you can still provide a custom icon for the executable. This article explains how you can add an icon resource to the project.
Resolution
When you create a Visual Basic executable application, you can select an icon from one of the project’s forms in the Icon drop-down list box on the Make tab of the Project Properties dialog box to use as the icon for the executable file. However, if there are no forms in the project, no icons are available in the drop-down list box of the Make tab. In this case, you can create a custom icon resource in your project. The compiler uses this resource as the icon for your executable file. If you include more than one icon, the compiler uses the icon whose letter appears first in the alphabet because it prioritizes alphabetically.
The following sample demonstrates how to provide a custom icon for a simple, formless Visual Basic EXE project. For this sample, you may select an icon from your system, or you can create your own. By default, Visual Basic 6.0 installs icons in the following folder:
C:\Program Files\Microsoft Visual Studio\Common\Graphics\Icons\ To create your own icon, you can use a tool such as Image Editor, which is available from the Visual Basic 6.0 CD (Disk 1) or the Visual Studio 6.0 CD (Disk 3) at the following location:
\Common\Tools\VB\Imagedit\Imagedit.exe
Step-by-Step ExampleCreate a new Visual Basic standard EXE project. Form1 is created by default.Right-click Form1 in the Project window, and then click Remove Form1 to remove the form from the project.Add a new standard module (Module1) to the project.Paste the following code into the code window of Module1:

Public Sub Main()MsgBox “Hello World”End Sub Select an icon from the list, or use Image Editor (or another appropriate tool) to create an icon file.To load the Visual Basic Resource Editor, follow these steps:From the Add-Ins menu, click Add-In Manager.Locate VB 6 Resource Editor in the list of available add-ins.Double-click VB 6 Resource Editor to load the editor add-in.From the Tools menu, click Resource Editor.In the Resource Editor window, click Add Icon on the toolbar. (This button appears as a small gray square that is outlined in blue.)Open the icon file that you created earlier.By default, the icon is added with the name “101″. Right-click the icon resource that was just added, and then click Properties.In the Id box, type APPICON to rename the icon resource, and then click OK.
NOTE: “APPICON” is just a suggested name for your resource. If you already have other, named icon resources, make sure that your executable icon begins with a letter that occurs later in the alphabet than the other icons. For example, if you have an icon that is named “AAA” and another that is named “BBB”, the compiler uses the one that is named “AAA” as your application icon.From the File menu, click Make to compile the Visual Basic project.In Windows Explorer, browse to the location where you compiled your executable file. Notice that the icon for the executable file is the icon that you selected in the Resource Editor.

PRB: Poor Performance with the GoSub Statement

Symptoms
A project compiled in native code shows poor performance. This behavioroccurs because the project contains calls to subroutines using the GoSubstatement.
Resolution
The GoSub statement has not been optimized in Visual Basic.

BUG: The Visual Basic Upgrade Wizard stops responding when you try to upgrade a Visual Basic 6.0 project in Visual Basic 2005

Symptoms
Consider the following scenario. A Microsoft Visual Basic 6.0 project contains at least one code module thathas nine or more Case Is statements. You try to upgrade the Visual Basic 6.0 project by using the Visual Basic Upgrade Wizard in Microsoft Visual Basic 2005. In this scenario, the Visual Basic Upgrade Wizard stops responding (hangs).
Resolution
To work around this problem, follow these steps:Open the Visual Basic 6.0 project in Microsoft Visual Studio 6.0.Comment out anyCase Is statements.Save the code module.Start Visual Basic 2005.On the File menu, click Open Project.In the Open Project dialog box, locate the Visual Basic 6.0 project folder, click the project file name, and then click Open. The Visual Basic Upgrade Wizard starts.Click Next.Under Choose a Project Type, click EXE or DLL, and then click Next.Type a location for the project, and then click Next.If the folder does not exist, click Yes to create the folder.Click Next to start the upgrade.After the upgrade is complete, remove the comments from the Case Is statements.

BUG: Error at run time after you upgrade a Microsoft Visual Basic 6.0 project that uses the ImageList control to Microsoft Visual Basic .NET

Symptoms
When you upgrade a Microsoft Visual Basic 6.0 project that uses the ImageList control to a Microsoft Visual Basic .NET project by using the Microsoft Visual Basic .NET Upgrade Wizard, you receive the following exception when you run the project in Visual Basic .NET:

An unhandled exception of type ‘System.Runtime.InteropServices.COMException’ occurred in axinterop.mscomctllib.dll
Resolution
The Visual Basic .NET Upgrade Wizard wraps the ImageList control in the AxHost class. However, it does not modify the corresponding Visual Basic 6.0 code to access the underlying ImageList control. For example, the following code may be part of the migrated Visual Basic .NET code.

ListView1.ColumnHeaderIcons = ImageList

An “could not copy temporary files to the output directory” error is generated in Visual Studio .NET or in Visual Studio 2005

Symptoms
When you build a project in Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, the build fails and you receive the following compile-time error message:

Could not copy temporary files to the output directory. For more information about another cause for this symptom, click the following article number to view the article in the Microsoft Knowledge Base:
313512?(http://support.microsoft.com/kb/313512/) BUG: “Could not copy temporary files to the output directory” error message when you build a solution that contains multiple projects
Resolution
This behavior may occur for one of the following reasons.
Cause 1You may receive this error message when you compile a component that is currently being used by another process. For example, you may try to compile a class library or a user control project. For example, this behavior may occur when the following conditions are true: Multiple instances of Visual Studio .NET or Microsoft Visual Studio 2005 are open. In one instance, you have a UserControl or class library project. In the other instance, you have a project that references the component and the project of the component is open in the first instance.The second instance uses a file reference to the assembly and the Copy Local property of the assembly is set to False.The assembly has been loaded into memory of the second instance of Visual Studio .NET or Microsoft Visual Studio 2005.If the Copy Local property is set to False for the referenced assembly, and the assembly is not installed in the global assembly cache, the assembly will not be copied to the local \Bin folder of the consuming project. To resolve the location of the referenced assembly, Visual Studio .NET or Microsoft Visual Studio 2005 will search the paths that are listed in the Reference Path property of the project until it finds the assembly. In a scenario where the assembly has been loaded, such as a UserControl loaded in the Windows Form designer, the assembly is being used and cannot be overwritten when you try to build it. When this behavior occurs, you receive the error message that is mentioned in the “Symptoms” section.
As soon as the assembly has been loaded in memory of the Visual Studio .NET or Microsoft Visual Studio 2005 IDE, the assembly will not be unloaded until you exit and then restart the Visual Studio .NET or Microsoft Visual Studio 2005 IDE. For more information, see the “Resolution” section.
Cause 2 This behavior also occurs if you try to build the application when you are currently running an instance of the application from its \Bin folder, externally of the Visual Studio .NET or Microsoft Visual Studio 2005 IDE.
During the build process, the compiler builds the project to an intermediate folder, such as Obj\configname. In this folder name, configname is the name of the project. When the build process has completed, the files are copied from the intermediate folder into the output folder. The “Could not copy temporary files to the output directory” error message indicates that the compiler could not copy these files from the intermediate folder to the output folder. If you run the application that is being built from the output directory, the compiler cannot overwrite these files because they are currently being used. For more information, see the “Resolution” section.