Highlight all lines that contain "admin" in a richtextbox - vb.net

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()

Related

TextBox Values not updating on click command

Im experimenting with upgrading from VBA to VB.NET, so I am learning some pretty basic stuff by figuring out the syntax differences between things I have done and). So far so good, but I cant seem to get a textbox to update the display value in the form. Anyone want to explain why I am dumb? I spent some time surfing the various interweb stuff, but have yet to find anything(unless I am dumb and just didnt understand it when i saw it).
Here's what I have so far:
Dim this As String = Trim$(Mid$(TextBox1.Text, InStr(TextBox1.Text, "&") + 1))
Dim oldtxt As String = TextBox1.Text
If InStr(TextBox1.Text, "&") > 0 Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "End Date Copied" & this)
Clipboard.SetText(this)
Threading.Thread.Sleep(2000)
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, oldtxt)
End If
After digging through the UI of visual studio I was able to find a more "band-aid" type of resolution. Even though I havent messed with SubString function yet, that is my next learning step, I found this option to be sufficient in the immediate resolution. If I find a better way of implementing this I will post back for others to see (if they care).
Private Sub TextBox1_Enter(ByVal sender As Object, ByVal e As System.EventArgs) Handles Block1.Enter
Dim this As String = Trim$(Mid$(TextBox1.Text, InStr(TextBox1.Text, "&") + 1))
Dim oldtxt As String = TextBox1.Text
If InStr(TextBox1.Text, "&") > 0 Then
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, "End Date Copied" & this)
Block1.Refresh()
Clipboard.SetText(this)
Threading.Thread.Sleep(200)
TextBox1.Text = TextBox1.Text.Replace(TextBox1.Text, oldtxt)
Block1.Refresh()
End If
End Sub
And just an FYI for #TEK, Enter and Click events for this work identically, so hopefully next time you try and help someone you wont get hung up on unnecessary details :)

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

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)

vb.net 2010 append text always at top of a richtextbox

I have create a simple function to append text to a Richtextbox. I want that this text is append always on top scrolling the old text to the bottom.
Private Sub BoxMessaggio(ByVal testo As String, ByVal errore As Integer)
Me.ActiveControl = RichTextBox1
RichTextBox1.Focus()
If errore Then
RichTextBox1.SelectionColor = Color.Red
Else
RichTextBox1.SelectionColor = Color.Black
End If
RichTextBox1.AppendText(testo + vbNewLine)
RichTextBox1.SelectionStart = RichTextBox1.Text.Length
'RichTextBox1.Select(RichTextBox1.TextLength, 0)
RichTextBox1.ScrollToCaret()
End Sub
I call the function in this way:
BoxMessaggio(Now + ": " + ex.Message, 1)
I tried a lot of different solutions found here on StackOverflow or in some forums but no one works for me, text is always add at the bottom....
Treat the RichTextBox1.Text as a String (because it is), call Insert, and place the new text at the front of the strong.
RichTextBox1.Text = RichTextBox1.Text.Insert(0, testo + vbNewLine)
As for the scrolling. Don't call Focus or ScrollToCaret and it will stay at the top viewing the most recently added text.

Open, Launch or Show a file for the user to read or write in vb.net

It sounds very simple but I have searched and cannot seem to find a way to open a log file which the user just created from my windows form app. The file exits I just want to open it after it is created.
I have a Dim path As String = TextBox1.Text and once the user names and clicks ok on the savefiledialog I have a msgbox that says "Done" and when you hit OK I have tried this
FileOpen(FreeFile, path, OpenMode.Input) but nothing happens. I just want it to open the log and show it to the user so they can edit or save it again or anything.
This is where I got the above code.
http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.filesystem.fileopen.aspx
Searching is difficult because everyone is trying to "Open" a file and process it during runtime. I am just trying to Show a file by Launching it like someone just double clicked it.
Here is the entire Export Button click Sub. It basically writes listbox items to file.
Private Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Dim sfd As New SaveFileDialog
Dim path As String = TextBox1.Text
Dim arypath() As String = Split(TextBox1.Text, "\")
Dim pathDate As String
Dim foldername As String
foldername = arypath(arypath.Length - 1)
pathDate = Now.ToString("yyyy-MM-dd") & "_" & Now.ToString("hh;mm")
sfd.FileName = "FileScannerResults " & Chr(39) & foldername & Chr(39) & " " & pathDate
sfd.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal)
sfd.Filter = "Text files (*.txt)|*.txt|CSV Files (*.csv)|*.csv"
sfd.ShowDialog()
path = sfd.FileName
Using SW As New IO.StreamWriter(path)
If CkbxFolder.Checked = True Then
SW.WriteLine("Folders")
For Each itm As String In ListBox1.Items
SW.WriteLine(itm)
Next
End If
If CkbxFiles.Checked = True Then
SW.WriteLine("Files")
For Each itm As String In ListBox2.Items
SW.WriteLine(itm)
Next
End If
End Using
MsgBox("Done...")
FileOpen(FreeFile, path, OpenMode.Input) 'Why can't I open a file for you...
End Sub
Do not use the old VB6 methods. They are still here for compatibility reason, the new code should use the more powerful methods in the System.IO namespace.
However, as said in comments, FileOpen don't show anything for you, just opens the file
You coud write
Using sr = new StreamReader(path)
Dim line = sr.ReadLine()
if !string.IsNullOrEmpty(line) Then
textBoxForLog.AppendText(line)
End If
End Using
or simply (if the file is not too big)
Dim myLogText = File.ReadAllText(path)
textBoxForLog.Text = myLogText
As alternative, you could ask the operating system to run the program associated with the file extension and show the file for you
Process.Start(path)
To get the same behavior as if the user double-clicked it, just use System.Diagnostics.Process, and pass the filename to it's Start method:
Process.Start(path)
This will open the file using whatever the default application is for that filename based on its extension, just like Explorer does when you double-click it.

