VB Notebook Main Page VB Notebook Files VB Notebook Function Library VB Notebook Articles VB Notebook Links VB Notebook

VBNotebook Home | Articles | Q&A Session: VB Oddities

Some commonly asked questions about Visual Basic have unusual or unexpected answers. Here are a few of them.

__________________________________________________________________________________________

Q1: How can I determine when an item is selected in a Style 2 (dropdown list) combo box? I've tried the Change event, but it doesn't seem to work.

A1: The Click event is what you want to use in this case. The reason that the Change event did not fire for you is that the Change event only applies to the text box portion of the combo box. The text box is inactive when the combo is in dropdown list mode.

__________________________________________________________________________________________

Q2: I want to right align a text box. This seems to work OK on some computers, but not on others. What gives?

A2: This behavior depends on the underlying operating system support of the edit class. In Win95 and earlier and in earlier versions of WinNT, the underlying edit class only supports right and center alignment when the style of the control is multi-line (essentially the same as MultiLine property = True in VB). In Windows 2000, WinNT 4 SP3 and higher and in Win98/ME, the edit class allows a single line control to align right or center.

There is a bug listed in the MS Knowledge base that concerns the VB Windows CE toolkit. It doesn't correctly follow the operating system support in this area, but this only affects you if you're using the CE toolkit.

The cross-platform solution is to simply set the textbox's MultiLine property to True. Note that you may have to expand the size of the box slightly or make it borderless for this to display correctly. You will also have to manage the possibility of someone pasting in a string with a CR/LF in it.

__________________________________________________________________________________________

Q3: I'm using a date/time picker to display/update a date field on a database. When the value of the date field on the database is null (or when adding a record), is it possible to make the date/time picker control show a blank?

A3: You can't get it to show a blank date. The way the date picker shows a Null date by unchecking a check box. By default, this checkbox isn't displayed, but you can set the CheckBox property to True to show the checkbox and, therefore, allow it to accept Null values.

If that doesn't seem like the best user interface idea you've ever heard of, well, I agree with you. But that's the way you have to do things unless you want to either write your own date picker control or buy a 3rd party product that does this differently.

__________________________________________________________________________________________

Q4: I keep getting a permission denied error when trying to compile ActiveX DLLs. Why does this happen?

A4: This happens because you have binary compatibility set to your working DLL. VB project groups that have the DLL as a member or the DLL project itself use the DLL obtain ActiveX interface information from the file while they're in the IDE. Since binary compatibility is set, VB cannot safely rely on what's cached into in the IDE (what happens for project compatibility), it must refer back to the compiled executable's information in the compatibility file. This requires that this file be protected from modification or deletion.

The way to handle this is to at first use project compatibility while the interface to the DLL is still being modified and compiled a lot and is not being distributed to users or being shared extensively with other members of a development team. Once the interface has stabilized, make a copy of the compiled DLL, name it to a non-executable extension (.CMP is the most common one used), and set binary compatibility to it instead of the DLL.

__________________________________________________________________________________________

Q5: When I type the following into the debug/immediate window...

    ? 123.66-50.5-50.5-22.66

I get
-3.5527136788005E-15 even though the result should be zero. Is this a bug?

A5: This isn't a bug, it's just the way IEEE floating point numbers work. See the following Knowledge Base article on the subject for full details on how to work with IEEE numbers

(Complete) Tutorial to Understand IEEE Floating-Point Errors
Article ID: Q42980
http://support.microsoft.com/support/kb/articles/q42/9/80.asp

__________________________________________________________________________________________

Q6: When I type the following into the debug/immediate window...

     ? 2964*20

I get an overflow error. Is this a bug?

A6: This happens because, by default, VB assumes that the values are integers and only allocates space for an integer for the results of a calculation. When the calculated value exceeds the maximum for an integer, an overflow error occurs. You can force it to allocate a long instead by explicitly indicating the type in some way. Here are some examples that work....

     ? 2964*20!
     ? CLng(2964)*20 

__________________________________________________________________________________________

Copyright 2000-2005, J. Frank Carr