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
Related
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.
Here is my code :
I'm trying to have some value out from a TXT file to a combobox or label but i feel combobox would be easier.
here's my code :
please note, some config.txt will only have 1 value while other 5-6
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IDinFile As String
Dim ID As String
If IO.File.Exists("config.txt") Then
Using StreamReader As New IO.StreamReader("config.txt")
Do
IDinFile = StreamReader.ReadLine
If (IDinFile.IndexOf("7656")) <> -1 Then
ID = IDinFile.Substring(2)
ID = ID.Trim().Remove(ID.Length - 1)
ComboBox1.Items.Add(ID)
Exit Do
End If
Loop Until IDinFile Is Nothing
End Using
End If
End Sub
the file here in .png :
https://i.stack.imgur.com/iYaqP.png
Re-written the code for you. Problem was wrongly placed Exit Do. Also, its advisable to check the line before entering the loop rather than at the end of the loop.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim IDinFile As String
Dim ID As String
Const FILENAME As String = "config.txt"
If IO.File.Exists(FILENAME) Then
Using StreamReader As New IO.StreamReader(FILENAME)
Do While StreamReader.Peek() >= 0
IDinFile = StreamReader.ReadLine.Trim()
If (IDinFile.IndexOf("7656")) <> -1 Then
ID = IDinFile.Substring(1, IDinFile.Length - 2)
ComboBox1.Items.Add(ID)
End If
Loop
End Using
End If
End Sub
After you add the first item to the combobox you have an Exit Do statement. It no longer continues checking further lines and adding them to the combobox.
You should remove that statement.
Try this. It'll work if the values are organized line by line in the txt file.
Dim srd as New StreamReader("config.txt")
If io.file.exists("config.txt") then
Dim str() = srd.ReadToEnd.split(environment.newline)
For i = 0 to str.count-1
Combobox.item.add(str(i))
Next
srd.close
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.
I have 2 text files named sQue.txt containing single words in each lines (each word in each line) and sObj.txt also containing single word in each line (but no. of entries are more in this file than in sQue.txt).
Now, I have a blank form in which I want to read both the above files & display them in a manner such that:
Each entry from sQue.txt file gets displayed in separate labels in the form
All the entries of file sObj.txt are put in a CheckedListBox & this CheckedListBox appears for each label displayed in point 1. above.
Example:
sObj.txt contains 3 entries aaa, bbb & ccc (vertically i.e each in new line).
sQue.txt contains 5 entries p,q,r,s & t (vertically i.e each in new line).
Now, when the form loads, 3 labels are seen with texts aaa, bbb & ccc. Also 3 CheckedListBoxes are seen containg p,q,r,s & t in each box.
Can it be done? I'm trying to find a solution with no luck yet.
Please help.
Till now all I have is
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim queue As String() = IO.File.ReadAllLines("C:\temp\sQue.txt")
Dim objects As String() = IO.File.ReadAllLines("C:\temp\sObj.txt")
For i = 0 To queue.Count - 1
'create labels here
For j=0 to objects.Count - 1
'create CheckedListBoxes
Next
Next
End Sub
If you use a groupbox you can use the text property as your label, and add a checkedlistbox to the groupbox with the items you want. This code will do that:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim NewForm2 As New Form2
NewForm2.Show()
Dim sObj() As String = File.ReadAllLines("sobj.txt")
Dim sQue() As String = File.ReadAllLines("sQue.txt")
For Each s As String In sObj
Me.Controls.Add(MakeNewGB(s, sQue))
Next
End Sub
End Class
Public Module Module1
Friend WithEvents NewGB As System.Windows.Forms.GroupBox
Friend WithEvents NewCLB As System.Windows.Forms.CheckedListBox
Public NextColumn As Integer = 0
Public Function MakeNewGB(lbl As String, clbItems() As String) As GroupBox
NewGB = New System.Windows.Forms.GroupBox()
NewCLB = New System.Windows.Forms.CheckedListBox()
NewGB.SuspendLayout()
'GroupBox1
'
NewGB.Controls.Add(NewCLB)
NewGB.Location = New System.Drawing.Point(NextColumn, 0)
NewGB.Name = lbl
NewGB.Size = New System.Drawing.Size(126, 210)
NewGB.TabIndex = 0
NewGB.TabStop = False
NewGB.Text = lbl
'
'CheckedListBox1
'
NewCLB.FormattingEnabled = True
NewCLB.Location = New System.Drawing.Point(6, 19)
NewCLB.Name = "clb" + lbl
NewCLB.Size = New System.Drawing.Size(103, 184)
NewCLB.TabIndex = 0
NewCLB.Items.AddRange(clbItems)
NextColumn += NewGB.Size.Width + 10
Return NewGB
End Function
End Module
I think your code should look like this. But i am not sure what the purpose of it is.
Private Sub Form7_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim queue As String() = IO.File.ReadAllLines("C:\temp\sQue.txt")
Dim objects As String() = IO.File.ReadAllLines("C:\temp\sObj.txt")
For i = 0 To queue.Count - 1
'create labels here
Dim label as new Label
label.Text = queue(i)
Dim chklst as new CheckedListBox
For j=0 to objects.Count - 1
'create CheckedListBoxes
chklst.Items.Add(object(j))
Next
Me.Controls.Add(label)
Me.Controls.Add(chklst)
Next
End Sub
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.