How to cycle through array one at a time with button [closed] - vb.net

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
This is my code so far. My professor said I can not hard code. Can you please help me with the loop under the button click event. Right the flash form will load and pull up the first term and definition in the text files but when you click the next button it shows the path of the file that has been read in.
Public Class frmFlash
Private _intSizeOfArray As Integer = 3
Private _strLocationOfTerms(_intSizeOfArray) As String
Private _strLocationOfDefinitions(_intSizeOfArray) As String
Private _objReader As IO.StreamReader
Private _strTerms As String
Private Sub frmFlash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim objReader As IO.StreamReader
Dim strLocationOfTerms As String = "C:\Users\boone\Desktop\Terms.txt"
Dim strLocationOfDefinitions As String = "C:\Users\boone\Desktop\Definitions.txt"
Dim intTermArray(4) As Integer
Dim intCount As Integer
lblDefinition.Visible = False
If IO.File.Exists(strLocationOfTerms) Then
objReader = IO.File.OpenText(strLocationOfTerms)
_strTerms = objReader.ReadLine()
lblTerm.Text = _strTerms
End If
If IO.File.Exists(strLocationOfDefinitions) Then
objReader = IO.File.OpenText(strLocationOfDefinitions)
strLocationOfDefinitions = objReader.ReadLine()
objReader.Close()
lblDefinition.Text = strLocationOfDefinitions
End If
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim objReader(_intSizeOfArray) As IO.StreamReader
Dim intCount As Integer = 0
Dim strTermArray As String
Dim strLocationOfTerms As String = "C:\Users\boone\Desktop\Terms.txt"
Dim strLocationOfDefinitions As String = "C:\Users\boone\Desktop\Definitions.txt"
Do While intCount <= _intSizeOfArray
lblTerm.Text = strLocationOfTerms(_strTerms)
lblDefinition.Text = strLocationOfDefinitions
intCount += 1
Loop
End Sub
Private Sub btnDefinition_Click(sender As Object, e As EventArgs) Handles btnDefinition.Click
lblDefinition.Visible = True
End Sub
End Class

Since you are working with arrays, the easiest way to do this is to skip the processing in the button handler entirely. Load up your arrays in the Load event and just grab an IEnumerator for them.
'In Class level declarations
Private _termsEnumerator As IEnumerator(Of String)
Private _defsEnumerator As IEnumerator(Of String)
Private Sub frmFlash_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Your other initialization code...
'...
'Load your _strLocationOfTerms and _strLocationOfTerms here.
_termsEnumerator = _strLocationOfTerms.GetEnumerator()
_defsEnumerator = _strLocationOfDefinitions.GetEnumerator()
End Sub
Then it's just as simple as grabbing the next item from the arrays (they implement IEnumerable):
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
If _termsEnumerator.MoveNext() And _defsEnumerator.MoveNext() Then
lblTerm.Text = _termsEnumerator.Current
lblDefinition.Text = _defsEnumerator.Current
End If
End Sub

Related

Passing data from one form to another form's textboxes

