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 ‘form’

How To Create Pie Charts Using the Circle Method in VB

Symptoms
The Pinnacle-BPS Graph control shipping with Visual Basic gives usersthe ability to create pie charts. The Pinnacle-BPS is a relatively largecontrol and uses a large amount of disk space on distribution disks.Therefore, the custom effects available are limited to the features of thecontrol. The Circle method in the VBA language provides functionality todraw arcs and segments. By drawing segments, you can easily create a piechart and add custom features as you require. Below is a code sampledemonstrating how to do this.
Resolution
Start a new Visual Basic project. Form1 is created by default.Place a Command button on the form.Place a 200×200 pixel Picture box on the form.Add the following code to the Form1 code window:

Option ExplicitPublic Sub DrawPiePiece(lColor As Long, fStart As Double, fEnd AsDouble)Const PI As Double = 3.14159265359Const CircleEnd As Double = -2 * PIDim dStart As DoubleDim dEnd As DoublePicture1.FillColor = lColorPicture1.FillStyle = 0dStart = fStart * (CircleEnd / 100)dEnd = fEnd * (CircleEnd / 100)Picture1.Circle (100, 100), 60, , dStart, dEndEnd SubPrivate Sub Command1_Click()Picture1.ScaleMode = vbPixelsCall DrawPiePiece(QBColor(1), 0.001, 36)Call DrawPiePiece(QBColor(2), 36, 55)Call DrawPiePiece(QBColor(3), 55, 75)Call DrawPiePiece(QBColor(4), 75, 99.999)End Sub Press the F5 key to run the project. You should see a pie chart beingdrawn with four sections mirroring the four times that the DrawPieceroutine was called in the Command1_Click event.

How To Create a Form That Always Stays on Top

Symptoms
Microsoft Visual Basic does not offer a property or method to make a formthe topmost window. This behavior can be achieved using the SetWindowPosWin32 API.
This article demonstrates how to set a form as the topmost window using theSetWindowPos Win32 API.
Resolution
The sample code below uses a function called SetTopMostWindow. TheSetTopMostWindow function sets a window as a topmost Window or as a normalWindow, based on the two parameters, hwnd and Topmost, passed to it.
The hwnd parameter specifies the handle of the window to be set as topmostor as normal.
The Topmost parameter specifies whether to set the form as topmost or asnormal. If the value is true, the function sets the form to always remainon top. If the value is false, the function sets the form as a normalwindow.
Step-by-Step ExampleStart a new Standard EXE project. Form1 is created by default.Add two command buttons (Command1 and Command2) to Form1.Set the caption property of Command1 to “Always on top.”Set the caption property of Command2 to “Normal.”Put the following code in the Form1 Declaration section:

Option ExplicitPrivate Sub Command1_Click()Dim lR As LonglR = SetTopMostWindow(Form1.hwnd, True)End SubPrivate Sub Command2_Click()Dim lR As LonglR = SetTopMostWindow(Form1.hwnd, False)End Sub On the Project menu, click Add Module, to add a new module to theproject.Add the following code to the new module:

Option ExplicitPublic Const SWP_NOMOVE = 2Public Const SWP_NOSIZE = 1Public Const FLAGS = SWP_NOMOVE Or SWP_NOSIZEPublic Const HWND_TOPMOST = -1Public Const HWND_NOTOPMOST = -2Declare Function SetWindowPos Lib “user32″ Alias “SetWindowPos”_(ByVal hwnd As Long, _ByVal hWndInsertAfter As Long, _ByVal x As Long, _ByVal y As Long, _ByVal cx As Long, _ByVal cy As Long, _ByVal wFlags As Long) As LongPublic Function SetTopMostWindow(hwnd As Long, Topmost As Boolean) _As LongIf Topmost = True Then ‘Make the window topmostSetTopMostWindow = SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, _0, FLAGS)ElseSetTopMostWindow = SetWindowPos(hwnd, HWND_NOTOPMOST, 0, 0, _0, 0,FLAGS)SetTopMostWindow = FalseEnd IfEnd Function
NOTE: In the above sample code, an underscore (_) at the end of a line isused as a line-continuation character.
Press F5 to run the project.If you click the “Always on top” command button, your form becomes thetopmost window and remains on top of every window; you cannot move anyother window on top of it. If you click the “Normal” button, the formbehaves normally (you can move other windows on top of it).

BUG: UserControl Containing ADO Data Control Fails to Unload

Symptoms
When a contained (inside a UserControl) control’s DataSource property is set to a contained ActiveX Data Objects (ADO) Data Control (MSADODC.OCX) at run-time, the container UserControl may fail to unload even when the form hosting the control is unloaded. As a result, the Terminate event of the UserControl does not fire as expected and if the form hosting the control is repeatedly loaded and unloaded, a memory leak may occur.
Resolution
To work around the problem, use a temporary recordset as described in the “More Information” section.

BUG: UserControl Containing ADO Data Control Fails to Unload

Symptoms
When a contained (inside a UserControl) control’s DataSource property is set to a contained ActiveX Data Objects (ADO) Data Control (MSADODC.OCX) at run-time, the container UserControl may fail to unload even when the form hosting the control is unloaded. As a result, the Terminate event of the UserControl does not fire as expected and if the form hosting the control is repeatedly loaded and unloaded, a memory leak may occur.
Resolution
To work around the problem, use a temporary recordset as described in the “More Information” section.

BUG: Queries or Views Do Not Appear in Data Form Wizard

Symptoms
When you use the Data Form Wizard, queries or views do not appear in thelist of available record sources.
Resolution
The Data Form Wizard incorrectly queries only for tables to populate thedrop-down combo box of available record sources.

BUG: MSChart Control Can’t Plot Data Points >15 Decimal Places

Symptoms
When the Chart Control is assigned an array of data points containing 16 ormore decimal places, those data points do not appear when the chart isupdated.
Resolution
There are two ways to deal with this issue:
Formatting the number to 15 or fewer decimal places.Using the SetData property on each data point.
Workaround 1Before setting the chart to the variant array, “truncate” the decimalvalues to 15 places or less as seen in the form_load event below:

Private Sub Form_Load()Dim data_pts(1 To 3, 1 To 2) As Variantdim i as integerdata_pts(1, 1) = “R1″data_pts(1, 2) = 5.48487730596136E-02data_pts(2, 1) = “R2″data_pts(2, 2) = 7.04216678154124E-02data_pts(3, 1) = “R3″data_pts(3, 2) = 3.10863837084563E-04For i = 1 To 3data_pts(i, 2) = CDbl(Format(data_pts(i, 2), “0.000000000000000″))’Truncate data_pts to 15 decimal places of precisionNextMSChart1 = data_ptsEnd Sub
Workaround 2Use the SetData method to add the data to the chart instead of setting thechart to the variant array. To use this workaround, rewrite the Form_Loadevent to look like this instead:

Private Sub Form_Load()With MSChart1.RowCount = 3.ColumnCount = 1.DataGrid.SetData 1, 1, 5.48487730596136E-02, False.DataGrid.SetData 2, 1, 7.04216678154124E-02, False.DataGrid.SetData 3, 1, 3.10863837084563E-04, FalseEnd WithEnd Sub