You are on page 1of 8

VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?

display=Print

Languages » VB.NET » Applications


Licence CPOL
First Posted 9 Jul 2010
Views 7,355
See Also
Bookmarked 14 times Articles like this
Tags Vista, VB9.0, Articles by this author
By rspercy60 | 18 Jul 2010 VS2008, VB, Windows ...

This is a remake of Pramod Kumar Sing's 2002 API Viewer.

3.78 (5 votes) Sponsored Links

Download source code - 549.55 KB

1 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

Introduction
Have you been looking for an API viewer for VB 9 and C# 3? Has the
old API viewers not been able to work in Vista® or Windows 7®?
VSAPIVIEWER is a remake of Pramod Kumar Singh's 2002 API
Viewer. Pramod's version, I believe, was written in VB.NET 7.0®.

As I was searching for an API viewer, I came across a lot of questions


in the MSDN forums that were asking if anyone knew of a site where
they could get a current API viewer for VS 2008®, and the answer
was always "No". I believe that this is a well needed utility.

Background
After I had downloaded Pramod's app and started to read his code, I
started to finally get hold of what he was trying to do.

Dissecting the old app to the new


If you look at Pramod's actual form, you will see that I tried to keep
mine pretty much the same, except some of the controls are in
different locations and I changed the color back to Control. :)
Updating the data file from WIN32API.txt to API32.txt was a chore.
API32.txt is two times larger than the WIN32API.txt file. I could not
get it to load during the application start-up. I couldn't figure out the
reason for this, so I broke the API32.txt file into four different,
smaller files (which are included) in the Bin\Debug\API\ folder. This
seemed to work very well, so I stuck with it.

Loading the API files!


The constants, functions, subs, and types are loaded when the app
starts. A splash-screen (written by Digital Thunder), and changed to
accommodate this app, displays what is being loaded. This is done
from within the Form_Load event. The code is given below:

Private Sub frmViewerNet_Load(ByVal sender As Object, _


ByVal e As System.EventArgs) Handles Me.Load

FormSplash.Status("Loading Constants!")
LoadConstants()

FormSplash.Status("Loading Functions!")
LoadFunctions()

FormSplash.Status("Loading Subs!")
LoadSubs()

FormSplash.Status("Loading Types!")
LoadTypes()
End Sub

The splash-screen is updated on a different thread via Private


Delegate Sub UpdateStatus(ByVal text As String). The
FormSplash.Status sub takes a string (code above) to display
what we are doing, then we just call the sub or function that does
the work that we want. The subs being called in the above code are
all pretty much the same code. Here is one sub's code:

2 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

Public Sub LoadConstants()


Dim m As RSP.ParseAPI = New RSP.ParseAPI(strFile1)
Dim ThreadConst As Thread

RSP.Constants = New RSP.CConst()

ThreadConst = New Thread(AddressOf m.GetConstInfo)


ThreadConst.Start()

Try
ThreadConst.Join()
Catch e As Exception
MessageBox.Show("Error occured reading file..." &
vbCrLf & strFile1, _
"Error...", MessageBoxButtons.OK,
MessageBoxIcon.Error)
End Try

Thread.Sleep(250)
End Sub

The ParseAPI class is where ninety percent or so of the work is


done. This is where we open up a file, one of the four API32 text
files, for reading, searching, and adding the constants that are found
in the module FileHandling.vb which contains 50 public constants
that we use in our code for VB® or C#®. One of the subs in the
ParseAPI class file is Public Sub GetConstInfo(). The code for
this is below.

3 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

Public Sub GetConstInfo()


Dim sKey As String
Dim sCSharp As String

If pFile <> "" Then


If Not File.Exists(pFile) Then
Types = Nothing
Return
End If

Dim sr As StreamReader = File.OpenText(pFile)


Dim input As String

input = sr.ReadLine()

While Not input Is Nothing


If InStr(1, input, APISTARTCONST) <> 0 Then
'Check for the Comment
If Not input.StartsWith("'") Then
'Look Ahead for false data
If Split(input, "=").Length() <= 2 Then
sKey = Split(input, " ")(1).ToString()
input = APISCOPE & input
sCSharp = input
sCSharp = sCSharp.Replace("Const",
"const int") + ";"
sCSharp = sCSharp.Replace("'", "//")
Constants.Add(sKey, input)
Constants.Add(sKey, sCSharp, True)
End If
End If
End If
input = sr.ReadLine()
End While
sr.Close()
Return
Else
Return
End If
End Sub

After all the data that is found in the four API files is stored in the
app, and the app is finally displayed, we finally get to use it.

One important part of the application that was pointed out to me was
if someone accidentally pressed the Add button twice, duplicates
were added. Deleting these out manually is somewhat time
consuming, so I came up with this code:

4 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

Private Sub btnAdd_Click(ByVal sender As Object, _


ByVal e As System.EventArgs) Handles btnAdd.Click

If lstItems.Items.Count > 0 Then


Select Case cmbAPI.Text
Case RSP.CMB_TYPES, RSP.CMB_CONSTANTS,
RSP.CMB_DECLARES, RSP.CMB_SUBS
If txtSelectedItems.Text = "" Then
Exit Sub
Else
strTemp = Trim(txtSelectedItems.Text)
If InStr(txtAddSelected.Text(), strTemp)
> 0 Then
'Checking for duplicates.
MessageBox.Show("Selection already
exists...", "Oops!", _
MessageBoxButtons.OK,
MessageBoxIcon.Information)
txtSelectedItems.Text = "" 'Clearing
the textbox.
Else
txtAddSelected.Text +=
txtSelectedItems.Text & vbCrLf & vbCrLf
txtSelectedItems.Text = "" 'Clearing
the textbox.
End If
End If
Case Else
End Select
End If
End Sub

