Question Your help with reading from text please

Superfurry

Member
Joined
Feb 8, 2011
Messages
19
Programming Experience
1-3
Hi I'm not really sure how to explain this but il have a go. Ok I have a text file with some numbers, next to the number is a description. What I would like to do is when I type a number in a textbox it will print the description in another box . Can I do this with txt file or does it have to be XML or something. How would I go about this please? Thank you


Thanks, superfurry :)
 
Can I do this with txt file or does it have to be XML or something.
That would be at your preference.
How would I go about this please?
What you need to do first is to read the file, System.IO namespace have tools for this. It's also in your interest to handle the file line by line, so you can use File.ReadAllLines method.
Then for each line you want to split it up by the number key and the description value, String.Split method can be used.
A Dictionary(Of Tkey, Tvalue) class is typically used to hold and do lookups on unique key/value pairs, for you that should be a Dictionary(Of String, String). This lookup will be used later from a different method than the one you use to load the data, so declare the Dictionary as a private field at module level. Fill it with your key/value pairs.

Done with the loading, it's time to handle the input. One option is to use the KeyUp event for the input TextBox, and check if user pressed the Enter key, then proceed to check the lookup table against the input text. The dictionary has a ContainsKey function that you can use to validate that the input matches a key, if it is a valid key you can retrieve the value and assign that to the description TextBox.Text property.

All help topics I linked to has code samples that should help you with each step.
 
Back
Top