Vb.net Get the text from Textbox inserted line by line in other Textboxes - vb.net

Main PageI made a program that gives the user the possibility to insert text into text boxes and then it gets saved as a Text-file. Now I want to give the user a possibility to read the values from file back to the Text boxes. I though about importing the whole text into a big multi-line-textbox( I called it "mothertext") and from there import it into other textboxes..
I think about something that works like:
textbox1.text = mothertext.text.line (1)
textbox2.text = mothertext.text.line (2)
textbox3.text = mothertext.text.line (3)
Is there a way that works that way or similar?
Thanks a lot if you can help me :)
"Für später speichern"= Save for later/later editing
"Öffnen"= Open( *.txt file)
Thats the last function, seems so easy but... yah

Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
Static count As Integer
count = count + 1
OpenFileDialog1.InitialDirectory = My.Computer.FileSystem.SpecialDirectories.MyDocuments
OpenFileDialog1.Filter = "All Files (*.*)|*.*|Excel files (*.xlsx)|*.xlsx|CSV Files (*.csv)|*.csv|XLS Files (*.xls)|*xls"
If (OpenFileDialog1.ShowDialog(Me) = System.Windows.Forms.DialogResult.OK) Then
Dim read As IO.StreamReader
read = IO.File.OpenText(OpenFileDialog1.FileName)
TextBox17.Text = read.ReadToEnd()
read.Close()
Dim readLines() As String = IO.File.ReadAllLines(OpenFileDialog1.FileName)
TextBox1.Text = readLines(0)

Related

VB.NET stopping strings being automatically split by spaces

I am building a program in VB that populates a listbox with all the songs in a chosen folder and opens them with media player when you click on them in the list. All of that works but my problem is that pretty much no matter what I do (I've tested this with WMP, VLC, and Winamp) it splits up the argument string by spaces, for example if the file path is C:\Program Files (x86)\Test song.mp3, it will try to open in sequence: C:\Program, then Files, then (x86)\Test, then song.mp3. It works on file paths containing no spaces, but else it always fails.
Private Sub SongListBox_Click(sender As Object, e As EventArgs) Handles SongListBox.Click
Dim song As String = SongListBox.SelectedIndex
If SongListBox.SelectedIndex = "-1" Then
Else
Dim SongPath As String = Pathlist.Items.Item(song)
Dim SongProcess As New ProcessStartInfo
SongProcess.FileName = "C:\Program Files\Winamp\winamp.exe"
SongProcess.Arguments = SongPath
SongProcess.UseShellExecute = True
SongProcess.WindowStyle = ProcessWindowStyle.Normal
MsgBox(Pathlist.Items.Item(song), MsgBoxStyle.Information, "Song")
Dim SongStart As Process = Process.Start(SongProcess)
SongListBox.Select()
End If
End Sub

Highlight all lines that contain "admin" in a richtextbox

I am Making a chat for my Game launcher, when you launch it up theres a button that Opens a Chat Forum with a richtextbox on it that it refreshes/timer checks when the text has changed in the txt file on the server has changed from textbox1 then it will reprint the text but thats not the problem, i have an admin panel for it, but i want to highlight lines where there is "!ADMINISTRATOR!" in it so it shows the user is an admin and it highlighted to show what he is saying. i've tried this
Dim index As Integer = Me.RichTextBox1.Find("!ADMINISTRATOR!")
If index <> -1 Then
Dim lineindex As Integer = Me.RichTextBox1.GetLineFromCharIndex(index)
Dim first As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex)
Dim last As Integer = Me.RichTextBox1.GetFirstCharIndexFromLine(lineindex + 1)
If last = -1 Then last = Me.RichTextBox1.TextLength
Me.RichTextBox1.Select(first, last - first)
Me.RichTextBox1.SelectionBackColor = Color.Yellow
End If
But that works, in a way, it does not always highlight it, while it will highlight sometimes, it also may highlight the text a just just put, but that i don't mind, the main problem is that it only highlights the First line with the text in it, so if the admin posts a message on it it will highlight <3 but then a user will chat and then admin posts another message, it only highlights the first line that contains that text. if you need any more info oh and for testing purposes im using a local file on my pc, ive tested the ftp it works, but just to save time im using a txt file this is the code i use for the timer
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If (System.IO.File.ReadAllText(Get_Directory_Current() & "\FilesDB\Chat\Chat.txt") = RichTextBox1.Text) Then
Else
Try
RichTextBox1.Text = System.IO.File.ReadAllText(Get_Directory_Current() & "\FilesDB\Chat\Chat.txt")
RichTextBox1.SelectionStart = RichTextBox1.Text.Length
RichTextBox1.ScrollToCaret()
ColorChat() ' this is the color chat that kinda dont work
Catch ex As Exception
End Try
End If
End Sub
if you can help thanks :) but for now i'm just trying new things. i guess it would kinda work a bit like syntax highlighting if you make it highlight a line instead of word.
Oh and if you wanted to know this is how i add the text to the chat / formatting
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
RichTextBox1.AppendText("<[" & Get_Time() & " " & strDate & "] " & "!ADMINISTRATOR!" & " | " & Environment.UserName & "> " & TextBox3.Text & vbNewLine)
End Sub
Use String.Replace() to inject the Rtf formatting where your target string appears.
Dim stringToFind = "!ADMINISTRATOR!"
Dim txt = Me.RichTextBox1.Text
Dim sb = New System.Text.StringBuilder()
sb.Append("{\rtf1\ansi\deff0 {\colortbl;\red0\green0\blue0;\red255\green0\blue0;}")
sb.Append(txt.Replace(stringToFind, String.Format("\cf2{0}\cf1", stringToFind)))
sb.Append("}")
Me.RichTextBox1.Rtf = sb.ToString()

