Thursday 10 May 2012

How to solve DLL Hell


Welcome to dll hell.

Suggestion:
1) open the exe in VB, and remove the reference to the dll, close VB
2) open the dll in VB, set the project to no compatibility
3) build the dll
4) change the project to binary compatibility, and point to the newly built dll
5) open the exe, ans restore the reference.

Caveat: there is a bug in VB that prevent to release the dll once the application is run, so sometimes when you try to rebuild the dll you get permission denied. In that case, just reopen the project, that will release the dll

Tip: you can load both dll and exe projects in a group, to easily debug them together.

Saturday 5 May 2012

WHY USE AN ACTIVEX CONTROL?:



When you create an ActiveX control (OCX file), you are creating a TOOL.  This tool can be a custom control to do something that no standard control can do, or it can be a control that works exactly like a standard control works, but with slightly changed functionality, properties, events, and/or methods.  It can be whatever you want.
Let's say for example that you would like to create a PictureBox that loads and saves BMP, JPEG, and GIF pictures, as well as adds special effects to them when they are loaded.  No standard Visual Basic control can do this custom work.  You can get this functionality by putting in a HUGE amount of custom coding into your application, or by using my Advanced Bitmap Processing module (along with the JPEG module posted on this site), or something like that.  But why bother if the thing you're displaying the picture on (most often a PictureBox) can do that kind of work for you?  Creating a custom PictureBox that does this kind of custom functionality would take that kind of work off of your main application, and allow your code in your main application to be cleaner and simpler.
Another possible use for this tool would be to create a control that does things for you in order to take processing and code off your main application.  For example, you could create an ActiveX control that contains several smaller controls like a TreeView on the left, and a ListBox on the right which resizes them dynamically when the ActiveX control is resized.  It could also contain data validation for information being passed in to be displayed in the TreeView or ListBox.  This takes a LARGE amount of code off of our main application so that your main application's code is cleaner and simpler.
Another advantage to using ActiveX controls over just coding the functionality into your main application is the ActiveX control runs in its own thread.  If you are handling some intense processing, graphics work, database connectivity, data transferring, or sub-classing within your ActiveX Control and something goes wrong, the ActiveX will freeze instead of your entire application freezing up.  The ActiveX control DOES get loaded into the same memory space as your main application though, so it is possible for something to go wrong with your ActiveX control that takes down the rest of your program, but that is VERY unlikely.
Also, because ActiveX controls run in their own threads, you can create a "multi-threaded" application by embedding functionality into an ActiveX control and having them do your application's work.  For example, you could create a "Download Control" that simply goes to the FTP site you specify and downloads the file you specify to a destination file on your hard drive that you specify.  All you'd have to do to download multiple files simultaneously is create a control array (or several instances of that ActiveX control) on a form, set each one to download a different file to a different destination, and cut 'em all loose!  BOOM!  Multi-threading, baby!  Multiple downloads that are independent of each other.
But ActiveX controls are not just for use within Visual Basic, C++, Delphi, and other "COM aware" programming languages.  You can also use ActiveX controls over the internet!  It is possible to embed an ActiveX control right into a web page and then view it with Microsoft Internet Explorer 4.x or better.  
"But how do I embed an ActiveX control into my HTML or ASP web page?"
First of all, I need to be clear that ActiveX is a Microsoft technology and Netscape does not natively support it.  There are Netscape Plug-Ins that will allow you to use them within Netscape, but they obviously don't work as well as Microsoft Internet Explorer (which is designed to natively support them).
Now, let's take the "CustomButton" (CustmBtn.ocx) ActiveX control that I posted on this web site.  If you wanted to embed that into a web page, the HTML code would look something like this:
 
<
HTML>
<
HEAD>
<
TITLE>TestOCX</TITLE>

<
SCRIPT LANGUAGE="JavaScript">
  
// This function changes the caption to "Hello" and displays a MessageBox via JavaScript
  function 
ChangeCaption() {
    TestForm.CustomBtn.Caption = "Hello";
    alert ("Click OK when ready to change the caption again");
  }

</
SCRIPT>

<
SCRIPT LANGUAGE="VbScript">
  
