I am using visual basic 2010.I have string of data in below format.I wanted to Split the comma seprated value and put into Individual text box.For The last
Temp_read:348,HV_Read:647,SPD:0,DIS:0". I would like to split values alone and put into text box.
can someone suggest me how can i do it. Is there any example code.
Public Class Form1
Dim selectedItem1 As String
Dim Data As String
Private Sub SMCB1_clientIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_clientIP.TextChanged
End Sub
Private Sub SMCB1_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Connect.Click
Dispay_Show.Text = SMCB1_clientIP.Text
Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine & Data
Data.Split()
End Sub
Private Sub SMCB1_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Disconnect.Click
End Sub
End Class
Image
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
I have written code as below.There are 28 text boxes . value should get recorded into each text box. With below code i could record upto 28 value.
But From 24 the paramter contains
Temp_read:348,HV_Read:647,SPD:0,DIS:0"
Need to separate string ":" and put reading to particular text box.
Option Explicit On
Public Class Form1
Dim selectedItem1 As String
Dim Data As String
Dim WrdArray() As String
Dim line As String
Private Sub SMCB1_clientIP_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_clientIP.TextChanged
End Sub
Private Sub SMCB1_Connect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Connect.Click
' Dispay_Show.Text = SMCB1_clientIP.Text
'Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
' Dispay_Show.Text = SMCB1_clientIP.Text & vbNewLine & SMCB1_Port.Text & vbNewLine & Data
Dispay_Show.Text = Data
Dim strArray() As String
Dim intCount As Integer
Dim Tempr_read As String
Dim voltage As String
Dim SPD As String
Dim Dis_value As String
Data = "SMCB3,3,0,0,0,0,0,0,0,0,0,3,0,0,0,0,0,0,0,0,0,0,0,0,0,0,Temp_read:348,HV_Read:647,SPD:0,DIS:0"
strArray = Split(Data, ",")
SMCB1_Name.Text = strArray(0)
SMCB1_DeviceId.Text = strArray(1)
SMCB1_String1.Text = strArray(2)
SMCB1_String2.Text = strArray(3)
SMCB1_String3.Text = strArray(4)
SMCB1_String4.Text = strArray(5)
SMCB1_String5.Text = strArray(6)
SMCB1_String6.Text = strArray(7)
SMCB1_String7.Text = strArray(8)
SMCB1_String8.Text = strArray(9)
SMCB1_String9.Text = strArray(10)
SMCB1_String10.Text = strArray(11)
SMCB1_String11.Text = strArray(12)
SMCB1_String12.Text = strArray(13)
SMCB1_String13.Text = strArray(14)
SMCB1_String14.Text = strArray(15)
SMCB1_String15.Text = strArray(16)
SMCB1_String16.Text = strArray(17)
SMCB1_String17.Text = strArray(18)
SMCB1_String18.Text = strArray(19)
SMCB1_String19.Text = strArray(20)
SMCB1_String20.Text = strArray(21)
SMCB1_String21.Text = strArray(22)
SMCB1_String22.Text = strArray(23)
SMCB1_String23.Text = strArray(24)
SMCB1_String24.Text = strArray(25)
' Tempr_read = Split(Data(strArray(26),":")
SMCB1_Temp.Text = strArray(26)
SMCB1_Hvread.Text = strArray(27)
SMCB1_SPD.Text = strArray(28)
SMCB1_DIS.Text = strArray(29)
For intCount = LBound(strArray) To UBound(strArray)
Debug.Print(Trim(strArray(intCount)))
Next
End Sub
Private Sub SMCB1_Disconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SMCB1_Disconnect.Click
End Sub
End Class
I suggest using 2 instances of the InStrRev function - one for the colon and the other for the comma whilst looping backwards through the string and building your array.
In this way you will be able to capture the data to the right of the colon (and before the comma), yet disregard the label between the comma and the colon (if that is in fact what you intend to do).
Please hit me up if you would like a worked example.
Regards
How to get online date in vb.net ? is this possible ?
i use this code but it gives error like this (The remote server
returned an error: (503) Server Unavailable.) can anyone help ? thank you in advance
Imports System.ComponentModel
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Function getPageSource(ByVal URL As String) As String
Dim getWebClient As System.Net.WebClient = New System.Net.WebClient()
Dim strSource As String = getWebClient.DownloadString(URL)
getWebClient.Dispose()
Return strSource
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myPageSource, myStr, myStr1, strDate As String
Dim curLen, myStrPos, setStartPos As Integer
Dim dt As DateTime
txtPageSource.Text = getPageSource("http://www.worldtimeserver.com/current_time_in_PH.aspx")
myPageSource = txtPageSource.Text
'code to trim all the text till TIME start position
myStr = ""
myStrPos = myPageSource.IndexOf(myStr)
txtPageSource.Text = txtPageSource.Text.Remove(1, myStrPos + 23)
myPageSource = txtPageSource.Text
'code to trim all the text till after DATE
curLen = Len(txtPageSource.Text)
myStr1 = "2010"
setStartPos = myPageSource.IndexOf(myStr1)
txtPageSource.Text = txtPageSource.Text.Remove(setStartPos + 4, curLen - setStartPos - 4)
'code to trim "" and "" tags in between TIME and DATE
' 6:27 AM
'
'
'Wednesday, June 23, 2010
txtPageSource.Text = txtPageSource.Text.Replace("", "")
txtPageSource.Text = txtPageSource.Text.Replace("", "")
strDate = txtPageSource.Text
dt = CType((TypeDescriptor.GetConverter(New DateTime(2000, 1, 1)).ConvertFrom(strDate)), DateTime)
'txtPageSource.Text = dt
MsgBox(dt)
End Sub
End Class
I got asked a couple of days ago how to save number of clicks you have done to each button in a program to a small file, to keep track of what is most used. Since it was ages ago i dabbled with visual basic i said i would raise the question here, so here goes.
There are 5 buttons labels Button1-Button5. When a button is clicked it should look for the correct value in a file.txt and add +1 to the value. The file is structured like this.
Button1 = 0
Button2 = 0
Button3 = 0
Button4 = 0
Button5 = 0
So when button1 is clicked it should open file.txt, look for the line containing Button1 and add +1 to the value and close the file.
I have tried looking for some kind of tutorial on this but have not found it so i'm asking the collective of brains here on Stackoverflow for some guidance.
Thanks in advance.
delete file first
new ver ..
Imports System.IO.File
Imports System.IO.Path
Imports System.IO
Imports System.Text
Public Class Form1
Dim line() As String
Dim strbild As StringBuilder
Dim arr() As String
Dim i As Integer
Dim pathAndFile As String
Dim addLine As System.IO.StreamWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fileName As String = "myfile.txt"
Dim appPath As String = My.Application.Info.DirectoryPath
pathAndFile = Path.Combine(appPath, fileName)
If System.IO.File.Exists(pathAndFile) Then
'line = File.ReadAllLines(pathAndFile)
Else
Dim addLineToFile = System.IO.File.CreateText(pathAndFile) 'Create an empty txt file
Dim countButtons As Integer = 5 'add lines with your desired number of buttons
For i = 1 To countButtons
addLineToFile.Write("Button" & i & " = 0" & vbNewLine)
Next
addLineToFile.Dispose() ' and close file
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
writeToFile(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
writeToFile(2)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
writeToFile(3)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
writeToFile(4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
writeToFile(5)
End Sub
Public Sub writeToFile(buttonId As Integer)
Dim tmpButton As String = "Button" & buttonId
strbild = New StringBuilder
line = File.ReadAllLines(pathAndFile)
Dim f As Integer = 0
For Each lineToedit As String In line
If InStr(1, lineToedit, tmpButton) > 0 Then
Dim lineSplited = lineToedit.Split
Dim cnt As Integer = lineSplited.Count
Dim newVal = addCount(CInt(lineSplited.Last()))
lineSplited(cnt - 1) = newVal
lineToedit = String.Join(" ", lineSplited)
line(f) = lineToedit
End If
f = f + 1
Next
strbild = New StringBuilder
'putting together all the reversed word
For j = 0 To line.GetUpperBound(0)
strbild.Append(line(j))
strbild.Append(vbNewLine)
Next
' writing to original file
File.WriteAllText(pathAndFile, strbild.ToString())
Me.Refresh()
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
Public Function addCount(ByVal arr As Integer) As Integer
Return arr + 1
End Function
End Class
Output in myfile.txt
Button1 = 9
Button2 = 2
Button3 = 4
Button4 = 0
Button5 = 20
Create a vb winform Application
Drag on your form 5 buttons (Button1, Button2, ... etc)
copy that :
Imports System.IO.File
Imports System.IO.Path
Imports System.IO
Imports System.Text
Public Class Form1
Dim line() As String
Dim strbild As StringBuilder
Dim arr() As String
Dim i As Integer
Dim pathAndFile As String
Dim addLine As System.IO.StreamWriter
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim fileName As String = "myfile.txt"
Dim appPath As String = My.Application.Info.DirectoryPath
pathAndFile = Path.Combine(appPath, fileName)
If System.IO.File.Exists(pathAndFile) Then
line = File.ReadAllLines(pathAndFile)
Else
Dim addLineToFile = System.IO.File.CreateText(pathAndFile) 'Create an empty txt file
Dim countButtons As Integer = 5 'add lines with your desired number of buttons
For i = 1 To countButtons
addLineToFile.Write("Button" & i & " = 0" & vbNewLine)
Next
addLineToFile.Dispose() ' and close file
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
writeToFile(1)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
writeToFile(2)
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
writeToFile(3)
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
writeToFile(4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
writeToFile(5)
End Sub
Public Sub writeToFile(buttonId As Integer)
Dim tmpButton As String = "Button" & buttonId
strbild = New StringBuilder
line = File.ReadAllLines(pathAndFile)
i = 0
For Each lineToedit As String In line
If lineToedit.Contains(tmpButton) Then
Dim lineSplited = lineToedit.Split
Dim newVal = addCount(CInt(lineSplited.Last()))
'lineToedit.Replace(lineSplited.Last, newVal)
lineToedit = lineToedit.Replace(lineToedit.Last, newVal)
line(i) = lineToedit
End If
i = i + 1
Next
strbild = New StringBuilder
'putting together all the reversed word
For j = 0 To line.GetUpperBound(0)
strbild.Append(line(j))
strbild.Append(vbNewLine)
Next
' writing to original file
File.WriteAllText(pathAndFile, strbild.ToString())
End Sub
' Reverse Function
Public Function ReverseString(ByRef strToReverse As String) As String
Dim result As String = ""
For i As Integer = 0 To strToReverse.Length - 1
result += strToReverse(strToReverse.Length - 1 - i)
Next
Return result
End Function
Public Function addCount(ByVal arr As Integer) As Integer
Return arr + 1
End Function
End Class
Have fun ! :)CristiC777
try this on vs2013
change If confition
line = File.ReadAllLines(pathAndFile)
i = 0
For Each lineToedit As String In line
If InStr(1, lineToedit, tmpButton) > 0 Then
'If lineToedit.Contains(tmpButton) = True Then
Dim lineSplited = lineToedit.Split
Dim newVal = addCount(CInt(lineSplited.Last()))
lineToedit = lineToedit.Replace(lineToedit.Last, newVal)
line(i) = lineToedit
End If
i = i + 1
Next
I have data like this
date value
24sep2014 2:23:01 0.1
24sep2014 2:23:02 0.3
24sep2014 2:23:03 0.2
24sep2014 2:23:04 0.3
These are not coma seprated value. I wanted to write in CSV file. Apend the value for next row.
1)How to open file only once here. when it run next time file name has to change to other name
2) How to append the next values
Imports System
Imports System.IO.Ports
Imports System.ComponentModel
Imports System.Threading
Imports System.Drawing
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Dim myPort As Array
Dim Distance As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
myPort = IO.Ports.SerialPort.GetPortNames()
PortComboBox.Items.AddRange(myPort)
BaudComboBox.Items.Add(9600)
BaudComboBox.Items.Add(19200)
BaudComboBox.Items.Add(38400)
BaudComboBox.Items.Add(57600)
BaudComboBox.Items.Add(115200)
ConnectButton.Enabled = True
DisconnectButton.Enabled = False
Chart1.Series.Clear()
Chart1.Titles.Add("Demo")
'Create a new series and add data points to it.
Dim s As New Series
s.Name = "CURRENT"
s.ChartType = SeriesChartType.Line
End Sub
Private Sub ConnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ConnectButton.Click
SerialPort1.PortName = PortComboBox.Text
SerialPort1.BaudRate = BaudComboBox.Text
SerialPort1.Open()
Timer1.Start()
Timer2.Start()
'lblMessage.Text = PortComboBox.Text & " Connected."
ConnectButton.Enabled = False
DisconnectButton.Enabled = True
End Sub
Private Sub DisconnectButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DisconnectButton.Click
SerialPort1.Close()
DisconnectButton.Enabled = False
ConnectButton.Enabled = True
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim counter As Integer
counter = 0
Try
SerialPort1.Write("c")
System.Threading.Thread.Sleep(250)
Dim k As Double
Dim distance As String = SerialPort1.ReadLine()
k = CDbl(distance)
ListBoxSensor.Text = k
Dim s As New Series
s.Points.AddXY(1000, k)
Chart1.Series.Add(s)
Dim headerText = ""
Dim csvFile As String = Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not File.Exists(csvFile)) Then
headerText = "Date& time ,Current"
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
Dim y As String = DateAndTime.Now
Dim x As String = y + "," + distance
outFile.WriteLine(x)
End Using
Catch ex As Exception
End Try
End Sub
Private Sub Relay_ON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_ON.Click
SerialPort1.Write("1")
End Sub
Private Sub Relay_Off_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Relay_Off.Click
SerialPort1.Write("0")
End Sub
End Class
Here i am opening file again and again. that reason i can store only one value
# steve error
The second parameter of OpenTextFileWriter allows to append instead of overwrite your file.
So it is simply a matter to check if the file exists (so you insert the header names) and then write your data
Dim headerText = ""
Dim csvFile As String = Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not File.Exists(csvFile) Then
headerText = "Date& time ,Current"
End If
Using outFile = My.Computer.FileSystem.OpenTextFileWriter(csvFile, True)
If headerText.Length > 0 Then
outFile.WriteLine(headerText)
End If
Dim y As String = DateAndTime.Now
Dim x As String = y + "," + distance
outFile.WriteLine(x)
End Using
Notice the Using Statement to be sure to close and dispose the file resource also in case of exceptions.
However given the simple text that need to be written you could also choose to use the method WriteAllText
Dim headerText = ""
Dim csvFile As String = Path.Combine(My.Application.Info.DirectoryPath, "Current.csv")
If Not File.Exists(csvFile) Then
headerText = "Date& time ,Current" & Environment.NewLine
End If
Dim y As String = DateAndTime.Now
Dim x As String = headerText & y & "," + distance & Environment.NewLine
My.Computer.FileSystem.WriteAllText(csvFile, x, True)
I would to copy all files to another location, this program will filter -7 days, when it copied to another location, it will show the all file are copied in .log. I'm still in difficulties to get the log when ever i copied all the file to that location. Please see below:
Imports System
Imports System.IO
Public Class Form1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim DirectoryToCopy As String
Dim NewCopy As String
Dim d As Date = Date.Today
d = d.AddDays(-7)
DirectoryToCopy = "c:\test\hmm.gdb"
NewCopy = "D:\Data\hmm.gdb"
'NewCopy = "D:\Data\hmm.gdb" & d.AddDays(-7)
Dim objWriter As New System.IO.StreamWriter("D:\Data\update.log")
If System.IO.Directory.Exists(DirectoryToCopy) = True Then
My.Computer.FileSystem.CopyDirectory(DirectoryToCopy, NewCopy, True)
Directory.GetFiles(NewCopy, d)
d = Directory.GetCreationTime(NewCopy)
System.IO.File.Copy(DirectoryToCopy, NewCopy)
' System.IO.File.Copy(NewCopy, "D:\Data\update.log")
'objWriter.WriteLine("file copy")
'objWriter.Close()
'MsgBox("File Copied")
End If
End Sub
End Class
Here .. I changed your code ..
Imports System.IO
Public Class Form1
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Dim SourceFoDir As String= "C:\test"
Dim TargetDir As String= "D:\Data"
Dim NewCopy As String
Dim oW As New System.IO.StreamWriter
Dim d As Date = Date.Today
d = d.AddDays(-7)
If Not My.Computer.FileSystem.DirectoryExists(DirectoryToCopy) Then
oW = My.Computer.FileSystem.OpenTextFileWriter("D:\Data\update.log", True)
For Each f In Directory.GetFiles(DirectoryToCopy)
file.copy(f & d.Date.ToString, TargetDir)
oW.WriteLine(f & d.Date & " copied")
Next
oW.Close()
End If
End Sub
End Class