TextBox Values not updating on click command - vb.net

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

Related

spell checking richtextbox in winforms project

I have a WinForms project that contains a RichTextBox (RTB) written with VB
I have set ShortcutsEnabled = FALSE in the RTB
To use any Spell Checker I am guessing this would need to set to TRUE
That is NOT my question! I have been reading for way more hours than I care to admit
With the understanding that Spell Checking is easy if you have a ASP.Net OR WPF project
Well I don't so here are the three candidates from NuGet NONE of these candidates offer much help
WeCantSpell.Hunspell and VPKSoft.SpellCheckUtility and NetSpell
I am not asking for a recommendation
Because I can not find a tutorial and am clueless on how to implement these Add In's with code
As well as NOT knowing if they are compatible with WinForms
I even looked at this CP post
CP LINK
Just a suggestion how to use one of these Add In's OR how to add spell checking to the RTB?
To achieve spell checking, you can try Nuget Package NHunspell.
First, you need to add "NHunspell" from "NuGet" and import it. The specific operation is as follows:
Right click the Reference and select "Manage NuGet Packages...", then type "NHunspell " in the search bar and install it:
Second step, you need to create a folder to store ".aff" and ".dic" like this.
Download the "zip" containing the corresponding file, you can access this site.
Here is a demo you can refer to.
Private Sub btCheck_Click(sender As Object, e As EventArgs) Handles btCheck.Click
Dim affFile As String = AppDomain.CurrentDomain.BaseDirectory & "../../Dictionaries/en_us.aff"
Dim dicFile As String = AppDomain.CurrentDomain.BaseDirectory & "../../Dictionaries/en_us.dic"
lbSuggestion.Items.Clear()
lbmorph.Items.Clear()
lbStem.Items.Clear()
Using hunspell As New Hunspell(affFile, dicFile)
Dim correct As Boolean = hunspell.Spell(TextBox1.Text)
checklabel.Text = TextBox1.Text + " is spelled " & (If(correct, "correct", "not correct"))
Dim suggestions As List(Of String) = hunspell.Suggest(TextBox1.Text)
countlabel.Text = "There are " & suggestions.Count.ToString() & " suggestions"
For Each suggestion As String In suggestions
lbSuggestion.Items.Add("Suggestion is: " & suggestion)
Next
Dim morphs As List(Of String) = hunspell.Analyze(TextBox1.Text)
For Each morph As String In morphs
lbmorph.Items.Add("Morph is: " & morph)
Next
Dim stems As List(Of String) = hunspell.Stem(TextBox1.Text)
For Each stem As String In stems
lbStem.Items.Add("Word Stem is: " & stem)
Next
End Using
End Sub
The result,
Hope this can help you.

How to get a form field to auto fill when a product is entered?

I am trying to get my form to populate the days needed for testing automatically when I enter a new record but it keeps erring out. I am very new to using VBA and Access 2016.
I have looked at some other examples that people have posted that work and cannot get it to work.
I am continually getting debugger.
Option Compare Database
Private Sub Fill_SKU_AfterUpdate()
PopulateFields
End Sub
Private Sub PopulateFields()
frmSerialTracerLog.Days_Used_For_Off_Test = DLookup("Days_Used_For_Off_Test", "tblTestDays", "Fill_SKU = '" & frmSerialTracerLog.Fill_SKU & "'")
End Sub
You probably are referring to the current form, thus use Me:
Private Sub PopulateFields()
Me!Days_Used_For_Off_Test.Value = DLookup("Days_Used_For_Off_Test", "tblTestDays", "Fill_SKU = " & Me!Fill_SKU.Value & "")
End Sub

Writing and Reading DataGridView to text file

