Visual Basic
  Home arrow Visual Basic arrow Page 3 - A Real-Time ActiveX News Control
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC

A Real-Time ActiveX News Control
By: Mitchell Harper
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2001-12-22

    Table of Contents:
  • A Real-Time ActiveX News Control
  • Creating the ActiveX control
  • Obtaining our news with XMLHTTP
  • Displaying the link
  • Compiling and using our new ActiveX control
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    A Real-Time ActiveX News Control - Obtaining our news with XMLHTTP


    (Page 3 of 6 )

      We will create a function named LoadNews. LoadNews will handle all of the background work (retrieving and displaying the news items), while still updating the progress bar on our actual ActiveX control.

    Our LoadNews function will accept a reference to a string variable (byref), and return a Boolean value, indicating the success/failure of the function as a whole. If an error occurs, its description will be stored in the string variable passed as a reference. The declaration of the LoadNews function looks like this:

    Private Function LoadNews(ByRef msg As String) As Boolean

    The LoadNews function is declared as private, so that when it comes time to compile and distribute our ActiveX control, only the control itself can call the LoadNews function. Several objects from the MSXML 4 library are also created, as well as two string variables and one integer variable. These will be used to hold the details of each news item:

    Dim objXMLHTTP As New MSXML2.XMLHTTP40

    Dim objXML As New MSXML2.DOMDocument40

    Dim objXML_Root

    Dim strURL As String

    Dim strHeadLine As String

    Dim counter As Integer


    Next, we set the URL of the XML news page we will be retrieving. If you visit http://w.moreover.com/categories/category_list_xml.html, you will see a list of news topics. At the time of writing, there were 334 available. I have created a constant string variable in the general declarations section to hold the name of our news category, like this:

    Const NewsCategory As String = "Videogame news"

    This is used to form the URL of the XML page to retrieve, as shown below:

    strURL = "http://p.moreover.com/cgi-local/page?c=" & NewsCategory & "&o=xml"

    Next, we use the Open and Send methods of our XMLHTTP object to actually retrieve the page. We are using the "GET" HTTP protocol, and have set asynchronous mode to false, meaning that our XML document MUST be downloaded before we can continue:

    objXMLHTTP.open "GET", strURL, False

    objXMLHTTP.send


    We also update the loading message and progress bar:

    lblLoading.Caption = "Loading XML news feed..."

    pbProgress.Value = pbProgress.Value + 1


    Once the request method of the XMLHTTP object has returned, we try and load its text (available as the responseText variable) into our XML document object. If an error occurs, the function is aborted and the value of the string-referenced variable is set to the errors description:

    objXML.loadXML (objXMLHTTP.responseText)



    'Make sure that valid XML was returned

    If objXML.parseError.errorCode <> 0 Then

    msg = objXML.parseError.reason

    LoadNews = False

    Exit Function

    End If


    At this point, if the function hasn't encountered any errors, then the XML has been loaded successfully, and we are ready to extract each news item. We obtain the root element of the XML document into the objXML_Root object, and use its getElementsByTagName function to make sure it only contains <article> elements:

    Set objXML_Root = objXML.documentElement

    objXML_Root.getElementsByTagName ("article")


    Our ActiveX control will display the top five items in the XML document (The MoreOver.com XML document returns twenty by default). We use a simple "for" loop to extract them from our XML document. Once a loop is complete, the status message and progress bar are also updated:

    For counter = 1 To 5

    lblLoading.Caption = "Loading news item " & counter & "..."

    pbProgress.Value = pbProgress.Value + 1


    The URL of each item is extracted into the strURL variable. The headline and source of each news item are also extracted into the strHeadLine variable:

    strURL = objXML_Root.childNodes(counter).childNodes(0).firstChild.Text

    strHeadLine = objXML_Root.childNodes(counter).childNodes(1).firstChild.Text & " [Source: " & objXML_Root.childNodes(counter).childNodes(2).firstChild.Text & "]"


    Under the general declarations section of our ActiveX control, I have created an array that will hold each of the links to the news items:

    Dim arrLinks(5) As String

    When a user clicks on a label containing the details of a news item, the items link (contained in the arrLinks string array) is executed using the ShellExecute API function (more on this later). We simply set the value of each items URL to its index in the arrLinks array, like this (we use "counter-1" because arrays are indexed from 0 onwards):

    arrLinks(counter - 1) = strURL

    We also set the caption of the link to the headline:

    Select Case counter

    Case 1

    lblLink1.Caption = strHeadLine

    Case 2

    lblLink2.Caption = strHeadLine

    Case 3

    lblLink3.Caption = strHeadLine

    Case 4

    lblLink4.Caption = strHeadLine

    Case 5

    lblLink5.Caption = strHeadLine

    End Select


    Lastly, we make sure that all of the labels are visible, destroy all of the XML objects, and return true, indicating that the function succeeded:

    'Hide the status label and progress bar

    lblLoading.Visible = False

    pbProgress.Visible = False



    'Make sure the labels are visible

    lblLink1.Visible = True

    lblLink2.Visible = True

    lblLink3.Visible = True

    lblLink4.Visible = True

    lblLink5.Visible = True



    'Kill the XML objects

    Set objXMLHTTP = Nothing

    Set objXML_Root = Nothing

    Set objXML = Nothing



    LoadNews = True

    Exit Function


    The LoadNews function takes care of retrieving, extracting and displaying our news links. Let’s take a look at how the ActiveX control will actually display the link.

    More Visual Basic Articles
    More By Mitchell Harper


     

    VISUAL BASIC ARTICLES

    - Developing an XML Web Service Using Visual S...
    - Creating an HTML File List with VB
    - Fun with Email: VB6, CDO, MAPI, and a Remote...
    - Extranet/Intranet Dictionary Cracker in VB
    - Finding Default App Icons With Visual Basic
    - Registry Fever With Visual Basic
    - Implementing An ADO Data Control With VB6
    - Printing With Visual Basic
    - MSMQ Part 1/2: Architecture and Simple Imple...
    - Magnifying The Desktop With Visual Basic
    - Sending Email With MAPI Components in Visual...
    - Two Person Chat With The Winsock Control And...
    - A Real-Time ActiveX News Control
    - Accessing the Windows API in Visual Basic







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek