Results 1 to 11 of 11

Thread: [RESOLVED] Converting the XmlTextReader to plain text

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Resolved [RESOLVED] Converting the XmlTextReader to plain text

    I'm new to the ticker stuff and specifically to the xml reader functions of .NET, I've got a flicker-free news ticker from this website, and it is working fine.

    Now, I want to convert the XmlTextReader to a plain text reader so I can this news ticker to my project. Eventually I will store the news ticker text to database so I guess the first that I need to work out is how to convert that code.

    Here's the XmlTextReader function

    Code:
    Private Sub loadthenews()
            Dim readXML As New XmlTextReader(Directory.GetCurrentDirectory & "\news.xml")
            While readXML.Read()
                If readXML.NodeType = XmlNodeType.Text Then
                    str += " " & readXML.Value
    
                End If
            End While
        End Sub
    I want to convert it to something simple like this

    Code:
    Private Sub loadthenews()
            Dim news As String = "The quick brown fox jumps over the lazy dog"
            'Read the news variable letter by letter so I can ticker it.
        End Sub
    and heres the complete code for the flicker-free news ticker if that matters

    Code:
    Imports System.Xml
    Imports System.Text
    Imports System.IO
    
    Public Class Form1
        Dim txtTicker As String = "NTB er verdens beste sted å jobbe!"
        Dim widthX As Single
        Dim heightY As Single = 0
        Dim g As Graphics
        Dim xmlst As String 'string from the xml file
        Dim fo As Font
        Dim str As String
        Dim strwidth As SizeF 'gets the xml string's width and height
        Dim douX As Double 'stores the xmlstring's width
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            'Debug.Write(vbCr & strwidth.Width & vbCr)
            'Debug.Write(String.Format("widthX = {0}", CStr(widthX) &         vbCr))
            ' Debug.Write("Dette er str: " & str)
            SetStyle(ControlStyles.AllPaintingInWmPaint Or
    ControlStyles.OptimizedDoubleBuffer Or
    ControlStyles.UserPaint, True)
    
            e.Graphics.Clear(Me.BackColor)
            e.Graphics.DrawString(str, fo, Brushes.Black, widthX, heightY)
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.AntiAlias
            ' e.Graphics.SmoothingMode =         Drawing2D.SmoothingMode.HighQuality
            e.Graphics.SmoothingMode = Drawing2D.SmoothingMode.HighSpeed
    
    
            ' Debug.Write(String.Format("the string width is {0}",         CStr(strwidth.Width)))
    
            If widthX <= (0 - douX) Then
                widthX = Me.Width
            Else
                widthX -= 1
            End If
    
            e.Dispose()
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Width = Screen.PrimaryScreen.WorkingArea.Width
            Me.Location = New Point(0, 20)
            Me.MaximumSize = New Point(Screen.PrimaryScreen.WorkingArea.Width, 150)
            Me.Height = 150
            Me.MaximizeBox = False
            Me.MinimizeBox = False
            Me.Text = ""
            Me.Icon = Nothing
    
            g = Me.CreateGraphics() 'get the graphics object of the form
            widthX = Me.Width ' x co- ordinate or width
            ' Debug.Write(String.Format("The width is {0}",
            'CStr(Me.Width)))
            Me.loadthenews()
            ' Dim gr As Graphics
            Timer1.Interval = 1
            Timer1.Start()
    
            fo = New Font("SEGOE UI", 16, FontStyle.Regular, GraphicsUnit.Point)
            strwidth = g.MeasureString(str, fo)
            douX = strwidth.Width
        End Sub
    
        Private Sub loadthenews()
            Dim readXML As New XmlTextReader(Directory.GetCurrentDirectory & "\news.xml")
            While readXML.Read()
                If readXML.NodeType = XmlNodeType.Text Then
                    str += " " & readXML.Value
    
                End If
            End While
        End Sub
    
        Private Sub tickerTypeWriter()
            Dim a
    
            Dim lblTicker As New Label
            Me.Controls.Add(lblTicker)
    
    
    
            For a = 1 To txtTicker.Length
                lblTicker.Text = txtTicker.Substring(0, a) & "_"
                System.Threading.Thread.Sleep(100)
                lblTicker.Update()
            Next
        End Sub
    
        Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
            Me.Refresh()
        End Sub
    End Class

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting the XmlTextReader to plain text

    Quote Originally Posted by cary1234 View Post
    I want to convert the XmlTextReader to a plain text reader
    I'm not 100% sure what that means but if you want to read text from a Stream, e.g. FileStream or NetworkStream, then you can use a StreamReader. StreamReader inherits TextReader but XmlTextReader, despite the name, does not.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: Converting the XmlTextReader to plain text

    Quote Originally Posted by jmcilhinney View Post
    I'm not 100% sure what that means but if you want to read text from a Stream, e.g. FileStream or NetworkStream, then you can use a StreamReader. StreamReader inherits TextReader but XmlTextReader, despite the name, does not.
    Oh, sorry. What I mean is instead of depending on the xml file type I want that reader to depend on the string variable.

    Instead of this code

    Code:
    Private Sub loadthenews()
            Dim readXML As New XmlTextReader(Directory.GetCurrentDirectory & "\news.xml")
            While readXML.Read()
                If readXML.NodeType = XmlNodeType.Text Then
                    str += " " & readXML.Value
    
                End If
            End While
        End Sub
    I want to change it to something like this

    Private Sub loadthenews()
    Dim news As String = "The quick brown fox jumps over the lazy dog"
    'Read the news variable letter by letter so I can ticker it.
    End Sub

    I'm not really sure about the words and terms that I am using. I hope you understand.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Converting the XmlTextReader to plain text

    If you need actual TextReader functionality then you could use a StringReader, which works much like a StreamReader but with an existing String as a source. Otherwise, you could just pick out the characters yourself. You can call ToCharrArray on your String to get an array of Char or you can pretty much treat the String as though it's an array already. You can use a For Each loop to enumerate the Chars in a String or use a For loop and get Chars by index.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Converting the XmlTextReader to plain text

    Quote Originally Posted by cary1234 View Post
    I want to convert it to something simple like this

    Code:
    Private Sub loadthenews()
            Dim news As String = "The quick brown fox jumps over the lazy dog"
            'Read the news variable letter by letter so I can ticker it.
        End Sub

    It seems the paint event is drawing the text from the str variable, so just set that variable,
    Code:
        Private Sub loadthenews()
            str = "The quick brown fox jumps over the lazy dog"
        End Sub

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: Converting the XmlTextReader to plain text

    Oh! I didn't realize that. Thank you so much, that really solves the problem.

  7. #7
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Converting the XmlTextReader to plain text

    Quote Originally Posted by cary1234 View Post
    Oh! I didn't realize that. Thank you so much, that really solves the problem.
    This should basically do the same thing,
    Code:
    Public Class Form1
        Private fo As Font = New Font("SEGOE UI", 16, FontStyle.Regular, GraphicsUnit.Point)
        Private scrollLeftPos As Integer ' keep track of text left start position.
        Private scrollText As String = "The quick brown fox jumps over the lazy dog"  ' text to scroll
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True
            scrollLeftPos = Me.ClientSize.Width ' set scrolling start position to far right
            Timer1.Interval = 1  ' timer to scroll text
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Me.Invalidate() ' update/draw text
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim textformatflag As TextFormatFlags = TextFormatFlags.Top
            TextRenderer.DrawText(e.Graphics, scrollText, fo, New Rectangle(scrollLeftPos, 0, Me.ClientSize.Width - scrollLeftPos, Me.ClientSize.Height), Color.Blue, textformatflag)
            scrollLeftPos -= 1
            If scrollLeftPos > Me.ClientSize.Width OrElse scrollLeftPos < (-(TextRenderer.MeasureText(e.Graphics, scrollText, fo, New Size(Integer.MaxValue, Integer.MaxValue), textformatflag)).Width) Then
                scrollLeftPos = Me.ClientSize.Width
            End If
        End Sub
    
    End Class

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: [RESOLVED] Converting the XmlTextReader to plain text

    Thank you so much EdgeMeal, it's working great but with less code. The final thing that I want to do now is to apply that ticker message inside the textbox text property.

    I've tried changing every `clientSize` to `TextBox1` but I realize that the Form1_Paint is the one that is responsible for painting the text inside the form itself.

    Code:
    Public Class Form1
    
        Private fo As Font = New Font("SEGOE UI", 16, FontStyle.Regular, GraphicsUnit.Point)
        Private scrollLeftPos As Integer ' keep track of text left start position.
        Private scrollText As String = "The quick brown fox jumps over the lazy dog"  ' text to scroll
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Me.DoubleBuffered = True
            scrollLeftPos = Me.TextBox1.Width ' set scrolling start position to far right
            Timer1.Interval = 1  ' timer to scroll text
            Timer1.Start()
        End Sub
    
        Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
            Me.Invalidate() ' update/draw text
        End Sub
    
        Private Sub Form1_Paint(sender As Object, e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
            Dim textformatflag As TextFormatFlags = TextFormatFlags.Top
            TextRenderer.DrawText(e.Graphics, scrollText, fo, New Rectangle(scrollLeftPos, 0, Me.TextBox1.Width - scrollLeftPos, Me.TextBox1.Height), Color.Blue, textformatflag)
            scrollLeftPos -= 1
            If scrollLeftPos > Me.TextBox1.Width OrElse scrollLeftPos < (-(TextRenderer.MeasureText(e.Graphics, scrollText, fo, New Size(Integer.MaxValue, Integer.MaxValue), textformatflag)).Width) Then
                scrollLeftPos = Me.TextBox1.Width
            End If
        End Sub
    
    End Class

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: [RESOLVED] Converting the XmlTextReader to plain text

    I've tried changing some of the codes again and it seems that adapting it to textbox1 text property is hard and will affect everything.

  10. #10
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: [RESOLVED] Converting the XmlTextReader to plain text

    Quote Originally Posted by cary1234 View Post
    I've tried changing some of the codes again and it seems that adapting it to textbox1 text property is hard and will affect everything.
    Not sure a textbox would be the best control but I posted similar example but using a picbox in post #8 here.

    Some other scrolling text examples here.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Oct 2009
    Posts
    253

    Re: [RESOLVED] Converting the XmlTextReader to plain text

    Thank you so much Edgemeal for your assistance!

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width