Converting a whole word to Ascii - vb.net

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.

Related

creating text file in VB.NET adding extra line

I am trying to create text file with the code below; but there is always an extra line at the end that I cannot get rid of. Is there any way to modify this code to write the text file without the last line?? Help appreciated:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim MekdamFile As String
MekdamFile = "C:\TEMP\MEKDAM.txt"
Dim dones As New List(Of String)
For i = 1 To 10
dones.Add("test test " & i)
Next
Using sw As New StreamWriter(MekdamFile)
For Each i As String In dones
sw.WriteLine(i)
Next
End Using
End Sub
End Class
Thanks for help in advance...
StreamWriter.WriteLine always writes a line terminator.
On the last iteration of the loop you could use sw.Write instead.
Your code can be simplified a bit:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
File.WriteAllText("C:\TEMP\MEKDAM.txt", [String].Join( _
Environment.NewLine, _
Enumerable.Range(1, 10).[Select](Function(i) "Test test" + Cstr(i))))
End Sub
You are calling WriteLINE, so whatever you pass into it will be appended with a newline character sequence.
If you don't want to write the newline, use Write instead of WriteLine. Of course, you should then append the newline yourself for all lines except the last.
Alternatively, for a small text, just build the whole text, then trim the end of it and write it at once:
Dim sb As new StringBuilder();
For i = 1 To 10
sb.AppendLine("line " + i);
Next
File.WriteAllText(path, sb.ToString().TrimEnd());

Validate only newly typed text

I'm making simple application. There is a textbox and a ListBox. When user type something in the textbox, that text add to the ListBox split by space after some validation process. I done it. Here is my code.
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'split by space
Dim arrText() As String = Split(TextBox1.Text, " ")
ListBox1.Items.Clear()
'ValidateText is a function
For i = 0 To UBound(arrText)
ListBox1.Items.Add(ValidateText(arrText(i)))
Next i
End Sub
But I want to upgrade it because the validation process take more time. When user type something in the textbox need to do the same process but for only newly typed text. (From the cursor position forward to the end of the text) already validated text doesn’t need to validate again. I think someone can help.
Note: user can be also copy & paste words in the textbox
Thank in advance
I have found a solution thanks to lapheal who member in msdn forum
Private validatedDic As New Dictionary(Of String, String) 'or Dictionary(Of String, Object)?
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
'split by space
Dim arrText() As String = Split(TextBox3.Text, " ")
ListBox1.Items.Clear()
'ValidateText is a function
For i = 0 To UBound(arrText)
Dim text As String = String.Empty
If Not validatedDic.TryGetValue(arrText(i), text) Then
text = ValidateText(arrText(i))
validatedDic(arrText(i)) = text
End If
ListBox1.Items.Add(text)
Next i
End Sub

Remove Whitespace from Line Item in ListBox (VB)

