Results 1 to 16 of 16

Thread: [2008] Text Editor With No Textbox!

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    [2008] Text Editor With No Textbox!

    How can I make a Button so when clicked a Openfiledialog shows asking to select a file and once clicked it loads the file but it will not be loaded onto a Textbox or Richtextbox and so on. So basically the file is loaded but it is not displayed. And I would like the same thing but it will be saved using a savefiledialog and the hidden text will be saved back onto the same location the opened file is located. Or is there a component to add so I could switch Textbox1 to something else I have tried most but nothing worked.

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

    Re: [2008] Text Editor With No Textbox!

    If you want to display the contents of a file then you have to get the contents of that file as a String, which you can then assign to the Text property of the TextBox. Just don't do the last bit. This is lijke saying you want to make a pizza but you don't want it in a box. To get a pizza in a box you have to make the pizza first. If you don't want it in a box then just don't put it in a box.

    You already know how to use an OpenFileDialog from your other thread. To read a text file just call IO.File.ReadAllText.
    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
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    Soo could do I just leave the code alone? Is it allready secretly loaded just that I don't know it?

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

    Re: [2008] Text Editor With No Textbox!

    ReadAllText returns a String containing the contents of the file. What you do with it is up to you.
    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

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    So would I replace Textbox3.Text With Readalltext or what?

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Text Editor With No Textbox!

    post your code

  7. #7
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Text Editor With No Textbox!

    So you simply want to load the contents of a file, but not display them to the user?
    Just use a variable (string) and assign the result of the ReadAllText function to that variable. To display it to the user you would assign this variable to the .Text property of a textbox. So if you don't want to display it to the user, don't assign the variable to the textbox...

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    Soo would I just remove the Text string saying
    Code:
    Textbox1.Text = sb.ToString?
    Soo if I removed that from
    Code:
        Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim data() As Byte = IO.File.ReadAllBytes(OpenFileDialog1.FileName)
                Dim sb As New System.Text.StringBuilder
            End If
        End Sub
    It will be loaded in the background its just that the user can't see it? One question what if I wanted a code like this
    Code:
    TextBox3.Text = TextBox3.Text.Replace(" ", "")
    Would I just go like
    Code:
     OpenFileDialog1.Text = OpenFileDialog.Text.Replace("Hello", "People")
    Last edited by VB6Learner; Aug 23rd, 2008 at 01:24 AM.

  9. #9
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Text Editor With No Textbox!

    I don't know why you are reading the bytes now suddenly instead of the text, but here is what I think you want:

    vb.net Code:
    1. 'Put this at the top of your form class (right after "Public Class <formname>")
    2. 'This is the variable that will hold the text
    3. Dim strText As String
    4.  
    5. Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    6.         If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
    7.             'Here we assign the text of the file to the string variable
    8.             strText = IO.File.ReadAllText(OpenFileDialog1.FileName)
    9.         End If
    10.     End Sub

    The 'Dim strText As String' line should be right at the top (and not in the Button4_Click event sub) because we want it to be a public variable instead of a local variable. That means that any sub in the current form can access (read and write) that variable, just like you can with a textbox.


    If you now want to use the text you use this variable:
    Let's say you want to replace all spaces in the text with dots for example:
    vb.net Code:
    1. strText = strText.Replace(" ", ".")

    That is all. So basically instead of using your textbox to store the string, you are now using a public variable.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    Yes this is EXACTALY what I want now I just need a Save button to save the hidden text... that shouldent be to hard!

  11. #11
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,424

    Re: [2008] Text Editor With No Textbox!

    vb Code:
    1. io.File.WriteAllText(filename, contents as string)

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    Now what about teh savefiledialog?

  13. #13
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2008] Text Editor With No Textbox!

    It works in the exact same way as the OpenFileDialog.

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    Ok ya I honestly do not know...

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    Ok now I need it in HEX format but I am running in to one error!

    Code:
    Public Class Form1
        Dim StrText As String
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
                Me.StrText = String.Empty
    
                Using streamReader As New IO.StreamReader(OpenFileDialog1.FileName, System.Text.Encoding.Default, True)
                    Array.ForEach(Array.ConvertAll(streamReader.ReadToEnd.ToArray, _
                    New Converter(Of Char, String)(Function(x As Char) Hex(AscW(x)).PadLeft(2, "0"c) & " ")), AddressOf StrText.AppendText)
    
                End Using
            End If
        End Sub
    That was my Openfieldialog and here is the part of my code that is crashing!

    Code:
    New Converter(Of Char, String)(Function(x As Char) Hex(AscW(x)).PadLeft(2, "0"c) & " ")), AddressOf StrText.AppendText)
    It crashes and says AppendText is not a member of String? I mean what else could it be!

  16. #16

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Posts
    493

    Re: [2008] Text Editor With No Textbox!

    How can I fix this?

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