VB.NET RTB line counter? - vb.net

I'm trying to make a simple line counter for a rich text box control. Right now, I have a label to the left of the box, using this code:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Label2.Text = RichTextBox1.Lines.Count()
End Sub
This works fine. Same as on a button, it will just update the number. The issue i'm having is using this code:
Private Sub RichTextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RichTextBox1.TextChanged
Label2.Text = Label2.Text + RichTextBox1.Lines.Count() + Environment.NewLine
I get:
An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll
Additional information: Conversion from string "
" to type 'Double' is not valid.
Any fix? Thanks

The operator to concatenate strings in VB.NET is the & character, not the + character. The RichTextBox1.Lines.Count() returns a value of Integer type; in order to return a string representation of that value you should call the ToString() method.
try using this instead:
Label2.Text &= RichTextBox1.Lines.Count.ToString() & Environment.NewLine
That will append the line count value to the end of the label every time the code is executed. However if you are making a line counter that will simply report the number of lines in the RichTextBox, I would suggest using the following:
Label2.Text = RichTextBox1.Lines.Count.ToString()

Can you try like,
Label2.Text = Label2.Text + System.Convert.ToString(RichTextBox1.Lines.Count()) + Environment.NewLine

Related

Add a completely different line with every button click

Complete noob to vb.net (and programming in general) here, all I really want is every time I click a button, the number in the textbox is added by 1 but the new number shows up on the next line. Tried to google this a hundred times but nothing really helped.
I don't want to use loops as I don't want all numbers to show up at once, only for the added number to show up after clicking a specific button (on a new line).
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtoutput As String = ""
Dim a As Integer = 1
txtoutput &= "the value of a =" & a & Environment.NewLine
a = a + 1
TextBox1.Text = txtoutput
End Sub
You are replacing the Text, you want to append a new line, so you need to do:
Private a As Int32 = 0
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a += 1
Dim newLine = $"the value of a = {a}"
TextBox1.Text = TextBox1.Text & Environment.NewLine & newLine
End Sub
You also have to use a field and not a local variable if you want to retain the old value and increment it. Otherwise it is reset always to it's inital value.
Please try to change dim a to static a
Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim txtoutput As String = ""
Static a As Integer = 1
txtoutput &= "the value of a =" & a & Environment.NewLine
a = a + 1
TextBox1.Text = txtoutput
End Sub

How do you change the value in my.settings in a form when you enter numbers in a textbox in VB.Net