' This function changes the caption to "Whasssup!" and displays a MessageBox via VbScript
  Sub 
ChangeBackColor
    TestForm.CustomBtn.Caption = "Whasssup!"
    
MsgBox "Caption was changed",vbOKOnly Or vbExclamation, " "
  End Sub
</
SCRIPT>

<
SCRIPT LANGUAGE="JavaScript" FOR="CustomBtn" EVENT="Click">
  
// This event fires when the ActiveX control is clicked and displays the About dialog
  
TestForm.CustomBtn.About();
</
SCRIPT>

</
HEAD>
<
BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<!-- The ActiveX control has to be within a form in order for it to be called via code //-->
<
FORM ID="TestForm">
  <
OBJECT ID="CustomBtn" CLASSID="CLSID:BB7BA40E-784E-11D4-AF87-0008C74B19A1">
    <
PARAM NAME="_ExtentX"   VALUE="2249">
    <
PARAM NAME="_ExtentY"   VALUE="714">
    <
PARAM NAME="Appearance" VALUE="0">
    <
PARAM NAME="BackColor"  VALUE="12632256">
    <
PARAM NAME="Caption"    VALUE="Testing">
    <
PARAM NAME="Enabled"    VALUE="-1">
    <
PARAM NAME="ForeColor"  VALUE="12632319">
    <
PARAM NAME="Style"      VALUE="0">
  </
OBJECT>
</
FORM>

<
SCRIPT LANGUAGE="JavaScript">
  
// Change the caption the first time via JavaScript
  
ChangeCaption();
</
SCRIPT>

<
SCRIPT LANGUAGE="VbScript">
  
' Change the caption the second time via VbScript
  Call 
ChangeBackColor
</
SCRIPT>

</
BODY>
</
HTML>
  
You'll notice a few things about this code.  First is that in order to use the ActiveX control within your page, it has to be embedded as an object.  Not only does it have to be embedded as an object, but it has to be embedded with an "ID" and within a form that also has an "ID".  The form it's embedded within doesn't have to do anything at all, but it has to be there, and it has to have an ID.  The reason for this is the embedded object can't stand alone in HTML just as a PictureBox control can't stand alone within Visual Basic without a Form, MDI Form, PropertyPage, UserControl, or UserDocument.  It has to be a part of a form within VB or HTML.  That is how the object is referenced in both cases...  by <FORM>.<CONTROL> where in this case, the form is "TestForm" and the control is "CustomBtn".  You'll notice that it is referenced in VbScript and JavaScript as such.  Again, VbScript is a Microsoft technology and isn't natively supported by Netscape (though I wish it was).
The second thing you may notice is that the ActiveX control's properties are initially set using the object's "PARAM" values, and later at run-time by referencing the <FORM>.<CONTROL>.<PROPERTY>.  That's easy enough, but what about Methods and Events?  Well, methods are called like functions from VbScript or JavaScript similar to properties... <FORM>.<CONTROL>.<METHOD>.  As far as events, those are a little bit more complex, but still fairly simple.  All you do is declare a JavaScript <SCRIPT> block and tack on "FOR" pointing to the name of the object that the event is for, and "EVENT" with the name of the event that the script block will be fired for represents.  In this case, the CLICK event is the one handled, and it calls the "About" method which shows the control's about message.
The third thing you may notice is that the ActiveX control is referenced not by location, but by "CLSID" (Class Identifier).  The CLSID is also referred to as the "GUID" (Globally Unique Identifier) or "UUID" (Universally Unique Identifier).
"But how will I know what the CLSID of my ActiveX control is?"
You'll be able to find out by looking in your Windows Registry via a tool called "Registry Editor" (REGEDIT.EXE).  Go to START > RUN and type "REGEDIT" and hit ENTER.  First locate the name of the interface you wish to reference by going to theHKEY_CLASSES_ROOT section and looking for the name of the project the class module interface you wish to reference is located.  This <PROJECT>.<CLASS> combination is refered to as the "ProgID" or "Program Identifier" and is used in calls toServer.CreateObject() in ASP, and CreateObject() in VB.  In this case, it's Custom_Button.CustomButton.  Under this registry key, you'll notice a registry key called "CLSID".  The default value for this key is the "CLSID" that you use to reference your ActiveX control as I did in the above code.
If you want to take this a step further and find out the location of the actual OCX or DLL file that is referenced by the CLSID, you can do that by going to the "CLSID" section under the HKEY_CLASSES_ROOT section and finding your CLSID... in this case {BB7BA40E-784E-11D4-AF87-0008C74B19A1}.  Once you find it, you'll notice there are several entries under it.  One of them is "InprocServer32".  The default value of this registry key will be the physical location of the file that is your ActiveX control.  In this case, CustmBtn.ocx.

