Advertisment

Visual Basic for Linux

author-image
PCQ Bureau
New Update

Missing Visual Basic on Linux? Gambas for Linux comes close, to make Visual Basic programmers feel at home. Here, we use Gambas to develop an application that will store the links to your favorite websites in a MySQL database and allow a preview of the website -a simple but feature-rich application that talks to a database as well as uses a Web browser component to display the website.

Advertisment

Install Gambas



Download the latest Gambas archive from http://gambas.sourceforge.net/.

Extract the archive as:

tar -jxvf gambas-0.99.RC2.tar.bz2

This will produce a directory named gambas-0.99.RC2. Change to this directory and

isue:

Advertisment

./configure -disable-libxml



make


make install

Direct Hit!
Applies to: Linux developers
USP:

Build a simple application that talks to a back-end



database
Links:

http://gambas.sourceforge.net 

Gambas, when configured with XML, doesn't compile on PCCQLinux 2004. Hence we disabled the XML support (by -disable-libxml). To run Gambas, type gambas in the 'Run Application' box or in a terminal window within X Window.

Advertisment

New project and application's form



Click on New Project. Select 'Create a Graphical Project'. Type in PCQuest (say) for project name and 'Favorite Links Manager' for project title. Select a directory to save the project. Click OK. This will launch the Gambas IDE with project explorer, toolbar and properties window. Right click on Forms in the project explorer and select New>Form. For Name type in FMain. Resize the resulting form to your preferred size. Right click on the form and select properties. In the properties window you can set the background color and title for the form. Type in Favorite Links Manager for the Text property, which will be shown as the form's title when the application is executed.

Design the menu



For the menu we will have a menu item named Menu, with two sub items, namely 'Add a Favorite Link' and 'Show My Favorite Links'. For this, right click on the form and select 'Menu Editor'. Click on Insert. For Name and Caption, type in Menu. Click on Next/Insert. Type in Add for name and 'Add a Favorite Link' for caption. Click on the right arrow button once. Again click on Next and then on Insert. For Name type in Show and Caption 'Show My Favorite Links'.



Click on OK.

Connect to the database



Start MySQL by issuing 'service mysqld start' . Next, create a new database named

pcquest.

Advertisment

mysqladmin -u root -p create pcquest

When prompted supply the password for the root MySQL user. Next issue:

mysql-u root -p pcquest

Advertisment

When prompted, again supply the MySQL root password. Type in the following query to create the table to store the website and URL.

CREATE TABLE favoritelinks (



id int NOT NULL auto_increment,


website varchar(50) default NULL,


url varchar(100) default NULL,


PRIMARY KEY (id)


)



Back in Gambas, double click on the form. Type in the following line above the

Form_Open().

Advertisment

PUBLIC oConn AS NEW Connection







This will create a new database Connection object named oConn. Type in the following code between the 'PUBLIC SUB Form_Open( )' and 'End'.

oConn.Type="mysql"



oConn.Name="pcquest"


oConn.Login="root"


oConn.Password="secret"

With oConn.Type you specify the database to be used. Besides MySQL, Gambas can connect to PostgreSQL and SQLite databases. With oConn.Name, oConn.Login and oConn.Password, you specify the database name, database user and the corresponding password. Substitute secret with the MySQL root user's password on your machine. For the database specific code to work, you will need to add the Database component/library to the project. For this, click on Projects>Properties>Components and check

gb.db.

Advertisment

First frame



This frame will consist of two text fields to enter the name of the site and its URL and a button labeled Add to commit the entry to the database. We will also need two text labels to label the textfields. On the toolbar, double click on Frame (you can identify the name of a component by moving the mouse over it). Resize the frame to the size of the main form. Right click on frame and select properties. For Name and Text, type in frAdd and 'Add a Favorite Link', respectively.

On the toolbar, double click on the TextBox to add a text box to the frame. Right click on it and select properties. In the properties window, type in txtWebsite for Name and delete the value for the Text. Add another TextBox and specify txtURL for its name and delete the value for its Text property. Add two TextLabels (component labeled as abc). Set their Text property as Website: and URL: respectively. Finally add a Button and set its Name and Text properties to btnAdd and Add, respectively. You can also use the Alignment option to align the components precisely.

