Building a News Ticker Using VB.Net - Application Basics
(Page 3 of 5 )
Scrolling
There are two vital parameters necessary to be calculated for scrolling:
- The String width
- The Form Width
Calculating Width
Width plays an important role in our application. We calculate the width of the form by simply putting:
ME.WIDTH
The above statement retrieves the width of the application for you. This width determines the starting point and ending point of the scrollable layout. To measure the string width we have to use:
System.Drawing.Graphics.MeasureString(String,System.Drawing.font)
where string (in this app) is the text from the news tag of our XML file. Font will be described in the application.
Manipulating the XML File
We can browse through the XML tag collections easily. Further to simplify the process, we use XMLTextReader in this app. Let’s look at it in detail:
Private Sub loadthenews()
Dim readXML As New XmlTextReader("G:\vbapps\dr\news.xml")
‘the file location might differ in your computer, make sure you add the correct file path
While readXML.Read()
If readXML.NodeType = XmlNodeType.Text Then
str += " " & readXML.Value
End If
End While
End Sub
We are using the loadthenews() method to load the XML file and also for building the scrolling text from the news tag(s).
As you can easily understand, the first line loads the XML file from the given file URL. readXML.read() lets the application browse through the XML news tags. The following two lines makes sure that the strings are appended only if they are of node type Text (in our case it is the <news> tag). These lines of code code are used to construct the string parameter for the drawstring method.
Timer Control
Timer is the driver of this ticker application. The main purpose of the timer is to activate the Graphics.Drawstring method at a specified interval until the timer stops. We introduce this drawstring method in the Timer1.Tick event handler. Hence, we draw the string at a decremented location with respect to the timer interval. I’ll explain about decrementing the location later.
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
'Debug.Write(vbCr & strwidth.Width & vbCr)
'Debug.Write(String.Format("widthX = {0}", CStr(widthX) & vbCr))
g.Clear(Me.BackColor)
g.DrawString(str, fo, Brushes.Black, widthX, heightY)
' Debug.Write(String.Format("the string width is {0}", ‘
‘ CStr(strwidth.Width)))
If widthX <= (0 - douX) Then ‘check whether all text ‘have been scrolled
widthX = Me.Width
Else
widthX -= 5
End If
END SUB
In the code above, we refresh the form with the application’s background color, draw the string at the given location (widthX and heightY). On the next line, we check whether the text has scrolled completely or not. If yes, we scroll it from the starting point (Me.Width); otherwise, decrease the widthX value by -5, so that drawstring paints in the new WidthX value to get the scrolling effect.
That’s it for the scrolling tape. Let’s see an example!
Next: Code >>
More VB.Net Articles
More By R. Abhiram Vikrant