code that doesn't work...Kill-process , search-for-file , streamwrite on file searched file

can you help me with that code ?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x As String = "C:\Users\Andy\Documents\Visual Studio 2008\Projects\minecraft srv\"
For Each app As Process In Process.GetProcesses
If app.ProcessName = "notepad" Then
app.Kill()
End If
Next
Dim result As String
Dim servprop() As String
servprop = System.IO.Directory.GetFiles(x, "server.*")
For Each file In servprop
result = Path.GetFileName(file)
Next
Dim z As String = "C:\Users\Andy\Documents\Visual Studio 2008\Projects\minecraft srv\" & result.ToString
Dim t As StreamWriter = New StreamWriter(z)
t.WriteLine(TextBox1.Text.ToString)
t.Close()
End Sub
so... I got a button (button1) that finds if notepad is opened and kills it.
Then it searches for "server.Properties" in "x" location
if server.properties is found , then "result" will get his name (server)
"z" is the file location where streamwriter must write the text from textbox1 .
And it doesn't work... streamwirter is not writing on server.properties ... why ?
mention : I'm just a kid :D and i'm trying to learn by myself visual basic .
If you have only one file called "server.properties" then you could remove all the code that search for this file and write it directly.
Dim z As String
z = System.IO.Path.Combine(x, "server.properties")
Using t = New StreamWriter(z)
t.WriteLine(TextBox1.Text.ToString)
t.Flush()
End Using
Regarding the error, encapsulating the writing code with a proper using statement ensures a complete clean-up. Also adding a call to Flush() is probably not necessary, but doesn't hurt.

Inputbox input to streamwriter file

