How to enter additional data entered in text files in vb.net? - vb.net

I'm currently working on a project where new orders are added to text files and the details of these orders must be displayed in text boxes.
I managed to get the application for 'Order 1' but I am unsure as to how to display details for orders that have not yet been added to the text files e.g. Order 20.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim query = IO.File.ReadAllLines("Order1.txt")
Dim name = query(0).Split(","c)(0)
Dim address = query(0).Split(","c)(1)
Dim phonenum = query(0).Split(","c)(2)
Dim method = query(0).Split(","c)(3)
txtName.Text = name
txtAddress.Text = address
txtPhoneNum.Text = phonenum
txtMethod.Text = method
End Sub

I do not know if I understand correctly what you want.
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim yourModelLists As New List(yourModel)
Do
Dim query = IO.File.ReadLine("Order1.txt")
Dim yourModelItem As yourModel
If Not (String.IsNullOrEmpty(query) Or String.IsNullOrWhiteSpace(query) Then
Dim column As Integer = 0
Dim items As String() = query.Split(";")
For Each item In items
If Not (String.IsNullOrEmpty(item) Or String.IsNullOrWhiteSpace(item)) Then
Select Case column
Case 0
yourModelItem.name = item
Case 1
yourModelItem.address = item
Case 2
yourModelItem.phonenum = item
Case 3
yourModelItem.method = item
Case Else
'Do something
End Select
End If
Next
End If
yourModelLists.Add(yourModelItem)
Loop Until (String.IsNullOrEmpty(query) Or String.IsNullOrWhiteSpace(query))
End Sub
All you need is in yourModelLists.

Related

The row is overwriting and not adding rows listview

Im sending the data from form1(Home) to form2 (StatusReport) but the information taken from form 1 is not adding to the next row in the form2, instead it is overwriting the same row. I did it on the form 1, the adding per row but from sending the data from form 1 to form 2, it's not adding properly.
Form 1 code
Dim recipientName As String = TextBox5.Text
Dim address As String = TextBox6.Text
Dim contactNumber As String = TextBox7.Text
Dim deliveryMode As String = ComboBox3.SelectedItem
Dim deliveryDate As Date = DateTimePicker1.Value.Date
Form 2 code
Dim recipientName As String = Home.TextBox5.Text
Dim address As String = Home.TextBox6.Text
Dim contactNumber As String = Home.TextBox7.Text
Dim deliveryMode As String = Home.ComboBox3.SelectedItem
Dim deliveryDate As Date = Home.DateTimePicker1.Value.Date
Dim orderStatus As String = "Pending"
Dim str(6) As String
Dim lvItem As ListViewItem
str(0) = recipientName
str(1) = address
str(2) = contactNumber
str(3) = deliveryMode
str(4) = deliveryDate
str(5) = orderStatus
lvItem = New ListViewItem(str)
ListView1.Items.Add(lvItem)
Creating a new ListViewItem using a String Array creates a single ListViewItem with the array signifying the sub items.
Maybe simpler to create the ListViewItems individually and add them, like this:
lv.Items.Add(New ListViewItem(recipientName))
lv.Items.Add(New ListViewItem(Address))
lv.Items.Add(New ListViewItem(contactNumber))
lv.Items.Add(New ListViewItem(deliveryMode))
lv.Items.Add(New ListViewItem(recipientName))
etc.
You may want to clear the items first:
lv.Items.Clear()
It is unclear how you are going back and forth between the 2 forms but this should get you started. Set up the columns in the ListView before adding any items. You can either do this once in code or at design time. ListViews hold ListViewItems. The first column is the Text property and the following columns are SubItems.
Private Sub CreateLVColumns()
'you will probably want to do this at design time
With ListView1.Columns
.Add("Name")
.Add("Address")
.Add("Number")
.Add("Mode")
.Add("Date")
.Add("Status")
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim lvi As New ListViewItem(Home.TextBox5.Text)
With lvi.SubItems
.Add(Home.TextBox6.Text)
.Add(Home.TextBox7.Text)
.Add(Home.ComboBox3.Text)
.Add(Home.DateTimePicker1.Value.Date.ToString) 'you can add a format to the ToString
.Add("Pending")
End With
ListView1.Items.Add(lvi)
End Sub
found the answer, this is just the base. Just added some codes to fit into the required system.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim itm As New ListViewItem(TextBox1.Text)
itm.SubItems.Add(TextBox2.Text)
itm.SubItems.Add(TextBox3.Text)
Form2.ListView1.Items.Add(itm)
Form2.Show()
Me.Hide()
End Sub
End Class
Public Class Form2
Private Sub ListView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form1.Show()
End Sub
End Class
Leaving this here as it can help others too.

How to count selected items in the listbox by just select and see result in a label?

getting the logic of counting is easy but in practice it gets hard sometimes.
Now I've a list with many items on it. how to count those items if there were repeated and i want to convert that to a number using the FOR loop since i know how many items in the list.
I tried some codes but i did not succeed
'''''''''''''''
' VB 2015
''''''''''''
Public Class Form1
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim strselection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = strselection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
End Class
for EX:
i wanna count the word "michigan " how many times repeated in the list by just clicking on it ?
Here's an example using Jim Hewitt's comment:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
Dim wins As Integer = (From team As String In lstWinners_List.Items Where team.Equals(selection)).Count
lblOutput.Text = wins.ToString
End If
End Sub
Edit
Here's an equivalent, manual, indexed for loop:
Private Sub lstWinners_List_SelectedIndexChanged(sender As Object, e As EventArgs) Handles lstWinners_List.SelectedIndexChanged
If lstWinners_List.SelectedIndex <> -1 Then
Dim count As Integer = 0
Dim selection As String = lstWinners_List.Items(lstWinners_List.SelectedIndex).ToString
For i As Integer = 0 To lstWinners_List.Items.Count - 1
If lstWinners_List.Items(i) = selection Then
count = count + 1
End If
Next
lblOutput.Text = count.ToString
End If
End Sub
Dim count As Integer = 0
For Each n As String In ListBox1.Items
If n = "red" Then
count += 1
End If
Next
lblOutput.Text(count)
something like this do you mean?

Datagridview doesnt auto select first row after filling datasource

i have datagridview with 4 columns
[Nam] , [ID] , [Shuru] , [Payan]
second column ([ID]) is hidden and is used to gather some details about the data.
this is the Load code :
Private Sub SalMali_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DGV_SalMaliTableAdapter.Fill(Me.FDBDataSet.DGV_SalMali)
End Sub
which retrieves everything in DGV_SalMali table.
i know that datagridview automatically selects first row after a fill so i put a function in the Selection Change to retrieve data. here is the code :
Private Sub DGV_SalMaliDataGridView_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DGV_SalMaliDataGridView.SelectionChanged
refreshDT()
End Sub
Public Sub refreshDT()
Dim cnt As Integer = DGV_SalMaliDataGridView.Rows.Count
If cnt = 0 Then
GoTo line
End If
unlockAll()
Dim sel As String = DGV_SalMaliDataGridView.SelectedRows(0).Cells(1).Value
Dim dt As New DataTable
dt = SalMaliTA.GetData_SalMali_B_ID(sel)
Dim dtr As DataRow = dt.Rows(0)
TextBox1.Text = dtr.Item(4)
MaskedTextBox1.Text = mc.MtoS(dtr.Item(1))
MaskedTextBox2.Text = mc.MtoS(dtr.Item(2))
CheckBox1.Checked = dtr.Item(3)
Exit Sub
line:
lockAll()
End Sub
but when I call refreshDT() the selection doesn't work anymore. it throws an error on line 8 of refreshDT()
sub { exactly here : Dim sel As String = DGV_SalMaliDataGridView.SelectedRows(0).Cells(1).Value }
Thrown: "Index was out of range. Must be non-negative and less than
the size of the collection."
(System.ArgumentOutOfRangeException) Exception Message = "Index was
out of range. Must be non-negative and less than the size of the
collection.", Exception Type = "System.ArgumentOutOfRangeException"
datagridview has rows and i dont know what the problem is.
thanks for help.
you're currently checking if there are any rows but not any selected rows:
Public Sub refreshDT()
Dim cnt As Integer = DGV_SalMaliDataGridView.Rows.Count
If cnt = 0 Then
lockAll()
Exit Sub
End If
If DGV_SalMaliDataGridView.SelectedRows.Count = 0 Then
DGV_SalMaliDataGridView.Rows(0).Selected = true
End If
unlockAll()
Dim sel As String = DGV_SalMaliDataGridView.SelectedRows(0).Cells(1).Value
Dim dt As New DataTable
dt = SalMaliTA.GetData_SalMali_B_ID(sel)
Dim dtr As DataRow = dt.Rows(0)
TextBox1.Text = dtr.Item(4)
MaskedTextBox1.Text = mc.MtoS(dtr.Item(1))
MaskedTextBox2.Text = mc.MtoS(dtr.Item(2))
CheckBox1.Checked = dtr.Item(3)
End Sub

VB.NET Read text file line by line and set every line in textbox with button clicks

hello i want to read text file line by line and set every lien in textbox eash time i click the button
this is my code and working fine. but i looking for easy way to read large text that has more thean 5 lines ?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Static count As Integer
count = count + 1
Dim textfile As String = "c:\test.txt"
If IO.File.Exists(textfile) Then
Dim readLines() As String = IO.File.ReadAllLines(myFile)
If count = 1 Then
TextBox1.Text = readLines(0)
End If
If count = 2 Then
TextBox1.Text = readLines(1)
End If
If count = 3 Then
TextBox1.Text = readLines(2)
End If
If count = 4 Then
TextBox1.Text = readLines(3)
End If
If count = 5 Then
TextBox1.Text = readLines(4)
End If
If count = 6 Then
TextBox1.Text = readLines(5)
End If
End If
End Sub
I think you need to read the file just one time when you load your form (assuming a WinForms example here)
' Declare these two globally
Dim readLines() As String
Dim count As Integer = 0
Private void Form_Load(sender As Oject, e As EventArgs) Handles Base.Load
' In form load, read the file, just one time
Dim textfile As String = "c:\test.txt"
If IO.File.Exists(textfile) Then
readLines() As String = IO.File.ReadAllLines(myFile)
else
TextBox1.Text "File not found"
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' Check if you have a file and if you don't have reached the last line
if readLines IsNot Nothing AndAlso count < readLines.Length Then
TextBox1.Text = readLines(count)
count += 1
else
TextBox1.Text "End of file"
End If
End Sub
This reduces the amount of code you need to write:
TextBox1.Text = readLines(count - 1)

Pass variable to new form with Datatable and Listbox

I am currently trying to write an application like address book. Listbox works properly, it shows everything corretly. But I need to pass id of chosen listbox item to another form. I got code like this in Form2:
Private myTable As New DataTable()
Public Sub LoadXml(sender As Object, e As EventArgs) Handles Me.Load
With myTable.Columns
.Add("DisplayValue", GetType(String))
.Add("HiddenValue", GetType(Integer))
End With
myTable.DefaultView.Sort = "DisplayValue ASC"
ListBox1.DisplayMember = "DisplayValue"
ListBox1.ValueMember = "HiddenValue"
ListBox1.DataSource = myTable
Dim doc As New Xml.XmlDocument
doc.Load("c:\address.xml")
Dim xmlName As Xml.XmlNodeList = doc.GetElementsByTagName("name")
Dim xmlSurname As Xml.XmlNodeList = doc.GetElementsByTagName("surname")
Dim xmlId As Xml.XmlNodeList = doc.GetElementsByTagName("id")
For i As Integer = 0 To xmlName.Count - 1
Dim nazwa As String = xmlName(i).FirstChild.Value + " " + xmlSurname(i).FirstChild.Value
myTable.Rows.Add(nazwa, xmlId(i).FirstChild.Value)
MsgBox(myTable.Rows(i).Item(1).ToString)
Next i
ListBox1.Sorted = True
End Sub
Later in the code I have event:
Public Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
End Sub
I would like to know how can I call id from DataTable for selected listbox item. I hope u understand what I mean since my english is not perfect :)
Since you have added the XML value id to the data table column HiddenValue and you have assigned HiddenValue as the ValueMember for the listbox, once a record is selected in the listbox, id will be available in the listbox's [SelectedValue][1] member. For example:
Public Sub ListBox1_DoubleClick(sender As Object, e As EventArgs) Handles ListBox1.DoubleClick
MsgBox("Selected Id: " & ListBox1.SelectedValue.ToString())
End Sub