Solved

Display TXT file in Textbox (Multiline)

Posted on 2010-11-24
6
443 Views
Last Modified: 2012-06-27
Need HELP to complete the following task: To display a  TXT file in multiline textbox (THIS IS DONE - See attached code), however now if text file is large (over 100 lines) I need to divide the file into pages, then show each page.  

SEE IMAGE for description and my code is attached. Again, I need help to complete the pages task.
 APPROX LAYOUT. See attached code - Default.aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
	 	<!-- begin form table -->
		<table width="600" cellpadding="4" cellspacing="0" border="0">
		
		<tr>
		<td colspan="2" class="formTitle">File Content:<br />
		<table width="320" cellpadding="1" cellspacing="0">
		<tr>
			<td><asp:Button ID="btnTop" Text=" Top " runat="server" /></td>
			<td><asp:Button ID="btnPrevPage" Text="< Previous" runat="server" /></td>
			<td><asp:Button ID="btnNextPage" Text="Next >" runat="server" /> </td>
		</tr>
		</table>
		<asp:TextBox ID="txtFileContent" runat="server" Width="600" 
				Height="260" TextMode="MultiLine" ></asp:TextBox></td>
	</tr>
	</table>
    </div>
    </form>
</body>
</html>

Open in new window

Imports System.IO


Partial Public Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        txtFileContent.Text = GetContent("C:\TXTdir\sample_file.txt")
    End Sub
    Private Function GetContent(ByVal Path As String) As String
        Dim finalResult As String = String.Empty
        Dim maxLine As Integer = 15
        Dim reader As StreamReader = Nothing
        Dim line As String = String.Empty
        Dim sb As New StringBuilder()

        Try
            reader = GetFile(Path)
            If Not reader Is Nothing Then
                While (ReadLineValue(line, reader.ReadLine())) IsNot Nothing
                    sb.Append(line & vbCrLf)
                End While
                finalResult = sb.ToString()
                reader.Close()
                reader.Dispose()

            End If
        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType, "", "<script language='javascript'>alert('" & ex.Message & "');</script>")
        End Try
        Return finalResult

    End Function
    Protected Sub btnNextPage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnNextPage.Click

    End Sub

    Protected Sub btnPrevPage_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnPrevPage.Click

    End Sub

    Protected Sub btnTop_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnTop.Click

    End Sub
    Public Function GetFile(ByVal Path As String) As StreamReader
        Dim fileReader As StreamReader = Nothing

        If File.Exists(Path) Then
            Dim input As New FileStream(Path, FileMode.Open)
            fileReader = New StreamReader(input)
        End If
        Return fileReader
    End Function

    Public Shared Function ReadLineValue(Of T)(ByRef target As T, ByVal value As T) As T
        target = value
        Return value
    End Function
End Class

Open in new window

0
Comment
Question by:smano
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 13

Expert Comment

