In VB I want to use Unicode characters - vb.net

I am having a problem, when I try to enter Japanese characters into the VB console they show up as question marks with no data attached to them. I am writing a program to help me conjugate words and I need to be able to input Japanese characters to do so.
Is there a setting I can change to allow the input and output of these characters?

I have found the the solution!
Add the following code to the top of your code:
Console.InputEncoding = System.Text.Encoding.Unicode
Console.OutputEncoding = System.Text.Encoding.Unicode
This will allow the input of foreign characters but it won't allow you to see the actual characters, (the characters will just have value)
Run the code
At the top left of the console, there is a an icon, click it, and then click properties
Go to the font tab and change the font. So far, the ones that work for me are any that begin with "MS", but "SimSun-ExtB" works too.
Click "Ok" and you're done!

This document has a reference for several unicode ranges.
You can try my method as follows. Please pay attention to RichTextBox1.Text &= (ChrW(i)) which is the most important step.
Public Class Form1
Dim First_Unicode, Last_unicode As Integer
'Hexadecimal to Decimal
Public Function H2D(ByVal Hex As String) As Long
Dim i As Long
Dim b As Long
Hex = UCase(Hex)
For i = 1 To Len(Hex)
Select Case Mid(Hex, Len(Hex) - i + 1, 1)
Case "0" : b = b + 16 ^ (i - 1) * 0
Case "1" : b = b + 16 ^ (i - 1) * 1
Case "2" : b = b + 16 ^ (i - 1) * 2
Case "3" : b = b + 16 ^ (i - 1) * 3
Case "4" : b = b + 16 ^ (i - 1) * 4
Case "5" : b = b + 16 ^ (i - 1) * 5
Case "6" : b = b + 16 ^ (i - 1) * 6
Case "7" : b = b + 16 ^ (i - 1) * 7
Case "8" : b = b + 16 ^ (i - 1) * 8
Case "9" : b = b + 16 ^ (i - 1) * 9
Case "A" : b = b + 16 ^ (i - 1) * 10
Case "B" : b = b + 16 ^ (i - 1) * 11
Case "C" : b = b + 16 ^ (i - 1) * 12
Case "D" : b = b + 16 ^ (i - 1) * 13
Case "E" : b = b + 16 ^ (i - 1) * 14
Case "F" : b = b + 16 ^ (i - 1) * 15
End Select
Next i
H2D = b
End Function
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Font = New Font("Cambria", 20, FontStyle.Regular)
First_Unicode = H2D(TextBox1.Text)
Last_unicode = H2D(TextBox2.Text)
'display 20 characters each line
Dim i As Integer
For i = First_Unicode To Last_unicode
RichTextBox1.Text &= (ChrW(i))
If i Mod 20 = 0 Then
RichTextBox1.Text &= vbCrLf
End If
Next
End Sub
End Class

Related

EXPLAIN what this VB CODE means

Function convertToText(ByVal data As String) As String
Dim result As String = Nothing
Dim i As Integer = 0
Dim j As Integer = 0
For Each c As Char In data.ToCharArray
j *= 2
If c = "1"c Then j += 1
i += 1
If i = 8 Then
i = 0
result &= Chr(j)
j = 0
End If
Next
Return result
End Function
It converts binary to text but its a bit difficult for me to understand the logic behind it.
Someone please help.
The code seems to convert a text containing a binary number representing 8 bit character codes to a string containing these characters.
The for each loop loops over all binary digits ("0" or "1") of the input. The code of each result character is computed and after every 8 input characters the code is considered to be complete and the new character whose code was determined is added to the result (result &= Chr(j) is the same as result = result & Chr(j). Chr(j) converts an Integer containing a character code into a character). The variable i counts the bits.
The variable j holds the character code. If a bit is "1", then 1 is added to j (j += 1 is the same as j = j + 1), but not if it is "0".
A "1" in the right most bit position has a (decimal) value of 1. The next to its left a value of 2. The next 4 and so on. The value doubles for each position until it reaches 128 for the left most bit of an 8 bit number. Therefore j is doubled on each loop (j *= 2 is the same as j = j * 2).
Example with just 4 bits:
data = "1010"
The binary number 1010 means
1 * 8 + 0 * 4 + 1 * 2 + 0 * 1 = (decimal)10
The code does this
j = 0 => 0
j *= 2 => 0
j += 1 => 1 'since c = "1"
j *= 2 => 2
'no += 1 since c = "0"
j *= 2 => 4
j += 1 => 5 'since c = "1"
j *= 2 => 10
'no += 1 since c = "0"
The first 1 we added is doubled 3 times and becomes 8. The second 1 we added is doubled only once and becomes 2. 8 + 2 = 10.

