Change selected ComboBox item, from text file string not working - vb.net

Ok.
So I am working on a project, irrelevant, and I have a bunch (8) of ComboBoxes (they are in DropDownList mode) and there is 8 save files. I have them being imported and converted to strings:
Using class2 As New StreamReader(path & "SaveData/classdata/classdata2.NIC")
Dim fdcount1 As String
fdcount = class2.ReadToEnd()
MessageBox.Show(fdcount1)
hr2choice.SelectedItem = fdcount1
End Using
I already tested this, and it seems to be working.
(Test code I used:)
MessageBox.Show(fdcount1)
and it showed the value ("DiVita")
Despite this, when I tried setting the ComboBox value to this, it did not seem to work.
The ComboBox does have this value in it, and if I try this, it works:
hr2choice.SelectedItem = "DiVita"
For whatever reasons though, it does not work when I try doing it directly from the string.
Thanks for any help with this!
Nic

To answer this, I have to assume that the data in the text file is formatted as one line for each piece of data.
There seems to be a couple of issues with your code. fdcount is just declared as a string where it should be an array to make it easier to access each line that is read from the file. fdcount1 has no relationship to fdcount - it is a completely separate entity, so the data in fdcount1 is coming from somewhere else.
Rather than the above code, It's easier to use this
Dim fdcount() As String
fdcount = File.ReadAllLines("SaveData/classdata/classdata2.NIC")
MessageBox.Show(fdcount(1))
Note that fdcount is declared as an Array of String. The 2nd line does all the opening, reading into the array, and closing of the file.
You can then access each element of the array as shown in the 3rd line.

Related

Read and split line by line in text file

I am trying to read a text file from my applications resources. For each line in this text file I want to split the text before and after the comma.
Each line in txt file looks like this:
-125.325235,4845636
My issue is that the function loops and does not end constantly repeating the for each statement
For Each Line As String In My.Resources.CompanyBases
MsgBox(My.Resources.CompanyBases.Split(","c).First)
MsgBox(My.Resources.CompanyBases.Split(","c).Last)
Next
Firstly, don't ever get a resource over and over like that. Those properties are not "live". Every time you get the property, the resource has to be extracted from your assembly. If you need to use the value multiple times, get the property once and assign it to a variable, then use that variable over and over.
Secondly, you're not getting a file. The whole point of resources is that they are not distinct files but rather data compiled into your assembly. It's just a String like any other. How would you usually split a String on line breaks?
Finally, you have a For Each loop with a loop control variable Line, yet you never use that variable inside the loop. It should be Line that you're splitting inside the loop, not the resource property containing all the lines.
For Each line In My.Resources.CompanyBases.Split({Environment.NewLine}, StringSplitOptions.None)
Dim fields = line.Split(","c)
Debug.WriteLine(fields(0))
Debug.WriteLine(fields(1))
Next
Note that, if you're using .NET Core, Split will accept a String as well as a String array.

Searching through a file and seperate data in a file with a separator value in vb.net

Hello I want some help with a code to search through the text file and to separate the text from a separator value followed by a rogue(stopping) value.
Each line of text in the text file is: for example with sample data:
DAC11010|This is a Desk and a chair|$100;.
In the above example has the | sign as a seperator value and ;sign as a rogue(stopping) value. The separator value separate the text DAC11010 , This is a Desk and a chair , $100 the rogue(stopping) value means that it it is the EOLN(end of line) and ready to check the next line.
The separated text goes to 3 ListBoxes and the data added one after the other. The DAC11010 goes to the lstItemCode , This is a desk and chair goes to the lstDescription , $100 goes to the lstPrice.
I would like if someone would volunteer to help me program the code in vb.net. I hope I made myself clear.
Thank you [shannon].
If you haven't already done so, please turn on Option Strict. This is a 2 part process. First for the current project - In Solution Explorer double click My Project. Choose Compile on the left. In the Option Strict drop-down select ON. Second for future projects - Go to the Tools Menu -> Options -> Projects and Solutions -> VB Defaults. In the Option Strict drop-down select ON. This will save you from bugs at runtime.
I will not write the code for you. That is not the purpose of Stack Overflow. We help with specific problems you are having with your code. You don't appear to have any code.
I will explain one way your goal might be accomplished.
Declare 3 New List(Of String) variables to hold the data for the three list boxes. We will be looping through the text files lines but we don't want to update the user interface until after the loop so we will collect the data in memory.
You will need to import System.IO to use methods of the File class. You can use File.ReadAllLines to get an array of strings; each member of the array will contain one line from the text file.
Next, you will want to loop through the lines splitting each line into 3 sections. Use the For Each...Next type of loop. You can use the String.Split method passing in your "|"c parameter. The c tells the compiler that this is a Char not a string because .Split is expecting a Char Now you can use the .Add method to add the sections to the appropriate list.
After the loop, you can assign the 3 list to the .DataSource property of the appropriate ListBox.
Now write the code and if you have any problems ask a new question showing the specific code where you are having the problem.

VB.NET determine which property is not yet assigned a value

I'm doing a visual studio 2010 project in VB.NET to import an ASCII file into a databae based on given fields in the ASCII file. Sometimes, there can be some optional fields missing from the ASCII file while importing.
How can I find out which property of a big class has not yet been assigned a value? Based on this, I want to show a form to request for the missing properties at run time!
Someone correct me if there is a faster way,
Dim myClassInstance As new MyClass();
If myClassInstance.SomeProperties is Nothing Then
'I'm null
End If
Do this for all properties that needs to be checked.

How do I retain TextBox line breaks in Winforms after assigning text to a string variable?

I have a WinForms app with a multi-line textbox. This displays and retains (after loading from the DB) line break characters fine.
However if I assign the TextBox.Text value to a string variable and then re-assign the variable back to the TextBox.Text property, the line break characters are lost and replaced with a square character (can't past them here as they just paste as a line break!)
I've tried:
Replace("\n",vbcrlf)
but to no avail.
Can anyone suggest a solution?
UPDATE**
I haven't managed to fix this but have worked around it by avoiding the variable assignment. I now pass a reference to the RichTextBox and directly manipulate the text there. Note that this seems specific to the RichTextBox as I don't see the issue with a normal TextBox.
Instead of Replace("\n"' vbcrlf), you need, to use either Replace(vblf, vbcrlf), or Replace(vbcr, vbcrlf).
Just an idea:
How about fixing ā€œ\r\nā€ instead of ā€œ\nā€?
Are you transforming the string anyway? Since something like this:
Dim s As String = TextBox1.Text
TextBox1.Text = s
works well.
textBox.AppendText("your new text" & Environment.NewLine)

An item with the same key has already been added - csvreader.fieldcount

I'm trying to create an import program from CSV.
My code is
csv = New CsvReader(New StreamReader("CSVFileLocation"), True)
Dim fieldCount As Integer = csv.FieldCount
The error message "An item with the same key has already been added." on the second line.
If I changed "HasReaders" to "False", there's no such error. But, I'm not able to get the Headers.
Could somebody help me on this, please?
FYI: I'm using Visual Studio 2010 version.
Regards,
Richard
Check that your CSV file may have duplicate column names, or multiple empty cells, in the header row?
If that's the case, try to loop through your csv object, and try rename the headers in code before calling the property FieldCount.
My guess is that the CsvReader class is going through the first row adding strings to a dictionary, and the header row has two cells with the same value (so two identically named fields). Take a look at your data and see if this is the case. Alternately, if you have access to the source code for CsvReader, you could have it handle this case by naming the second field something slightly different (e.g., by appending a "1" onto the end of its name).