[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.
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.
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?
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.
Re: [2008] Text Editor With No Textbox!
So would I replace Textbox3.Text With Readalltext or what?
Re: [2008] Text Editor With No Textbox!
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...
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")
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:
'Put this at the top of your form class (right after "Public Class <formname>")
'This is the variable that will hold the text
Dim strText As String
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
If Me.OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
'Here we assign the text of the file to the string variable
strText = IO.File.ReadAllText(OpenFileDialog1.FileName)
End If
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:
strText = strText.Replace(" ", ".")
That is all. So basically instead of using your textbox to store the string, you are now using a public variable.
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!
Re: [2008] Text Editor With No Textbox!
vb Code:
io.File.WriteAllText(filename, contents as string)
Re: [2008] Text Editor With No Textbox!
Now what about teh savefiledialog?
Re: [2008] Text Editor With No Textbox!
It works in the exact same way as the OpenFileDialog.
Re: [2008] Text Editor With No Textbox!
Ok ya I honestly do not know...
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!
Re: [2008] Text Editor With No Textbox!