WHAT IS AN ACTIVEX CONTROL?:


An ActiveX control is a COM object similar to a Standard VB Class Module or ActiveX DLL with properties, methods, and events.  The difference between an ActiveX control and an ActiveX DLL is that an ActiveX control has a user interface and requires you to put it on something (like a Form, MDI Form, Property Page, User Document, or User Control) so the user interface can be displayed.

A CommandButton, ListBox, PictureBox, Label, Timer, etc., are all ActiveX controls as well, only they are built into Visual Basic as native controls.  ActiveX Controls are the building blocks of applications, just as native types (like String, Integer, and Long) are the building blocks of custom variable Types.

Wednesday 2 May 2012

VB6 tips to quickly change controls property by just triple click



If you want to copy a number of properties from one control to one or more other controls, you can select the control you want to copy from, press Shift and select the other controls, press F4 to show the Properties window, and triple-click the name of the property you want to copy. Note that you must click the name of the property on the left, not the value cell on the right. The values of the properties on which you triple-click are copied from the source controls to all the other selected controls. This technique doesn't work with all the items in the Properties window.


Handy VB6 select control property quickly




There is one handy but undocumented technique for quickly selecting a given property of a control. You just have to select the control on the form and press the Ctrl+Shift+x key, where x is the first letter in the property's name. For instance, select a Label control, and then press Ctrl+Shift+C to display the Properties window and select the Caption property in one operation. Pressing the Ctrl+Shift+C key again moves the focus to the next property whose name begins with the C character, and so on in a cyclic fashion.

Visual Basic 6 tips to create multiple control at once


If you need to create multiple controls of the same type, you can follow this three-step procedure: First, click on the control's icon on the Toolbox window while you keep the Ctrl key pressed. Next, draw multiple controls by clicking the left button on the form's surface and then dragging the cursor. Finally, when you're finished creating controls, press the Escape key or click the Pointer icon in the upper left corner of the Toolbox.

Visual Basic 6 Strength

One of the greatest strengths of the Visual Basic language is that programmers can design an application and then test it without leaving the environment. But you should be aware that designing and testing a program are two completely different tasks. At design time, you create your forms and other visible objects, set their properties, and write code in their event procedures. Conversely, at run time you monitor the effects of your programming efforts: What you see on your screen is, more or less, what your end users will see. At run time, you can't invoke the form designer, and you have only a limited ability to modify the code you have written at design time. For instance, you can modify existing statements and add new ones, but you can't add new procedures, forms, or controls. On the other hand, at run time you can use some diagnostic tools that aren't available at design time because they would make no sense in that context (for example, the Locals, the Watches, and the Call Stack windows

Visual Basic 6 is event-driven programming


Visual Basic lets you build a complete and functional Windows application by dropping a bunch of controls on a form and writing some code that executes when something happens to those controls or to the form itself. For instance, you can write code that executes when a form loads or unloads or when the user resizes it. Likewise, you can write code that executes when the user clicks on a control or types while the control has the input focus.

This programming paradigm is also known as event-driven programming because your application is made up of several event procedures executed in an order that's dependent on what happens at run time. The order of execution can't, in general, be foreseen when the program is under construction. This programming model contrasts with the procedural approach, which was dominant in the old days.

Why END function is not recommended in VB6


CAUTION

