I am getting an error in my program.
Too many arguments to Public Function LTrim(str As String) As String.
Dim fortrim As String
Dim trimed As String
fortrim = TextBox1.Text
trimed = LTrim(fortrim, 3)
' ^
' error appears here
TextBox2.Text = trimed
Help is appreciated. I can't find a workaround.
I really suggest you to remove the old VB6 functions and use the more advanced NET equivalent.
string.TrimStart
string.TrimEnd
string.Trim
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
TextBox2.Text = TextBox1.Text.TrimStart(Nothing)
End Sub
EDIT: Instead, (looking at your comment in a different answer) if you want to extract the last 3 chars from a string the method to use is string.Substring and the property string.Length
Dim original = TextBox1.Text
Dim last3 = original
' be sure to have at least 3 chars before doing substring math
If last3.Length > 3 Then
last3 = original.Substring(original.Length - 3, 3)
End If
TextBox2.Text = last3
Well, as it states, you have more arguments than what is expected.
So change
LTrim(fortrim, 3)
to
LTrim(fortrim)
You could also shorten this code to something like
TextBox2.Text = LTrim(TextBox1.Text)
Related
I'm using Visual Basic 2010 Express. I need to parse a string with unprintable characters in it. I need to detect ASCII 4 (End of Trans).
A scanner dumps data into a TextBox in my app. In a loop, I am using:
If Chr(MyString.Chars(counter)) = 4 Then
MsgBox("Found")
End If
This is not the correct syntax but should convey what I'm trying to do.
After the scanner dumps the data into a textbox:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TextBox1.Text = "Some chars coming in from " & Chr(4) & " a scanner"
End Sub
Try something like this:
Dim MyString As String = TextBox1.Text
If MyString.Contains(Chr(4)) Then
MessageBox.Show("Found")
End If
Or even something like this:
Dim MyString As String = TextBox1.Text
Dim counter As Integer = 26
If MyString.Chars(counter) = Chr(4) Then
MessageBox.Show("Found")
End If
I am writing some code in VB.Net to subtract one string from another string, but this is not working. in output nothing is changed in the target string. But there is no error message. Please help. Thanks.
If RadioButton1.Checked Then
TextBox1.Text = ""
positive = (TextBoxp1.Text + TextBoxp2.Text + TextBoxp3.Text)
negative = (TextBoxn1.Text + TextBoxn2.Text + TextBoxn3.Text)
findstring = Replace(positive, negative, "")
TextBox1.Text = findstring
End If
The concatenation symbol in vb.net is the ampersand (&). You may get unexpected results it you use the plus sign and the strings contain numbers. Parenthesis are not necessary to evaluate an expression except to establish order of calculation when it conflicts with order of precedence.
You are using the vb.net Strings.Replace method. I would use the .net String.Replace method because it is easier to move between .net languages when you get used to using .net methods instead of vb specific methods.
This method takes the original string in this case negative and looks for the entire positive string. If it finds the entire string it replaces it with the empty string.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim positive = "b" & "cd" & "ef"
Dim negative = "abc" & "def" & "ghi"
TextBox1.Text = negative.Replace(positive, "")
'Result is aghi
End Sub
If you are trying to remove individual letters from a string then you will have to use a loop. Luckily for us a String is an array of Char.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim positive = "ceg"
Dim negative = "abcdefg"
For Each ch As Char In positive
negative = negative.Replace(ch, "")
Next
TextBox1.Text = negative
'Result abdf
End Sub
You are making this way too complicated. If what you want is to remove a substring from within a string use replace like this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = Replace(txtLargeString.Text, txtSearchString.Text, "")
End If
End Sub
All you need is two radio buttons, three text boxes and a button. If you enter 1121221114141 in the txtLargeString text box, 2122 in the txtSearchString text box and execute the code, the result is 111114141 which is the result of removing the txtSearchString input from the txtLargeString input.
Or if as #Mary suggested you want to use the more modern version of replace use this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnRemove.Click
If rdbtnRemove.Checked Then
txtResultString.Text = txtLargeString.Text.Replace(txtSearchString.Text, "")
End If
End Sub
I have 2 buttons and a DataGridView with 2 Columns (0 & 1).
The 1st button transfers a randomized cell from the Column(1) to a TextBox. Then, it stores that Cell in variable (a), plus the cell that opposites it in variable (b).
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim rnd As New Random
Dim x As Integer = rnd.Next(0, Form1.DataGridView1.Rows.Count)
Dim y As Integer = 1
Dim a As String = Form1.DataGridView1.Rows(x).Cells(y).Value
Dim b As String = Form1.DataGridView1.Rows(x).Cells(y - 1).Value
TextBox3.Text = a
End Sub
The 2nd button, however, is supposed to compare if another TextBox's text has the same string variable (b) has as Strings. Now, if so, then it has to display a certain message and so on...
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
If TextBox4.Text = b Then '<<< ISSUE HERE!
MsgBox("Correct! ^_^")
ElseIf TextBox4.Text = "" Then
MsgBox("You have to enter something first! O_o")
Else
MsgBox("Wrong! >,<")
End If
End Sub
The problem is that the variable (b) is surely not shared across the two "private" subs. And so, there is NOTHING to compare to in the 2nd button's sub! I presume that the solution here is to split the "randomization process" into a separate function, then execute it directly when the 1st button gets activated. Furthermore, that function's variables have to be SHARED somehow, and I certainly don't know how!
Thanks for Mr. Olivier, the code has been improved significantly! Yet, I still encounter a "wrong" comparison issue, somehow!
Dim RND As New Random
Dim x As Integer
Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) As String
Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value
End Function
Private Sub btnRoll_Click(sender As Object, e As EventArgs) Handles btnRoll.Click
x = RND.Next(0, Form1.DataGridView1.Rows.Count)
tbxRoll.Text = GetCell(x, 1)
End Sub
Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
If tbxSubmit.Text = GetCell(x, 0) Then
MsgBox("Correct! ^_^")
ElseIf tbxSubmit.Text = "" Then
MsgBox("You have to enter something first! O_o")
Else
MsgBox("Wrong! >,<")
End If
End Sub</code>
Well, unbelievably, I read a guide about "comparison operations" in VB.net and tried out the first yet the most primal method to compare equality - which was to use .Equals() command - and worked like a charm! Thank God, everything works just fine now. ^_^
If tbxSubmit.Text.Equals(GetCell(x, 0)) Then
Alright now... This is going to sound weird! But, following Mr. Olivier's advise to investigate "debug" the code, I rapped the string I'm trying to compare with brackets and realized that it's been outputted after a break-line space! So, I used the following function to remove the "white-space" from both of the comparison strings! And it bloody worked! This time for sure, though. ^_^
Function RemoveWhitespace(fullString As String) As String
Return New String(fullString.Where(Function(x) Not Char.IsWhiteSpace(x)).ToArray())
End Function
If RemoveWhitespace(tbxSubmit.Text) = RemoveWhitespace(GetCell(x, 0)) Then
Turn the local variables into class fields.
Dim rnd As New Random
Dim x As Integer
Dim y As Integer
Dim a As String
Dim b As String
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
x = rnd.Next(0, Form1.DataGridView1.Rows.Count)
y = 1
a = Form1.DataGridView1.Rows(x).Cells(y).Value
b = Form1.DataGridView1.Rows(x).Cells(y - 1).Value
TextBox3.Text = a
End Sub
These fields can now be accessed from every Sub, Function and Property.
Of course Button3_Click must be called before Button2_Click because the fields are initialized in the first method. If this is not the case then you should consider another approach.
Create a function for the Cell access
Private Function GetCell(ByVal rowIndex As Integer, ByVal cellIndex As Integer) _
As String
Return Form1.DataGridView1.Rows(rowIndex).Cells(cellIndex).Value
End Function
And then compare
If TextBox4.Text = GetCell(x, y - 1) Then
...
And don't store the values in a and b anymore. If y is always 1 then use the numbers directly.
If TextBox4.Text = GetCell(x, 0) Then
...
One more thing: give speaking names to your buttons in the properties grid before creating the Click event handlers (like e.g. btnRandomize). Then you will get speaking names for those routines as well (e.g. btnRandomize_Click).
See:
- VB.NET Class Examples
- Visual Basic .NET/Classes: Fields
I don't understand the other code found on this website and would really appreciate the help.
By the way, I have no idea how to use this website and sincerely apologize If I'm formatting this question wrong. I should note that i need to add the number & vbtab & occurances. All this is to go in a lstbox.
Private Sub btnCount_Click(sender As System.Object, e As System.EventArgs) Handles btnCount.Click
Dim NumberCounts As Integer
Dim phrase As String
phrase = Me.txtNumbers.Text
Call CountNumbers(phrase, NumberCounts)
Could I modify this code for numbers?
For character as integer = 0 to phrase.length -1
uppercaseLetter = char.ToUpper(phrase.lengthchars(character))
if uppercaseLetter>= "A" and uppercaseLetter <= "Z" then
letterIndex = Ascw(uppercaseLetter)
lettercounts(letterindex) +=1
end if
next character
If what you are trying to do is to count the number of digits ("0" - "9") in a phrase, you can make CountNumbers a function that takes a String as its argument and returns an Integer. For example:
Function CountNumbers(phrase As String) As Integer
Return phrase.Count(Function(c) Char.IsDigit(c))
End Function
Or if you prefer:
Function CountNumbers(phrase As String) As Integer
Dim numberCount As Integer
For Each ch As Char In phrase
If Char.IsDigit(ch) Then numberCount += 1
Next
Return numberCount
Return phrase.Count(Function(c) Char.IsDigit(c))
End Function
Both versions use the Char.IsDigit method to test if each character in the String is a digit. You could use it to display the number of digits in the phrase entered in a TextBox like this:
Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Label1.Text = CountNumbers(TextBox1.Text).ToString
End Sub
So I have the code below that is connected to a pager, the problem I have is sometimes a 'page message' will be split across two actual "pages" but if its split it will always contain (part 1 of 2) or (part 2 of 2), the problem I have is that my sub receives the page data and then calls the sub Parse_Page which is fine for single paged massages.
Ive tried to test with an if statement say if PageData.contains ("(Part 1 of 2)") and that's all good and well but I need store that string somewhere and somehow wait for the part message to arrive before putting part 1 and part 2 together and calling the "Parse_page" sub. I've tried various if's and arrays but I;m getting confused in what has to happen. Any ideas on how to do it?
Public Sub serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serial.DataReceived
Dim PageData As String = serial.ReadLine
Parse_Page(Nothing, Nothing)
End Sub
Simply use a global var.
Dim PageData1 As String = String.Empty
Dim PageData2 As String = String.Empty
Public Sub serial_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles serial.DataReceived
If String.IsNullOrEmpty(PageData1) = True Then
PageData1 = serial.Readline
ElseIf String.IsNullOrEmpty(PageData2) = True Then
PageData2 = serial.Readline
PageData = PageData1 & PageData2
PageData1 = String.Empty
PageData2 = String.Empty
Parse_Page(Nothing, Nothing)
End If
End Sub