I'm trying to remove all spaces in on all Line Items in a ListBox. I would have assumed I could just throw in a normalize whitespace function but that doesn't appear to be the case. Long story short, I'm trying to write a program that counts the number of XML Fields from a file. I'd like to display all the fields but kill all the spacing in front of it (since it's formatted as XML).
'Imports System.IO
Public Class Form1
Dim theFile As StreamReader
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadTheFile.Click
'Load the File
theFile = New StreamReader("C:\Users\Marc Wilson\Documents\XML\sampledata.xml")
While (theFile.Peek > -1)
ListBox1.Items.Add(theFile.ReadLine)
End While
theFile.Close()
'Only get the fields
Dim numberOfLines As Integer = ListBox1.Items.Count
For i = 0 To numberOfLines - 1
Dim theLineItem As String = ListBox1.Items.Item(i).ToString
If theLineItem.Contains("<my:") And theLineItem.Contains("/>") Then
ListBox2.Items.Add(theLineItem)
End If
Next
'Count items
lblCount.Text = ListBox2.Items.Count.ToString
End Sub
End Class
this will remove leading and trailing spaces
theFile.ReadLine.trim

Text Search And Replacement

I am working with visual basic and i'm creating a function which performs a search for each word that is entered into a richtextbox. As a word is being entered into RichTextBox2 a search Is performed in RichTextBox1 and the text is highlighted .
RichTextBox1 and RiochTextBox2 are read only , RichTextBox2 is able to be written in via code while RichTextBox1 has only default text (A directory of words). There is also RichTextBox3 which holds a copy of RichTextBox2's text
RichTextBox3.Text = RichTextBox2.Text
this is the code for the function .
public class textsearch
Private intPosition As Integer
Private Sub NextButton_Click(sender As Object, e As EventArgs) Handles NextButton.Click
generatekanji()
' Static intStart As Integer
'used to select compare method
Dim intStart As Integer
Dim objType As Object
Dim lastWord As String = RichTextBox2.Text.Split(" ").Last
objType = CompareMethod.Text
'set starting position to 1
intPosition = 1
'use the InStr function to look up a staring position of a search string in a given text box using objType (case-insensitive or case-sensitive)
intStart = InStr(intPosition, RichTextBox1.Text, lastWord, objType) ' what it searches
If intStart > 0 Then
'set starting select position on a textbox and select the search string
RichTextBox1.SelectionStart = intStart - 1
RichTextBox1.SelectionLength = lastWord.Length 'highlights the searched word
RichTextBox1.Select()
End If
End Sub
End Class
This is a very useful function but the main issues are
(1) because RichTextBox1 is read only , you gear the "ding" sound each time a search is being performed and it gets very annoying.
(2) I am unable to find a way to select the character whenever a word is found , or how to replace the word in RichTextBox3 with the character next to the searched word.
Could someone help with this problem .
When I use this code the search word is highlighted with no dinging, in a readonly richtextbox:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim SearchWord As String = RichTextBox2.text
Dim SelStart As Integer = InStr(RichTextBox1.Text, SearchWord, CompareMethod.Text)
If SelStart > 0 Then
RichTextBox1.Select(SelStart - 1, SearchWord.Length)
RichTextBox1.Focus()
For Each line In RichTextBox1.Lines
If line.Contains(SearchWord) Then
RichTextBox3.Text = line.Split()(0)
End If
Next
End If
End Sub
I'm wondering if the dinging is coming from the generatekanji() routine.

Extracting specific lines from one text file to other text file

I want to extract some specific lines from a text file to other text file. i am using the following code
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim Tr As IO.TextReader = System.IO.File.OpenText("C:\Assignment.txt")
For c As Integer = 1 To 10
If c = 7 Then
Dim MyFileLine As String = Split(Tr.ReadToEnd(), vbCrLf)(c) & vbCrLf
Tr.Close()
Dim TW As System.IO.TextWriter
'Create a Text file and load it into the TextWriter
TW = System.IO.File.CreateText("C:\Assignment1.txt")
TW.WriteLine(MyFileLine)
'Flush the text to the file
TW.Flush()
'Close the File
TW.Close()
End If
Next c
End Sub
End Class
But this code extract only the line no 7 where i want to extract the 8th,9th,10th,14th,15th,16th, lines also . Please guide me the right solution. Thank u in advance.
There seems to be several issues here. I will correct them and then explain below:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim currentLine As String
Dim lineCounter As Integer = 1
Dim lineNumbersRequired As List(Of Integer) = New List(Of Integer)
lineNumbersRequired.Add(7)
lineNumbersRequired.Add(8)
lineNumbersRequired.Add(9)
lineNumbersRequired.Add(10)
lineNumbersRequired.Add(14)
lineNumbersRequired.Add(15)
lineNumbersRequired.Add(16)
Dim TW As System.IO.TextWriter
'Create a Text file and load it into the TextWriter
TW = System.IO.File.CreateText("C:\Assignment1.txt")
Using Tr As IO.TextReader = New IO.StreamReader("C:\Assignment.txt")
While Not Tr.EndOfStream
If lineNumbersRequired.Contains(lineCounter) Then
Dim MyFileLine As String = Split(currentLine, vbCrLf)(c) & vbCrLf
TW.WriteLine(MyFileLine)
End If
lineCounter = lineCounter + 1
End While
End Using
TW.Flush()
'Close the File
TW.Close()
End Sub
End Class
NOTE: Code not tested, but should be pretty close if you do get a few compile errors!
Ok then, just a quick rundown of what I did here:
Changed the For Loop into a while because you had the for loop running from 1 To 10, so even if it worked, then you would have never read past the 10th line in your file. So I have changed it to a while loop that will end when the TextReader has read all lines in the file. Also the current line read from the file has been added to a new variable called currentLine.
The new currentLine variable is now used to populate the lines of your writing file.
I have added a list of Integers that will hold the line numbers you want to keep, then within the while loop I have a counter that counts each line as it is processed and if this counter is inside the list of line numbers you want to save into your output file, then it will output the current line.
Let me know how you get on, and if you need more of an explanation on any of it then please ask.