by:Jesus Rodriguez
ID: 34207939
My suggestion is read the whole text and each line put it inside an array variable for example
Line(#Lines) then count the number of lines and do a cycle for showing and activating or counting the pagination on the textbox.

I'll modify your code and I will posted here
0
 
LVL 1

Author Comment

by:smano
ID: 34207970
Forgot to add the sample .TXT file   sample-file.txt
Per above code I createad TXTdir folder on C: and moved the file there. This is the path shown on page_load event when calling GetContent method

Also Either C# or VB.NET works for me...
0
 
LVL 1

Author Comment

by:smano
ID: 34207977
I would appreciate it!
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
Jesus Rodriguez earned 500 total points
ID: 34208418
Here is your code my friend. I'm assuming that the text file alwasy will have more than 15 line if not you need to do a small variation.
Imports System.IO
Partial Class Default8
    Inherits System.Web.UI.Page
    Public I As Integer = 1
    Public finalResult(1000) As String
    Private StartPgCounter As New Integer
    Private EndPgCounter As New Integer


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            btnNextPage.Enabled = False
            btnPrevPage.Enabled = False

            Session.Add("AmountofLines", GetContent("d:\sample.txt"))
            If Session("AmountofLines") > 15 Then
                btnNextPage.Enabled = True
                Dim k As Integer
                For k = 1 To 15
                    Me.txtFileContent.Text += Session("finalResult" & k) & vbCrLf
                Next
                Session.Add("StartPgCounter", 1)
                Session.Add("EndPgCounter", 15)
            Else
                btnNextPage.Enabled = False
            End If
        Else
        End If
    End Sub
    Public Function GetContent(ByVal Path As String) As Integer
        Try
            Dim oRead As System.IO.StreamReader
            oRead = File.OpenText(Path)
            While oRead.Peek <> -1
                Session.Add("finalResult" & I, oRead.ReadLine())
                I += 1
            End While
            oRead.Close()

        Catch ex As Exception
            ClientScript.RegisterStartupScript(Me.GetType, "", "<script language='javascript'>alert('" & ex.Message & "');</script>")
        End Try
        Return I
    End Function
    

    Protected Sub btnNextPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnNextPage.Click
        Me.Session("StartPgCounter") = Me.Session("EndPgCounter") + 1
        If Me.Session("StartPgCounter") > Session("AmountofLines") Then
            Me.Session("StartPgCounter") = Session("AmountofLines")
            btnNextPage.Enabled = False
        Else
            btnNextPage.Enabled = True
        End If
        Session("EndPgCounter") = Session("StartPgCounter") + 15
        If Me.Session("EndPgCounter") > Session("AmountofLines") Then
            Me.Session("EndPgCounter") = Session("AmountofLines")
            btnNextPage.Enabled = False
        Else
            btnPrevPage.Enabled = True
        End If
        Dim k As Integer
        Me.txtFileContent.Text = ""
        For k = Me.Session("StartPgCounter") To Me.Session("EndPgCounter")
            Me.txtFileContent.Text += Session("finalResult" & k) & vbCrLf
        Next

        If Me.Session("EndPgCounter") = Session("AmountofLines") Then
            btnNextPage.Enabled = False
        End If
    End Sub

    Protected Sub btnPrevPage_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevPage.Click
        Me.Session("StartPgCounter") = Me.Session("StartPgCounter") - 16
        If Me.Session("StartPgCounter") < 1 Then
            Me.Session("StartPgCounter") = 1
            btnPrevPage.Enabled = False
        Else
            btnPrevPage.Enabled = True
        End If
        Session("EndPgCounter") = Session("StartPgCounter") + 15
        If Me.Session("EndPgCounter") > Session("AmountofLines") Then
            Me.Session("EndPgCounter") = Session("AmountofLines")
            btnNextPage.Enabled = True
        Else
            btnPrevPage.Enabled = True
            btnNextPage.Enabled = True
        End If
        Dim k As Integer
        Me.txtFileContent.Text = ""
        For k = Me.Session("StartPgCounter") To Me.Session("EndPgCounter")
            Me.txtFileContent.Text += Session("finalResult" & k) & vbCrLf
        Next

        If Me.Session("StartPgCounter") = 1 Then
            btnPrevPage.Enabled = False
        End If
    End Sub

    Protected Sub btnTop_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnTop.Click

        btnNextPage.Enabled = True
        Me.txtFileContent.Text = ""
        Dim k As Integer
        For k = 1 To 15
            Me.txtFileContent.Text += Session("finalResult" & k) & vbCrLf
        Next
        Session.Add("StartPgCounter", 1)
        Session.Add("EndPgCounter", 15)
    End Sub
End Class

Open in new window

0
 
LVL 1

Author Comment

by:smano
ID: 34210142
Thanks! Had to leave early will get back to you asap. I quickly glanced and getting the idea.
HAPPY THANKSGIIVING!!!!
0
 
LVL 1

Author Comment

by:smano
ID: 34234407
Appreciate your HELP! Modified bunch of stuff but overall good idea! And holy Sessions, batman :-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Developer portfolios can be a bit of an enigma—how do you present yourself to employers without burying them in lines of code?  A modern portfolio is more than just work samples, it’s also a statement of how you work.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question