Items doesn't display in list box when I run the program

I was doing a program about Bisection Method. I haven't encountered any errors while doing the program, but when I run it, input the data, and press the compute button, no answer is displayed, but a scroll bar appears and freezes the program.
I don't know what is wrong but I guess it has something to do with logical error.
Public Class Form1
Dim a As Double
Dim b As Double
Dim c As Double
Dim fa As Double
Dim fb As Double
Dim fc As Double
Dim err As Double
Dim n As Integer = 1
Dim x As Double
Dim diserr As String
Dim fas As String
Dim fbs As String
Dim fcs As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
a = TextBox1.Text
b = TextBox2.Text
err = TextBox3.Text
fa = a ^ 3 - a - 3
fb = b ^ 3 - b - 3
c = (a + b) / 2
fc = c ^ 3 - c - 3
x = Math.Abs(a - b)
Do While x > err
fa = a ^ 3 - a - 3
fb = b ^ 3 - b - 3
c = (a + b) / 2
fc = c ^ 3 - c - 3
x = Math.Abs(a - b)
If err < x Then
diserr = "No"
Else
diserr = "Yes"
End If
If fa > 0 Then
fas = "+"
Else
fas = "-"
End If
If fb > 0 Then
fbs = "+"
Else
fbs = "-"
End If
If fc > 0 Then
fcs = "+"
Else
fcs = "-"
End If
If diserr = "Yes" Then
fcs = Str(x)
End If
ListBox1.Items.Add(Str(n))
ListBox2.Items.Add(Str(a))
ListBox3.Items.Add(Str(b))
ListBox4.Items.Add(Str(c))
ListBox5.Items.Add(Str(x))
ListBox6.Items.Add(diserr)
ListBox7.Items.Add(fas)
ListBox8.Items.Add(fbs)
ListBox9.Items.Add(fcs)
n = n + 1
If fas = "-" And fcs = "+" Then
b = c
End If
If fbs = "-" And fcs = "+" Then
a = b
b = c
End If
Loop
End Sub
End Class
You need to consider the cases where fcs is "-".
If I'm not misunderstanding the method you can just replace the last two ifs with
If fas <> fcs Then
b = c
Else
a = b
b = c
End If

vb basic text is not a member of double

I have a problem of conversion. When I write txta1 = txta1.Text, it shows "text is not a member of double." I'm not sure why, can someone explain this to me?
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Dim treatments As String
Dim ssb, ssw, msb, msw, sigmaA, sigmaB, sigmaC, sigmaabc, squaresigmaA, squaresigmaB, squaresigmaC, squaresigmaabc, f As Double
Dim txta1, txta2, txta3, txta4, txta5, txtb1, txtb2, txtb3, txtb4, txtb5, txtc1, txtc2, txtc3, txtc4, txtc5 As Double
'Data Input
txta1 = txta1.Text
treatments = lblTreatments.Text
'Calaculation
sigmaA = txta1 + txta2 + txta3 + txta4 + txta5
sigmaB = txtb1 + txtb2 + txtb3 + txtb4 + txtb5
sigmaC = txtc1 + txtc2 + txtc3 + txtc4 + txtc5
sigmaabc = sigmaA + sigmaB + sigmaC
squaresigmaA = txta1 ^ 2 + txtb2 ^ 2 + txtb3 ^ 2 + txtb4 ^ 2 + txtb5 ^ 2
squaresigmaB = txtb1 ^ 2 + txtb2 ^ 2 + txtb3 ^ 2 + txtb4 ^ 2 + txtb5 ^ 2
squaresigmaC = txtc1 ^ 2 + txtc2 ^ 2 + txtc3 ^ 2 + txtc4 ^ 2 + txtc5 ^ 2
squaresigmaabc = squaresigmaA + squaresigmaB + squaresigmaC
ssb = ((sigmaA) ^ 2 / 5 + (sigmab) ^ 2 / 5 + (sigmac) ^ 2 / 5) - (sigmaabc) ^ 2 / 15
ssw = squaresigmaabc - ((sigmaA) ^ 2 / 5 + (sigmab) ^ 2 / 5 + (sigmac) ^ 2 / 5)
msb = ssb / (3 - 1)
msw = ssw / (15 - 1)
f = msb / msw
'Data output
lblSSB.Text = ssw
lblSSW.Text = ssw
lblMSB.Text = msb
lblMSW.Text = msw
lblF.Text = f
End Sub
As the compiler is trying to tell you, you declared txta1 as a Double, which has no Text property.
You should choose different names for your controls and your local variables.
You've declared txta1 as a Double in line 4. I assume you also have a TextBox called txta1 on your form. Use a different name like txta1Value for the Double, so it doesn't clash with the name of the TextBox. Then you can say
txta1Value = Double.Parse(txta1.Text)
(This will blow up if someone types something other than a valid Double; how you want to deal with that is up to you.)

