Storing Characters above 128 in string array - vb.net

The VB.net code below works fine in earlier programs to use characters above 128
But it throws an error in current developments
It looks like the code page that holds the standard windows characters above 128 has disappeared behind the scenes. The code still works fine in older programs thankfully.
Any ideas ?
Public QAZ, conv As String
Public SREP(200) As String
Public I, J As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
For I = 128 To 190
SREP(J) = Chr(I)
J = J + 1
Next
End Sub
Tried adding the following line on advice. This stopped the error messages but the holding string array was blank.
Public SREP(200) As String
Public I, J As Integer
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
J = 0
For I = 128 To 190
SREP(J) = Chr(I)
J = J + 1
Next
End Sub

I'm not sure what your issue is. I tried this and it worked as expected.
Dim SREP As New List(Of String)
For I As Integer = 128 To 190
Dim foo As Char = ChrW(I)
SREP.Add(foo)
Debug.WriteLine(foo)
Next
I also tested this with the same results.
Dim zSREP As New List(Of String)
For I As Integer = 128 To 190
Dim foo As Char = Convert.ToChar(I)
zSREP.Add(foo)
Debug.WriteLine(foo)
Next
Results check.
Dim union As List(Of String) = SREP.Union(zSREP).ToList

The simple answer to ensure that the chr() function works consistently and without causing errors for 128 to 256 character codes, using the original default windows character set, so that consistent characters are generated, seems to be to put this line early on in your program.
System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance)
I hope this saves others time in trying to wade through loads of complex and often witch doctor solutions to the problem.

Related

Converting a whole word to Ascii

Public Class Form1
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim s As String
Dim b As String
Dim length As Integer
length = Len(TextBox1.Text)
For x = 1 To length
s = TextBox1.Text
b = s.Remove(0, 1)
a = Asc(b)
TextBox2.Text = a
Next
End Sub
End Class
This is my code. I tried to do a loop so the whole word is translated to ASCII but it still did not work, I am trying to get it so a user enters a word into a text box (textbox1) then if they press button 2, the whole of textbox1 will be converted to ASCII, and displayed in textbox2.
I have looked online but I can not find anything,
the current issue I have is that when I press 'convert' only the first letter of the word is converted which is not what I want. This is done in vb 2008, forms. But I have also tried in console with similar code.
All help would be Great.
Try using a loop:
Imports System
Imports Microsoft.VisualBasic
Imports System.Text
Dim input As String = TextBox1.Text
Dim output as new StringBuilder
for each item as string in input.ToCharArray()
output.Append(Asc(item).ToString() + " ")
next
Console.WriteLine(output)
In this case:
Input : Sunil
Output : 83 117 110 105 108
I added that space for clarity, you can change it to anything or remove it.

Alternative Process

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

Visual Basic Windows Form - Help Loading picture corresponding to numerical value

Hello i am trying to make a texas hold'em style game and im at the point where i filled an array with numbers 1-52 randomly assorted. I first pull the value from the first index of the array and have the corresponding card picture be set to a picturebox value. I have saved the 52 card .png files in my resources as well. These pictures are also saved with their names as 1.png, 2.png, 3.png.... etc depending the suit and value.
I am sorting the values as 1-13 spades (2-ace), 14-26 hearts, 27-39 diamonds, 40-52 clubs.
I also just saw i should probably use a global counter to keep track of the deck position.
Public Class Form1
Dim Deal As MsgBoxResult
Dim CardDeck As New Random
Dim Counter As Integer = 1
Dim CardCount(52) As Integer
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnStart.Click
Deal = MessageBox.Show("Would you like to start a Game?", "Texas Holde'em", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
ShuffleDeck()
CalculateFirst3Cards()
End Sub
Private Sub ShuffleDeck()
If Deal = MsgBoxResult.Yes Then
For num As Integer = 1 To CardCount.Length - 1
Dim DeckValue As Integer = CardDeck.Next(1, 52)
CardCount(Counter) = DeckValue
Counter += 1
Next
End If
End Sub
Private Sub CalculateFirst3Cards()
Dim counter As Integer = 1
For num As Integer = 1 To 3
Dim hold As Integer = CardCount(counter)
Dim hold1 As String = Convert.ToString(hold)
River1.Image = My.Resources.
counter += 1
Next
End Sub
End Class

Too many arguments to `LTrim`

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)

Why VB.NET simple app takes forever to load several lines

I have a super simple script that I am using to separate a long list of phone numbers we've gathered from donors over the years into separate area code files.
Obviously, when you have almost 1 million lines it's going to take a while - BUT - if I put in 1,000 it takes less than a second. Once I put in 1 million it takes 10 seconds to do only 5 lines. How could this be?
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = False
Dim lines As String
lines = RichTextBox1.Lines.Count
Dim looper As String = 0
Dim path As String = "c:\acs\"
MsgBox("I have " + lines + " lines to do.")
If (Not System.IO.Directory.Exists(path)) Then
System.IO.Directory.CreateDirectory(path)
End If
Dim i As String = 0
For loops As Integer = 0 To RichTextBox1.Lines.Count - 1
Dim ac As String = RichTextBox1.Lines(looper).ToString
ac = ac.Substring(0, 3)
Dim strFile As String = path + ac + ".txt"
Dim sw As StreamWriter
sw = File.AppendText(strFile)
sw.WriteLine(RichTextBox1.Lines(looper))
sw.Close()
Label1.Text = String.Format("Processing item {0} of {1}", looper, lines)
looper = looper + 1
Next
MsgBox("done now")
End Sub
End Class
Each time you use the RichTextBox.Lines properties, VB.Net will need to split the content by CR+LF pair. Thus your For loops As Integer = - To RichTextBox1.Lines.Count-1 is really a performance hit.
Try to use:
For Each vsLine As String in RichTextBox1.Lines
instead. It will be a lot faster. Alternatively, if you must use For loop, then get it once:
Dim vasLines() As String = RichTextBox1.Lines
For viLines As Integer = 0 to UBound(vasLines.Count)
'....
Next
instead.
First, you're performing UI updates in your For loop. That will take time.
You're updating the UI in a thread that is not the main thread which might impact performance. You should not use the CheckForIllegalCrossThreadCalls method. You should update the UI properly using the ReportProgress method of the BackgroundWorker.
You are opening and closing a file for each iteration of the loop. That will take time as well.
I think a better method would be to add the data to a Dictionary(Of String, List(Of String)), with the area code as the key and the list would hold all the number for that area code. Once the Dictionary is filled, loop through the keys and write the numbers out.