You can also stop any Visual Basic program running in the environment by invoking the End command from the Run menu, but in general this isn't a good approach because it prevents a few form-related events—namely the QueryUnload and the Unload events—from firing. In some cases, these event procedures contain the so-called clean-up code, for example, statements that close a database or delete a temporary file. If you abruptly stop the execution of a program, you're actually preventing the execution of this code. As a general rule, use the End command only if strictly necessary.

Visual Basic intrinsic controls

The Pointer isn't a control; click this icon when you want to select controls already on the form rather than create new ones.


The PictureBox control is used to display images in any of the following formats: BMP, DIB (bitmap), ICO (icon), CUR (cursor), WMF (metafile), EMF (enhanced metafile), GIF, and JPEG.


The Label control serves to display static text or text that shouldn't be edited by the user; it's often used to label other controls, such as TextBox controls.


The TextBox control is a field that contains a string of characters that can be edited by the user. It can be single-line (for entering simple values) or multiline (for memos and longer notes). This is probably the most widely used control of any Windows application and is also one of the richest controls in terms of properties and events.


The Frame control is typically used as a container for other controls. You rarely write code that reacts to events raised by this control.


The CommandButton control is present in almost every form, often in the guise of the OK and Cancel buttons. You usually write code in the Click event procedure of this control.


The CheckBox control is used when the user has to make a yes/no, true/false selection.


OptionButton controls are always used in groups, and you can select only one control in the group at a time. When the user selects a control in the group, all other controls in the group are automatically deselected. OptionButton controls are useful for offering to the user a number of mutually exclusive selections. If you want to create two or more groups of OptionButton controls on a form, you must place each group inside another container control (most often a Frame control). Otherwise, Visual Basic can't understand which control belongs to which group.


The ListBox control contains a number of items, and the user can select one or more of them (depending on the value of the control's MultiSelect property).


The ComboBox control is a combination of a TextBox and a ListBox control, with the difference that the list portion is visible only if the user clicks on the down arrow to the right of the edit area. ComboBox controls don't support multiple selections.


The HScrollBar and VScrollBar controls let you create stand-alone scroll bars. These controls are used infrequently because the majority of other controls display their own scroll bars if necessary. Stand-alone scroll bars are sometimes used as sliders, but in this case you'd better use other, more eye-catching controls, such as the Slider control, which is covered in Chapter 10.


The Timer control is peculiar in that it isn't visible at run time. Its only purpose is to regularly raise an event in its parent form. By writing code in the corresponding event procedure, you can perform a task in the background—for instance, updating a clock or checking the status of a peripheral device.


The DriveListBox, DirListBox, and FileListBox controls are often used together to create file-oriented dialog boxes. DriveListBox is a ComboBox-like control filled automatically with the names of all the drives in the system. DirListBox is a variant of the ListBox control; it shows all the subdirectories of a given directory. FileListBox is another special ListBox control; this control fills automatically with names of the files in a specified directory. While these three controls offer a lot of functionality, in a sense they have been superseded by the Common Dialog control, which displays a more modern user interface (to be covered in Chapter 12). If you want to write applications that closely conform to the Windows 9x look, you should avoid using these controls.


The Shape and Line controls are mostly cosmetic controls that never raise any events and are used only to display lines, rectangles, circles, and ovals on forms or on other designers.


The Image control is similar to the PictureBox control, but it can't act as a container for other controls and has other limitations as well. Nevertheless, you should use an Image control in place of a PictureBox control whenever possible because Image controls consume fewer system resources.


The Data control is the key to data binding, a Visual Basic feature that lets you connect one or more controls on a form to fields in a database table. The Data control works with Jet databases even though you can also use attached tables to connect to data stored in databases stored in other formats. But it can't work with ActiveX Data Objects (ADO) sources and is therefore not suitable for exploiting the most interesting database-oriented Visual Basic 6 features.


The OLE control can host windows belonging to external programs, such as a spreadsheet window generated by Microsoft Excel. In other words, you can make a window provided by another program appear as if it belongs to your Visual Basic application.



Visual Basic 6 new Project


Visual Basic 6

In its six versions, Visual Basic has evolved from the simplest programming language for Microsoft Windows to an exceedingly complex development environment, capable of delivering virtually anything from tiny utilities to huge n-tier client/server applications.