I am trying to pass the selected nodes from a treeview to another form that is displayed in the text boxes.
this is my code in TreeView1_NodeMouseDoubleClick event
the code works fine when the data from the selected treeview is shown in the same form, but the problem is when I want to pass it to the other form 4 nothing is shown and the debug shows me that if it receives the values ​​but they are not reflected, maybe my code to refer to the other form is wrong I would like a support with this case.
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim nodeKey As String = TryCast(e.Node.Tag, String)
Dim form4 As New Form4
' Dim nodetext As String = TryCast(e.Node.Tag, String)
'Dim nodeKey As String = DirectCast(e.Node.Tag, String)
If nodeKey IsNot Nothing AndAlso nodeKey.StartsWith("DIST") Then
'You have double clicked a district node
Dim IDDISTRITO As Integer = Integer.Parse(nodeKey.Substring(4))
form4.lblco.Text = IDDISTRITO
'Do something with the district id here
'...
form4.txtdis.Text = e.Node.Text
form4.txtpro.Text = e.Node.Parent.Text
form4.txtdepa.Text = e.Node.Parent.Parent.Text
End If
End Sub
so I call form 3 where is the treeview
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
for3.Show()
End Sub
As you assumed, yes you are referencing the forms incorrectly. Please follow this technique. In form3 class, declare a public variable Public Dim form4 As New Form4.
Then modify the form3 calling procedure like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
for3.form4 = Me
for3.Show()
End Sub
Now modify your existing code to this :
Private Sub TreeView1_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseDoubleClick
Dim nodeKey As String = TryCast(e.Node.Tag, String)
' Dim nodetext As String = TryCast(e.Node.Tag, String)
'Dim nodeKey As String = DirectCast(e.Node.Tag, String)
If nodeKey IsNot Nothing AndAlso nodeKey.StartsWith("DIST") Then
'You have double clicked a district node
Dim IDDISTRITO As Integer = Integer.Parse(nodeKey.Substring(4))
form4.lblco.Text = IDDISTRITO
'Do something with the district id here
'...
form4.txtdis.Text = e.Node.Text
form4.txtpro.Text = e.Node.Parent.Text
form4.txtdepa.Text = e.Node.Parent.Parent.Text
End If
End Sub
I have not tested the above code, but I believe you have got the idea.
Well I managed to solve it, the simplest thing was complicated, I publish the solution.
form 4 by calling 3
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim for3 As New Form3
AddOwnedForm(for3)
for3.ShowDialog()
End Sub
just add this code and voila
Dim form4 As Form4 = CType(Owner, Form4)

Why is my form not loading data from .mdb file?

This is a homework assignment that I am working on. I have an .mdb file that is supposed to connect to my application and then I should be able to navigate among the records. The database is connected as a data source. But, when I run the application no data is populated and I get a IndexOutOfRangeException was unhandled error when I press a button on my toolstrip. I have tried to ask for assistance from the professor but she has been non-existent all semester. What am I doing wrong here? I'm only looking for help for where to focus my attention so that I can figure this out on my own.
Public Class Form1
Dim strMemoryConnection As String = "Provider = Microsoft.Jet.OLEDB.4.0; Data Source = " &
Application.StartupPath & "\memory.mdb"
Dim strSQLMem As String
Dim dtMem As New DataTable()
Dim intTotalRows As Integer
Dim intCurrentRow As Integer
Private Sub displayRecord()
Me.txtTitle.Text = CStr(dtMem.Rows(intCurrentRow)("title"))
Me.txtAuthor.Text = CStr(dtMem.Rows(intCurrentRow)("author"))
Me.txtPublisher.Text = CStr(dtMem.Rows(intCurrentRow)("publisher"))
Me.txtStuff.Text = CStr(dtMem.Rows(intCurrentRow)("stuff"))
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventHandler)
dtMem.Clear()
strSQLMem = "SELECT * FROM Memory"
Dim dataAdapter As New OleDb.OleDbDataAdapter(strSQLMem, strMemoryConnection)
dataAdapter.Fill(dtMem)
dataAdapter.Dispose()
intTotalRows = dtMem.Rows.Count
intCurrentRow = 0
displayRecord()
End Sub
#Region "Tool Strip Button Clicks"
Private Sub btnTop_Click(sender As Object, e As EventArgs) Handles btnTop.Click
intCurrentRow = 1
displayRecord()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
intCurrentRow = intCurrentRow - 1
If intCurrentRow < 0 Then
intCurrentRow = 1
End If
displayRecord()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
intCurrentRow = intTotalRows + 1
If intCurrentRow = intTotalRows Then
intCurrentRow = intTotalRows - 1
End If
displayRecord()
End Sub
Private Sub btnBot_Click(sender As Object, e As EventArgs) Handles btnBot.Click
intCurrentRow = intTotalRows - 1
displayRecord()
End Sub
#End Region
End Class
In the end, it was as I expected. The data was not loading properly. I finally realized that the Form1_Load argument was not correct.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventHandler)
Needed to be:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
I will be letting my professor and fellow classmates know that this is incorrect.
Thanks to Bugs for the troubleshooting. At the least, I now know how to create a SQL connection very easily. I also appreciate whomever downvoted my question and their help in figuring this out.

Extract a single line from a webpage VB2008