ASCII Conversion Tool Help VB.NET

I'm trying to make something along the lines of the Branah Unicode Converter to decode lines like this for instance: [99,97,108,108,32,99]. http://www.branah.com/unicode-converter. I found this. I'm guessing the form has a textbox and 2 buttons. One is using Asc to get the textbox's ASCII code and the other using Chr to convert the ASCII code in the textbox into a character.
So, last time I posted this, it got closed because it wasn't "a real question". Let me be blunt. I haven NO idea how to get this working. Labeling of things is very confusing. What do I declare Text1, Text2, Text3 and Label 6 as because it's giving me the error that they're not declared? I'm also getting an error that TextBox1 cannot be converted to an integer from this line: Text2 = Text2 + Chr(TextBox1). How do I fix that? Am I not importing something that I should be?
Private Sub EncodeButton_Click()
'Code the character
TextBox1 = Asc(Text1)
'clears text box
Text1 = ""
' adds the coded character to the textbox (Textbox1)
LABEL6 = LABEL6 + "," + TextBox1
' set focus to textbox
Text1.SetFocus
'decodes the coded character
Text2 = Text2 + Chr(TextBox1)
End Sub
Private Sub DecodeButton_Click()
'this part decodes the coded number
TextBox1 = Text3
Text3 = ""
LABEL6 = LABEL6 + "," + TextBox1
Text2 = Text2 + Chr(TextBox1)
Text3.SetFocus
End Sub
Here's how to make this code work in VB.NET:
Create a new Windows Forms project.
In the project there is a form, open it and create the following items:
Three TextBoxes, named Text1, Text2, Text3
Two Buttons, named EncodeButton and DecodeButton
A Label named Label6
Right click the form in solution explorer and select "View Code".
Paste the following code into the code window:
Public Class Form1
Private Sub EncodeButton_Click(sender As System.Object, e As System.EventArgs) Handles EncodeButton.Click
Dim sTextBox1 As String
sTextBox1 = Asc(Text1.Text)
Text1.Text = ""
Label6.Text = Label6.Text & "," & sTextBox1
Text1.Focus()
Text2.Text = Text2.Text + Chr(sTextBox1)
End Sub
Private Sub DecodeButton_Click(sender As System.Object, e As System.EventArgs) Handles DecodeButton.Click
Dim sTextBox1 As Integer
sTextBox1 = Val(Text3.Text)
Text3.Text = ""
Label6.Text = Label6.Text & "," & sTextBox1
Text2.Text = Text2.Text & Chr(sTextBox1)
Text3.Focus()
End Sub
End Class
Run the project.
Type a single letter into Text1 and click EncodeButton
Type a number into Text3 (try one of the numbers that appeared in Label6 first) and click DecodeButton
This should accomplish everything the sample you pasted was designed to accomplish. If you have difficulty with any of these steps, I suggest looking up a good Windows Forms tutorial online.
Part of your problem is probably that you need to understand that controls like TextBoxes have properties and methods.
So, to GET the value from a TextBox called Text1 and SET it to a TextBox called TextBox1 you'd do this:
TextBox1.Text = Asc(Text1.Text)
If you type in the name of a control and hit the fullstop (".") character afterwards, the intellisense will give you a very helpful list of propertyies and methods you can use.
If you've copied this code from somewhere else, you may also need to create a Form, and drag/drop some TextBox controls onto it and give then names to match your code.