r/visualbasic • • Nov 11 '19

Reading and outputting text in visual form

Post image
7 Upvotes

8 comments sorted by

2

u/[deleted] Nov 11 '19

Hi so i'm a complete noob in visual basic, and i'm learning it in college, i need to output the separate pieces of information to each text box. How would i go about doing this? And how i would i do it when pressing a button?

4

u/chrwei Nov 11 '19

that appears to be a CSV file, lots of good guides on reading them in.

you can either parse the whole file into a list and use the button to pick the next item, or you can keep the file open and use the button to read and parse the next line.

there's probably 10 other ways to accomplish it too, if this is for school work, you'll want to look back at your notes and course work and pick something you should be using. asking on the internet is likely to get you answers you haven't learned yet and should not be using, yet.

2

u/[deleted] Nov 11 '19

Okay will read back on my notes, thought it was worth a shot anyway but yeah ur probably right, thanks anyway.

2

u/andrewsmd87 Web Specialist Nov 12 '19

FYI the reason this person told you to go look it up is that is a skill you're going to need to learn to be a successful programmer. Since this person mentioned it's a csv file google how to read a csv file with visual basic and then go from there.

If you run into problems, then post here with code and people will help out. It's not usual to get a comment like the one from laicure who basically wrote the code for you. Plus, it's doing you a disservice by just giving you the answer.

2

u/Laicure Nov 12 '19

oh cool VS2019!

Something like...

Public Class MainX
	Private ReadOnly EndangeredList As New List(Of Endangered)
	Dim CurrentIndex As Integer = 0

	Private Sub MainX_Load(sender As Object, e As EventArgs) Handles MyBase.Load
		Dim TxFile() As String = IO.File.ReadAllLines("file path goes here but do some file exists validation, at least")
		With EndangeredList
			'.Clear()
			'.TrimExcess()
			For x As Integer = 0 To TxFile.Count - 1
				Dim LineSplit() As String = TxFile(x).Split({","}, StringSplitOptions.RemoveEmptyEntries)
				Dim Amou As String = LineSplit(3)
				If Not IsNumeric(Amou) Then Amou = "0"
				EndangeredList.Add(New Endangered With {.Species = LineSplit(0), .Continent = LineSplit(1), .ThreatLevel = LineSplit(2), .AmountLeft = Convert.ToInt32(Amou)})
			Next
		End With
	End Sub

	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
		Dim Speciez As String = EndangeredList(CurrentIndex).Species
		Dim Continentz As String = EndangeredList(CurrentIndex).Continent
		Dim ThreatLevelz As String = EndangeredList(CurrentIndex).ThreatLevel
		Dim AmountLeftz As Integer = EndangeredList(CurrentIndex).AmountLeft

		'speciestextbox.text = Speciez
		'continenttextbox.text = Continentz
		'threatleveltextbox.text = ThreatLevelz
		'amountlefttextbox.text = AmountLeftz.tostring("#,0", globalization.cultureinfo.invariantculture)

		'next?
		CurrentIndex += 1
		If CurrentIndex = EndangeredList.Count Then CurrentIndex = 0
	End Sub

End Class

Friend Class Endangered
	Friend Property Species As String
	Friend Property Continent As String
	Friend Property ThreatLevel As String
	Friend Property AmountLeft As Integer 'or just make it as string (if you don't need it for calculations) just to be safe
End Class

1

u/taeratrin Nov 12 '19

This is similar to how I would do it, but is there any reason why you set the textbox properties manually instead of utilizing data binding?

1

u/Laicure Nov 13 '19

Oh, I just made it in an instant.

2

u/JasonHenley Nov 11 '19

As a general strategy, read the file, use the split function with vbCrLF as the delimiter to divide it into lines, then loop through the lines and parse again with split and "," as the delimiter.

Your input file isn't clean, though. For example, you have "Boa- " in one place, "Iguana,-" in another, "Frog = ", and "Ocelot,". You're going to have to account for those variations in your code.