Link to home
Start Free TrialLog in
Avatar of smano
smano

asked on

Display TXT file in Textbox (Multiline)

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.
 User generated image
<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

Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

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
Avatar of smano
smano

ASKER

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...
Avatar of smano

ASKER

I would appreciate it!
ASKER CERTIFIED SOLUTION
Avatar of Jesus Rodriguez
Jesus Rodriguez
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of smano

ASKER

Thanks! Had to leave early will get back to you asap. I quickly glanced and getting the idea.
HAPPY THANKSGIIVING!!!!
Avatar of smano

ASKER

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