project euler problem17 vb code bug

If the numbers 1 to 5 are written out in words: one, two, three, four, five, then there are 3 + 3 + 5 + 4 + 4 = 19 letters used in total.
If all the numbers from 1 to 1000 (one thousand) inclusive were written out in words, how many letters would be used?
NOTE: Do not count spaces or hyphens. For example, 342 (three hundred and forty-two) contains 23 letters and 115 (one hundred and fifteen) contains 20 letters. The use of "and" when writing out numbers is in compliance with British usage.
my answer is coming 21148. i cant find the mistake here is my code in vb
the answer should be 21124
Dim length As UInt32 = 11
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For i = 1 To 999
If i / 10 < 1 Then
single_digit(i)
ElseIf i / 10 = 1 Then
ten()
ElseIf i / 10 > 1 And i / 10 < 2 Then
ele_twe_thi(i)
ElseIf i / 10 >= 2 And i / 10 < 10 Then
multiple_10(Math.Floor(i / 10) * 10)
single_digit(i Mod 10)
ElseIf i = 100 Then
single_digit(i / 100)
length += 7
ElseIf i Mod 100 >= 11 And i Mod 100 <= 19 Then
length += 10
single_digit(Math.Floor(i / 100))
ele_twe_thi(i Mod 100)
ElseIf i Mod 100 = 10 Then
length += 3
single_digit(Math.Floor(i / 100))
length += 10
Else
length += 10
single_digit(Math.Floor(i / 100))
multiple_10(Math.Floor((i Mod 100) / 10) * 10)
single_digit(i Mod 10)
End If
Next
MsgBox(length)
End Sub
Private Sub single_digit(num)
If num = 1 Then
length = length + 3
ElseIf num = 2 Then
length = length + 3
ElseIf num = 3 Then
length = length + 5
ElseIf num = 4 Then
length = length + 4
ElseIf num = 5 Then
length = length + 4
ElseIf num = 6 Then
length = length + 3
ElseIf num = 7 Then
length = length + 5
ElseIf num = 8 Then
length = length + 5
ElseIf num = 9 Then
length = length + 4
End If
End Sub
Private Sub ele_twe_thi(num)
If num = 11 Or num = 12 Then
length = length + 6
ElseIf num = 13 Or num = 14 Or num = 18 Or num = 19 Then
length = length + 8
ElseIf num = 17 Then
length = length + 9
ElseIf num = 15 Or num = 16 Then
length = length + 7
End If
End Sub
Private Sub multiple_10(num)
If num = 20 Then
length = length + 6
ElseIf num = 30 Then
length = length + 6
ElseIf num = 40 Then
length = length + 5
ElseIf num = 50 Then
length = length + 5
ElseIf num = 60 Then
length = length + 5
ElseIf num = 70 Then
length = length + 7
ElseIf num = 80 Then
length = length + 6
ElseIf num = 90 Then
length = length + 6
End If
End Sub
Private Sub ten()
length = length + 3
End Sub
every time the program reaches 100, 200, 300 etc. is it adding an "and" unnecessarily?

do not understand if else block behaviour

I have the following code:
Sub Main()
Dim a As Integer = 8 * 60
Dim b As Integer
Dim c As Integer
If a < (6 * 60) Then
b = 0 And c = 0
ElseIf a >= 6 * 60 And a < 9 * 60 Then
b = 30 And c = 1
Else
b = 45 And
c = 1
End If
MsgBox(b)
End Sub
Thinks i dont understand and where i need someones help:
"c=0" and "c=1" are underlined with the error: Strict on doesnt allow implicit convertation from boolean to integer. WHY? I declared c as integer!
Variable "b" and "c" are always "0" even though in the case above they should be b=30 and c = 1.
can anyone please explain me this behaviour.
You are using the And keyword where it is not allowed. And is a logical operator (along with Or, AndAlso, OrElse.)
The following should work.
Sub Main()
Dim a As Integer = 8 * 60
Dim b As Integer
Dim c As Integer
If a < (6 * 60) Then
b = 0
c = 0
ElseIf a >= 6 * 60 And a < 9 * 60 Then
b = 30
c = 1
Else
b = 45
c = 1
End If
MsgBox(b)
End Sub