I was wondering if you could help me? My question is that, is there a way of changing the value in my.Settings in a form if you enter a number/decimal in a textbox and click a button and then update in the settings to be then changed in another from which is linked to my.Settings in a variable?!
Form 1:
Public Class frmConverter
Dim input As String
Dim result As Decimal
Dim EUR_Rate As Decimal = My.Settings.EUR_Rates
Dim USD_Rate As Decimal = 1.6
Dim JYP_Rate As Decimal = 179.65
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
input = txtInput.Text
Try
If ComboBox1.Text = "£" Then
Pounds()
ElseIf ComboBox1.Text = "€" Then
Euros()
ElseIf ComboBox1.Text = "$" Then
Dollars()
ElseIf ComboBox1.Text = "¥" Then
Yen()
End If
Catch es As Exception
MsgBox("Error!")
End Try
End Sub
Private Sub btnSettings_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSettings.Click
Me.Hide()
frmExchange.Show()
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
txtInput.Text = ""
lblResult.Text = ""
End Sub
Function Pounds()
If ComboBox1.Text = "£" And ComboBox2.Text = "€" Then
result = (input * EUR_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "$" Then
result = (input * USD_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
ElseIf ComboBox1.Text = "£" And ComboBox2.Text = "¥" Then
result = (input * JYP_Rate)
lblResult.Text = FormatNumber(result, 2) & " " & ComboBox2.Text
End If
Return 0
End Function
Form 2:
Public Class frmExchange
Private Sub frmExchange_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
My.Settings.EUR_Rates = (txtinput.Text)
My.Settings.Save()
My.Settings.Reload()
End Sub
Public Sub SetNewRate(ByVal rate As Decimal)
txtinput.Text = rate.ToString
End Sub
Private Sub btnchange_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnchange.Click
If ComboBox1.Text = "€" Then
My.Settings.USD_Rates = (txtinput.Text)
frmConverter.SetNewRate(txtinput.Text)
End If
End Sub
End class
It sounds like you are trying to use My.Settings as some sort of set of global reference variables. Thats not what they are for, not how they work and not what they are.
First, turn on Option Strict as it looks like it may be Off. This will store the decimal value of a textbox to a Settings variable which is defined as a Decimal:
My.Settings.USD_Rates = CDec(SomeTextBox.Text)
Thats all it will do. It wont save the value and it wont pass it around or share it with other forms and variables.
My.Settings.Save 'saves current settings to disk for next time
My.Settings.Reload 'Load settings from last time
This is all covered on MSDN. There is no linkage anywhere. If you have code in another form like this:
txtRate.Text = My.Settings.USD_Rates.ToString
txtRate will not automatically update when you post a new value to Settings. There are just values not Objects (see Value Types and Reference Types). To pass the new value to another form:
' in other form:
Public Sub SetNewRate(rate As Decimal)
' use the new value:
soemTextBox.Text = rate.ToString
End Sub
in form which gets the change:
Private Sub btnchangeRate(....
' save to settings which has nothing to do with passing the data
My.Settings.USD_Rates = CDec(RateTextBox.Text)
otherForm.SetNewRate(CDec(RateTextBox.Text))
End Sub
You may run into problems if you are using the default form instance, but that is a different problem.
You botched the instructions. The 2 procedures are supposed to go in 2 different forms - one to SEND the new value, one to RECEIVE the new value. With the edit and more complete picture, there is an easier way.
Private Sub btnSettings_Click(...) Handles btnSettings.Click
' rather than EMULATE a dialog, lets DO a dialog:
'Me.Hide()
'frmExchange.Show()
Using frm As New frmExchange ' the proper way to use a form
frm.ShowDialog
' Im guessing 'result' is the xchg rate var
result = CDec(frm.txtInput.Text)
End Using ' dispose of form, release resources
End Sub
In the other form
Private Sub btnchange_Click(....)
' do the normal stuff with Settings, if needed then:
Me.Hide
End Sub

Output on label involving string

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
FirstNumber = TextBox1.Text
SecondNumber = TextBox2.Text
Message = "The results are as follows:" & vbNewLine
Message = Message & "Addition:" & FirstNumber + SecondNumber
Label3.Text = Message
End Sub
I am using the code above in a program where I have declared Message As String. But why is it that on the second line the text "The results are as follows" doesn't appear even though I have included Message as part of the line?
Maybe because your label isn't high enough to show two lines of text. Increase Label3.Height and it will be fixed.
Label3.Height = 30 ' 30 is an example of course

Change every character in textbox

I am having a problem with string replacement. Below is my code as of now. I want to replace each character in textbox1 and write it to textbox2, but this only works for the last character.
If I write:
Hello
Then it should end up as:
[[h]][[e]][[l]][[l]][[o]]
Public Class Form1
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
Try
TextBox2.Text = TextBox1.Text.Replace("0"c, "[[something0a0]]")
TextBox2.Text = TextBox1.Text.Replace("1"c, "[[something1a1]]")
TextBox2.Text = TextBox1.Text.Replace("2"c, "[[something2a2]]")
TextBox2.Text = TextBox1.Text.Replace("3"c, "[[something3a3]]")
Catch ex As Exception
End Try
End Sub
End Class
You're overwriting the value of TextBox2. Chain your Replace calls instead and set the assignment once.
TextBox2.Text = TextBox1.Text.Replace("0"c, "[[something0a0]]")
.Replace("1"c, "[[something1a1]]")
.Replace("2"c, "[[something2a2]]")
.Replace("3"c, "[[something3a3]]")
A way you could do this is using a loop like this. Not sure if it's the most efficient, but it's fairly easy to understand:
TextBox2.Text = ""
For Each chr As Char In TextBox1.Text
TextBox2.Text += "[[" & chr & "]]"
Next
And another simple way is:
TextBox2.Text = "[[" & String.Join("]][[ ", TextBox1.Text.ToCharArray().AsEnumerable()) & "]]"
HTH

VB.NET Count line number in textbox

I use Fileystemwatcher to watch a certain folder on our network share and wan't to count the lines in a textbox.
I have 2 richtextboxes, the first one will display the line number and the second one displays the change/deleted/renamed file.
I am able to display the total of lines in a label, but also want to show the line numbers in a richtextbox, but the problem is that it adds the number to the current, it needs to add the number on a new line.
Hopefully the image will explain.
Private Sub textFolderActiviteit_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textFolderActiviteit.TextChanged
textFolderActiviteit.SelectionStart = textFolderActiviteit.Text.Length
textFolderActiviteit.SelectionLength = 0
textFolderActiviteit.ScrollToCaret()
textFolderActiviteit.Focus()
Dim currentLineIndex As Integer = textFolderActiviteit.GetLineFromCharIndex(textFolderActiviteit.SelectionStart)
Me.Label2.Text = String.Format("{0}", currentLineIndex + 1)
Me.textLineCount.Text = String.Format("{0}", currentLineIndex + 1 & vbCrLf)
End Sub
Private Sub textLineCount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles textLineCount.TextChanged
textLineCount.SelectionStart = textLineCount.Text.Length
textLineCount.SelectionLength = 0
textLineCount.ScrollToCaret()
textLineCount.Focus()
End Sub
Maybe I'm misunderstanding the question, but couldn't you just put the VbCrlf before the line number?
Me.textLineCount.Text = String.Format("{0}", vbCrLf & (currentLineIndex + 1).ToString())