I'm looking to extract only the line that starts "A SUNOT..." from this website. https://pilotweb.nas.faa.gov/common/nat.html
I then want to paste that line into a text box in VB2008.
I tried using:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim TrackA As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://pilotweb.nas.faa.gov/common/nat.html")
Dim gather As System.Net.HttpWebResponse = TrackA.GetResponse
Dim write As System.IO.StreamReader = New System.IO.StreamReader(gather.GetResponseStream)
RawData.Text = write.ReadLine(?)
End Sub
End Class
I got it to write the entire page but I wanted just that line.
The '?' is to show if that ReadLine command is the right thing to be using there.
Thanks, James
Try the following code
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim TrackA As System.Net.HttpWebRequest = System.Net.HttpWebRequest.Create("https://pilotweb.nas.faa.gov/common/nat.html")
Dim gather As System.Net.HttpWebResponse = TrackA.GetResponse
Dim write As System.IO.StreamReader = New System.IO.StreamReader(gather.GetResponseStream)
Dim ContentStr As String = write.ReadToEnd
Dim StartIndex As Integer = ContentStr.IndexOf("A SUNOT")
Dim StrLength As Integer = ContentStr.IndexOf(vbLf, StartIndex) - StartIndex
RawData.Text = ContentStr.Substring(StartIndex, StrLength)
End Sub

vb.net Entering and retrieving data from a Array [duplicate]

This question already has answers here:
Building a multidimensional array in vb.net
(5 answers)
Closed 9 years ago.
Below is my code to enter data into an array and retrieve the information
Entering the data seems to have no problems but when I click the button to retrieving the information it just presents 0's in the text box.
Public Class Form1
Dim Array(20) As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim counter As Integer
For counter = 0 To ListBox1.Items.Count - 1
Array(counter) = txtEnterMarks.Text(counter)
Next
Label1.Text = "omfg"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For counter = 0 To Array.Length - 1
ListBox1.Items.Add(Array(counter))
Next
End Sub
End Class
Again .. try this different answer
Structure Student
Dim Name As String
Dim Mark As Integer
End Structure
To save your input in it :
Dim StudentsInfo as New List(Of Student)
Dim si As New Student
si.Name = txtEnterName.Text
si.mark = val(txtEnterMark.Text)
StudentsInfo.Add(si)
To get value from StudentsInfo
txtEnterName.Text = StudentsInfo(0).Name
txtEnterMark.Text = StudentsInfo(0).mark.ToString
About preserve it To ListView .. Again .. Try Mr. Google ...

How do you read a file and display in listbox in VB.NET?

I was trying to display data from file in a list saved on the hard drive by clicking on a button, however I'm not sure on haw to do it properly:
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Dim TextLine As String
If System.IO.File.Exists(Filename) = True Then
Dim RecipeReader As New System.IO.StreamReader(Filename)
Do While RecipeReader.Peek() <> -1
TextLine = TextLine & RecipeReader.ReadLine() & vbNewLine
Loop
lstRecipes.Text = TextLine.Text
Else
MsgBox("File Does Not Exist")
End If
End Sub
I would be really grateful for assistance :D
Private Sub btnListRecipes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnListRecipes.Click
Try
lstRecipes.AddRange(File.ReadAllLines(FileName))
Catch
MsgBox("Unable to read file")
End Try
End Sub
Use:
lstRecipes.Items.Add(TextLine.Text)
More specifically before it jumps to the next item in the list, so this would go right after your assignment of TextLine.
you can do this:
Imports System
Imports System.IO
Public Class Form1
Public Shared listTXT, listOfTxt, listOfTxtFile As New List(Of String)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "C:\myDirectory\"
Dim parentinfo As New DirectoryInfo(path)
' Store Text file name in a list
For Each txtFile As FileSystemInfo In parentinfo.GetFileSystemInfos()
listTXT.Add(txtFile.Name)
Next
' Store Path of Text file in a list
For noOfTxtFile = 0 To listTXT.Count - 1
Dim pathOfFile As String = path & listTXT(noOfTxtFile)
listOfTxtFile.Add(pathOfFile)
Dim obj As System.IO.StreamReader
obj = System.IO.File.OpenText(pathOfFile)
While Not obj.EndOfStream
listOfTxt.Add(obj.ReadLine)
End While
Next
Dim lineOfTxt As Integer
Dim txt As String
For lineOfTxt = 0 To listOfTxt.Count - 1
txt = listOfTxt(lineOfTxt)
ListBox1.Items.Add(txt)
Next
End Sub
End Class