I'm looking for some assistance on how to populate a DataGridView control with data from a text file. The column number is static, but the row count is dynamic. I am using StreamWriter to create the text file line by line. Here is the code:
Private Sub btnSaveReport_Click(sender As Object, e As EventArgs) Handles btnSaveReport.Click
Dim path As String = "c:\users\service5\desktop\ReportFile.txt"
Using writer As New StreamWriter(path)
For row As Integer = 0 To dgvLabor.RowCount - 2
For col As Integer = 0 To dgvLabor.ColumnCount - 1
writer.WriteLine("Labor" & "|" & dgvLabor.Rows(row).Cells(col).Value)
Next
Next
writer.Close()
End Using
End Sub
The output file looks like this:
Labor|Mon 09/25/2017
Labor|Labor Time
Labor|11:42 PM
Labor|12:42 AM
Labor|23.00
Labor|20
I'm stumped when using the StreamReader method to read each line from the text file and place it in the correct column and row. I had success populating text boxes using the "|" delimited format, but I'm not sure how to go about tackling the DataGridView code. Would it be wise to reformat my StreamWriter method to make the file comma delimited instead of "|" delimited?
UPDATE 10/08/2017
Ok, I have the writer formatting the text file the way I want and the StreamReader is 95% working. It successfully populates the DataGridView for the first row, but it needs to recognize every line beginning with "Labor" and add a new row with the array in the line. Here is a sample of the text I'm trying to read into the DataGridView.
Labor,Sun 10/08/2017,Labor Time,12:39 AM,12:39 AM,0.00,0
Labor,Mon 10/09/2017,Travel Time,12:39 AM,12:39 AM,0.00,0
Here is the StreamReader code:
Private Sub OpenFileDialog1_FileOk(sender As Object, e As CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim FileReader As StreamReader
Dim prop(2) As String
Dim path As String = OpenFileDialog1.FileName
FileReader = New StreamReader(path, False)
Do Until FileReader.EndOfStream
prop = FileReader.ReadLine().Split(",")
If String.Equals(prop(0), "Labor") Then
Dim col(6) As String
Dim index = dgvLabor.Rows.Add()
col = FileReader.ReadLine().Split(",")
dgvLabor.Rows(index).SetValues(col(1), col(2), col(3), col(4), col(5), col(6))
End If
Loop
FileReader.Close()
End Sub
The part I'm having trouble with is the number of lines beginning with "Labor" will vary every time the text file is written, so I think I need a Do Until or Do While loop within the If String.Equals method, but I cannot get the syntax right. I've tried these so far with no success:
Do While String.Equals("Labor")
Do Until String.Equals(Not "Labor")
Do Until String.Equals(prop(0), "Labor")
Any ideas?
UPDATE WITH SOLUTION 10/09/2017
After really thinking about what my code is doing, I finally solved the problem. It's stupid simple and proves how much I was overthinking everything. Thanks for your assistance and patience with me, jmcilhinney. Hope this can help someone else out as well.
ElseIf String.Equals(prop(0), "Labor") Then
dgvLabor.Rows.Add(prop(1), prop(2), prop(3), prop(4), prop(5), prop(6))
End If
After really thinking about what my code is doing, I finally solved the problem. It's stupid simple and proves how much I was overthinking everything. Thanks for your assistance and patience with me, jmcilhinney. Hope this can help someone else out as well.
ElseIf String.Equals(prop(0), "Labor") Then
dgvLabor.Rows.Add(prop(1), prop(2), prop(3), prop(4), prop(5), prop(6))
End If
This code takes the already split line and inserts the desired objects of the array into the next available row's cells. It only adds the lines in the text document that begin with "Labor"

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

Basic URI (Parsing?)

I have been helped by the good people of stackoverflow many times before, so here's my problem...
I haven't been coding for a GOOD while, and for class, we are going to be starting Visual Basic. Visual Basic is really not that hard, but I am not familiar with it, and can't think of a proper way to do this.
As an exercise, I'm coding a very simple web browser. Here's my issue...
Private Sub Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Send.Click
Dim input As String = TextBox1.Text
Me.WebBrowser1.Navigate(New Uri(input))
If the user types "www.youtube.com" in the address bar, they throw an exception (I presume because there is no http:// at the beginning) However, I can't simply add "http://" to the beginning of the string, because then there is the chance for a double up.
How can I check the string for "http://" and add it accordingly?
You can use regular expression to validate the URL/URI.
Dim pattern = "http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?"
Dim Inputurl = "http://www.abc.com/aa"
If Regex.IsMatch(Inputurl, pattern) Then
'
Else
'
End If
Or use String.StartsWith() method,
If Inputurl.StartsWith("http://") Then
'
End If
You need to do something like this:
Dim value As String = Mid(input, 1, 7)
if value = "http://" then
'you don't need to modifie the url
else
'you add your http:// string normaly
EndIf
Hope this can help you
PS: sory I corrected some mistakes I maid