So, in my assignment, we're supposed to create phone directories in the Directories.txt file and then change the listings in the directories. I made a blank directories.txt file and placed that in the debug folder. I also have created a button titled Create a New Phone Directory. When a user clicks on it, the inputbox shows up prompting the user to title the new directory. I am wondering how to get the results from that inputbox that the user typed and use that to create a new directory file in directories.txt and display it in a listbox. I think I have to use stream writer but every time I try, the result in the listbox shows up as system.IO.streamwriter.
This is my current code:
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
inputDirectories = InputBox("Please Enter the Name of the New Directory")
If inputDirectories Is "" Then
MessageBox.Show("Invalid Directory Name")
End If
Dim fileDirectories As IO.StreamWriter = IO.File.CreateText(inputDirectories)
fileDirectories.WriteLine(inputDirectories)
End Sub
The assignment instructions say to use to write line method to add the name of the new file to the directories.txt file but I am totally lost on how to do this.
Any help would be appreciated!
Private Sub btnCreate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreate.Click
'
Dim path As String = "c:\temp\MyTestDirectory.txt"
Dim fileDirectories As System.IO.StreamWriter
Dim inputDirectory As String = ""
'
inputDirectory = InputBox("Please Enter the Name of the New Directory")
If inputDirectory = "" Then
MessageBox.Show("Invalid Directory Name")
Else
If System.IO.File.Exists(path) = False Then
'The file does not exist so create a new file & add the inputted data
fileDirectories = System.IO.File.CreateText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
Else
'The file exists so append file with the inputted data
fileDirectories = System.IO.File.AppendText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
End If
End If
'
End Sub
Sub ReadDataBackNow()
' Open the file to read from one line at a time
Dim path As String = "c:\temp\MyTestDirectory.txt"
Dim DataStreamIn As System.IO.StreamReader = System.IO.File.OpenText(path)
Dim TextLines As String = ""
'
Do While DataStreamIn.Peek() >= 0
TextLines = TextLines & DataStreamIn.ReadLine()
Loop
DataStreamIn.Close()
MsgBox(TextLines)
End Sub
UPDATE
Update to answer additional question.
In your button click event add the following line
Listbox1.Items.Add(inputDirectory)
Add the line AFTER the inner IF THEN block so your code would like this
If System.IO.File.Exists(path) = False Then
'The file does not exist so create a new file & add the inputted data
fileDirectories = System.IO.File.CreateText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
Else
'The file exists so append file with the inputted data
fileDirectories = System.IO.File.AppendText(path)
fileDirectories.WriteLine(inputDirectory)
fileDirectories.Flush()
fileDirectories.Close()
End If
Listbox1.Items.Add(inputDirectory)
Note that you will need several files to answer your question, so you may end up with something like
Directories.txt (contains list of directories)
Friends_Directory.txt
Workmates_Directory.txt
Family_Directory.txt
Friends_Directory.txt (contains list of friends)
Bob 1234567890
Angela 2345678901
Steve 3456789012
Ahmed 4567890123
Fatima 5678901234
Workmates_Directory.txt (contains list of workmates)
CEO_Alan 0987654321
Manager_Daisy 0876543219
Foreman_Judy 0765432198
Colleague_Jill 0654321987
Family_Directory.txt
Bro_Malcolm 1122334455
Sis_Alisha 2233445566
Moms 3344556677
Pops 4455667788
Uncle_Ben 5566778899
Aunty_Sarah 6677889900

After importing .csv file into textbox. Special characters shows up as "�"

I am trying to import a .csv file exported from google contacts into a textbox in VB.net. For some reason characters such as é å ä ö turns out as � when imported.
This is the code used to import the file:
Dim ofd As New OpenFileDialog()
ofd.CheckFileExists = True
ofd.CheckPathExists = True
ofd.Filter = "Text Files|*.csv" 'for multiple filters do this:
'ofd.Filter = "Text Files, XML Files|*.txt;*.xml"
If ofd.ShowDialog() = DialogResult.OK Then
Using sr As New StreamReader(ofd.FileName)
txtInput.Text = sr.ReadToEnd()
End Using
End If
How can I fix this?
You're reading the file with the wrong encoding.
Figure out what encoding it really is (probably Encoding.GetEncoding(1252)), then pass the correct Encoding instance to whatever method you're using to the StreamReader constructor.
The .CSV file was encoded with ANSII. Didn't find how to use ANSII while importing so I re-saved the file using UTF-8 so now it works. I'm sure there is a way to do it without having to do it manually like that but this will have to do for now I guess.
Private Sub cmdStore_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdStore.Click
'InsertDBValues()
Dim filename As String = "C:\gProducts.csv"
Dim fields As String()
Dim delimiter As String = ","
Using parser As New TextFieldParser(filename)
parser.SetDelimiters(delimiter)
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
' Add code here to use data in fields variable.
txtName.Text = fields(1).ToString ' likewise give all textbox
End While
End Using
End Sub