The first thing that we do is check to see if the ListBox has any
items in it. If so, we continue. We then check the Combobox's text. It
doesn't matter if it is "Constants", "Declares", "Subs", or
"Types". It will check for any type of duplicate. If we find a
duplicate, then we display a MessageBox (shown below) to the user
telling that we have found a duplicate and that we are erasing it
from the TextBox. If no duplicates are found, then we add it to the
correct TextBox.

Classes
The CConst, CDeclare, CSubs, and CTypes classes have pretty
much the same properties and variables. Here is a list of the class
properties and variables. They all implement the IStore Interface.

Properties

ReadOnly Property Count() As Integer Implements


IStore.Count

5 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

ReadOnly Property GetKey(ByVal index As Integer)


As String Implements IStore.GetKey
Overloads Function GetDataCSharp(ByVal Key As
String) As String Implements IStore.GetDataCSharp
Overloads Function GetDataCSharp(ByVal index As
Integer) As String Implements
IStore.GetDataCSharp

Variables and class files they are in

Dim ConstList As SortedList.............CConst.vb


Dim ConstListCSharp As SortedList.......CConst.vb
Dim DeclareList As SortedList...........CDeclare.vb
Dim DeclareListCSharp As SortedList.....CDeclare.vb
Dim TorpedoeList As SortedList..........CSubs.vb
Dim TorpedoeListCSharp As SortedList....CSubs.vb
Dim TypeList As SortedList..............CTypes.vb
Dim TypeListCSharp As SortedList........CTypes.vb

Duplicate code
While going through the old API Viewer code, I noticed an awful lot
of duplicate code in all the radio buttons, combo, and listboxes. As I
did not understand why all this duplicate code was in there, a friend
told me to delete it all out and start from scratch. This was done and
the cmbAPI_SelectedIndexChanged (code below) was created
using the old code from two different places. The Select Case
statement is from the original combobox.click event, and the code
inside the Case statements is from the FileHandling.vb file.

6 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

Private Sub cmbAPI_SelectedIndexChanged(ByVal sender As


Object, _
ByVal e As System.EventArgs) Handles
cmbAPI.SelectedIndexChanged

txtSelectedItems.Text = ""
'Load the listbox from the selected arrayed strings.
Select Case cmbAPI.Text
Case RSP.CMB_CONSTANTS
lstItems.Items.Clear()
For i As Integer = 0 To RSP.Constants.Count - 1
Dim sKey As String
sKey = RSP.Constants.GetKey(i)
If sKey <> "" Then
lstItems.Items.Add(RSP.Constants.GetKey(i).ToString())
Next
Case RSP.CMB_DECLARES
lstItems.Items.Clear()
For i As Integer = 0 To RSP.Declares.Count - 1
Dim sKey As String
sKey = RSP.Declares.GetKey(i)
If sKey <> "" Then
lstItems.Items.Add(RSP.Declares.GetKey(i).ToString())
Next
Case RSP.CMB_TYPES
lstItems.Items.Clear()
For i As Integer = 0 To RSP.Types.Count - 1
Dim sKey As String
sKey = RSP.Types.GetKey(i)
If sKey <> "" Then
lstItems.Items.Add(RSP.Types.GetKey(i).ToString())
Next
Case RSP.CMB_SUBS
lstItems.Items.Clear()
For i As Integer = 0 To RSP.Torpedoes.Count - 1
Dim sKey As String
sKey = RSP.Torpedoes.GetKey(i)
If sKey <> "" Then
lstItems.Items.Add(RSP.Torpedoes.GetKey(i).ToString())
Next
Case Else

End Select

End Sub

This is a simple routine for searching a listbox with typed text in a


TextBox.

Private Sub txtSearch_TextChanged(ByVal sender As Object, _


ByVal e As System.EventArgs) Handles
txtSearch.TextChanged
If lstItems.Items.Count > 0 Then
lstItems.SelectedIndex =
lstItems.FindString(txtSearch.Text)
End If
End Sub

Copy the text for pasting in your code:

Private Sub btnCopy_Click(ByVal sender As System.Object, _


ByVal e As System.EventArgs) Handles btnCopy.Click
Clipboard.SetText(txtAddSelected.Text) 'Add text to
clipboard for pasting.
End Sub

That's all folks. Hope all of you can make use of this application.

7 of 8 12/15/2010 4:42 PM
VB 9.0, C# 3.0 API Viewer - CodeProject http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx?display=Print

History
Uploaded project on 07/09/2010.
Updated project and article on 07/18/2010.

License
This article, along with any associated source code and files, is
licensed under The Code Project Open License (CPOL)

About the Author

rspercy60 I am currently retired.


I have no degree but I have some
programming experience
when I was in college(Cobol, Pascal).

My accomplishments thus far are;


Best VB.Net article for January(2009)
Best VB.Net article for July(2009)
Other

United States

Member

Comments and Discussions


15 messages have been posted for this article Visit
http://www.codeproject.com/KB/vb/VSAPIVIEWER.aspx to
post and view comments on this article, or click here to get a print
view with messages.

PermaLink | Privacy | Terms of Use Copyright 2010 by rspercy60


Last Updated: 18 Jul 2010 Everything else Copyright © CodeProject, 1999-2010
Web24 | Advertise on the Code Project

8 of 8 12/15/2010 4:42 PM

You might also like