Add code for database insert



Double click on the button and type in the following between PUBLIC SUB btnAdd_Click() and END.

Dim strQuery AS String



Dim strWebsite AS String


Dim strURL AS String


oConn.Open


IF (Trim(txtWebsite.Text) <> "" AND Trim(txtURL.Text) <> "") THEN


strWebsite = Trim(txtWebsite.Text)


strURL = Trim(txtURL.Text)


strQuery = "insert into favoritelinks (website,url) values ('" & strWebsite & "','" & strURL & "')"


oConn.Exec(strQuery)


Message.Info("Website info entered into the Database")


txtWebsite.Text=""


txtURL.Text=""


END IF


oConn.Close











In the above code the noteworthy statement is the one to issue an SQL query. Its syntax is:

oConn.Exec()

Before executing the SQL, we open the connection to the database as oConn.Open and after executing, we close the connection as oConn.Close. After executing the query, we display a message using Message.Info( ), which similar to the MessageBox function in VB.

Add the second frame



The second frame will consist of a combo box and the Web browser component. The combo box will display all the names in the database. Selecting one will open the page. On the toolbar, double click on Frame. Resize the frame to the size of the main form. Right click on it and select properties. For Name and Text, type in frShow and 'Show My Favorite Links', respectively.

On the toolbar, double click on the ComboBox to add a combo box component to the frame. Right click on it and select properties. In the properties window, type in cbWebsite for Name. Also add a TextLabel with the Text property set to 'Favorite Websites :'.

To add a browser component, click on Projects>Properties>Components and check gb.qt.kde.html. On the toolbar, click on the KDE tab. Double click the WebBrowser component. Drag and arrange the components on the form. Next, we have to fill the combo box with the websites stored, each time the second form is loaded. For this, on the main form, click on Menu>Show My Favorite Links. Type in the following code for the Show_Click( ) Sub.

DIM rsResult AS Result



DIM strQuery AS String


'hide the frame frAdd and show the frame frShow


frmAdd.Visible=FALSE


frmShow.Visible=TRUE


'clear the combo box


cbWebsite.Clear


'add an empty or blank item to the combo box


cbWebsite.Add(" ",0)


'open the database connection


oConn.Open


strQuery = "select id,website from favoritelinks "


'execute the above query and store the result in rsResult


rsResult = oConn.Exec(strQuery)


'iterate through each row of the Result object


WHILE rsResult.Available


'add the website name to the combo box and use the id from the database as the index


cbWebsite.ADD(rsResult<"website">,rsResult<"id">)


'move to the next row


rsResult.MoveNext


WEND


'close the connection


oConn.Close




























We also need to hide the frame frShow and show the frame frAdd when the 'Add a Favorite Link' menu item is selected. For this click on Menu> Add a Favorite Link and add the following code for the subroutine.

frAdd.Visible=TRUE



frShow.Visible=FALSE

OnChange, show the link



Next, when a website is selected in the combo box, we need to display the corresponding page in the WebBrowser component. For this, right click on the ComboBox component and select Event>Change. Type in the following code for the cbWebsite_Change Sub.

DIM strID AS String



DIM rsResult AS Result


DIM strQuery AS String


'get the index of the item selected in the combo box


strID = cbWebSite.Index


'open database connection


oConn.Open




Get the URL corresponding to the selected index (the id column of the table)

strQuery = "select url from favoritelinks where id=" & strID



rsResult = oConn.Exec(strQuery)


IF rsResult.Available THEN


' set the path or URL for Web Browser's component to the URL retrieved from the above query


webbrowser1.Path=rsResult<"url">


END IF


'close the database connection


oConn.Close





Shows the URL in the WebBrowser component.

See it running



Right click on the second frame, frShow, select Properties and set Visible to False. This is because we don't want the second frame to be visible initially. Click on Project>Compile all and then click on the Play icon, Type in the name of a website say



PCQuest and its absolute URL (along with the http:// prefix) ie, https://www.pcquest.com.
What is left is packaging the application for distribution. Click on Project and try 'Make Executable' and 'Make Installation Package'.

Shekhar Govindarajan



IT4Enterprise

Advertisment