C
C#β€’9mo ago
Gamer1234556

βœ… Error with Displaying txt file in a Listbox

Hello, I am having some issues getting a text file to display it's contents in a generated display. Apparently when I run the debugger, it ends up displaying the contents in a seperate page with an additional txt extension attached to it. Can someone tell me what the problem is here? https://paste.mod.gg/qdpmcphcwgrr/0
BlazeBin - qdpmcphcwgrr
A tool for sharing your source code with the world!
42 Replies
leowest
leowestβ€’9mo ago
can u maybe post a screenshot of what happens
Gamer1234556
Gamer1234556β€’9mo ago
Ok sure
leowest
leowestβ€’9mo ago
from the code alone is being hard to figure out what hte actual issue is
Gamer1234556
Gamer1234556β€’9mo ago
No description
Gamer1234556
Gamer1234556β€’9mo ago
This is what I am getting, it's supposed to display in the listbox Should I send the UI of my application as well?
Gamer1234556
Gamer1234556β€’9mo ago
Here it is in case you need it to understand what is going on
No description
leowest
leowestβ€’9mo ago
@Gamer1234556 I suspect your try and catch clause if bubbling it try removing it commenting it
Gamer1234556
Gamer1234556β€’9mo ago
What do I replace it with?
leowest
leowestβ€’9mo ago
nothing just comment the try and catch
private void DisplayFileContents(string filePath)
{
//try
//{
string fileContent = File.ReadAllText(filePath);
GenerateValueDisplay.Text = fileContent;
//}
//catch (Exception ex)
//{
// MessageBox.Show($"Error reading file: {ex.Message}");
//}
}
private void DisplayFileContents(string filePath)
{
//try
//{
string fileContent = File.ReadAllText(filePath);
GenerateValueDisplay.Text = fileContent;
//}
//catch (Exception ex)
//{
// MessageBox.Show($"Error reading file: {ex.Message}");
//}
}
Gamer1234556
Gamer1234556β€’9mo ago
Yeah it seems like there are no changes
leowest
leowestβ€’9mo ago
super weird behavior then, can you show me your winform code the designer why did u have a try and catch there in first place?
Gamer1234556
Gamer1234556β€’9mo ago
BlazeBin - hkzfirtbjmop
A tool for sharing your source code with the world!
Gamer1234556
Gamer1234556β€’9mo ago
It worked before when I originally had a read-only text box. It was to check whether or not the dragged file was a txt file
leowest
leowestβ€’9mo ago
ah I see the issue GenerateValueDisplay is a ListBox not a textbox so GenerateValueDisplay.Items.Add(fileContent)
Pobiega
Pobiegaβ€’9mo ago
and that wont play very nicely, since your "items" is just a single long string a listbox is for many items, not a single one
leowest
leowestβ€’9mo ago
yep it wouldn't unless he processes it first to split by , and add the array which would make more sense since he wants to sort it in different ways
Pobiega
Pobiegaβ€’9mo ago
yup we've had that exact problem before with the int.Parse, remember? πŸ˜„
leowest
leowestβ€’9mo ago
me?
Pobiega
Pobiegaβ€’9mo ago
no, Gamer
Gamer1234556
Gamer1234556β€’9mo ago
Yeah
leowest
leowestβ€’9mo ago
:catlaugh:
Gamer1234556
Gamer1234556β€’9mo ago
I have been posting here for a while Pobiega has been a great help for me
leowest
leowestβ€’9mo ago
so if u do the change above does it work?
Gamer1234556
Gamer1234556β€’9mo ago
Well it works but it displays as one item So that's wrong
leowest
leowestβ€’9mo ago
yes it is but it works so u know what to do now right?
Gamer1234556
Gamer1234556β€’9mo ago
I think so... Do you have any pointers as for getting it to display as multiple items instead of one?
leowest
leowestβ€’9mo ago
read from there πŸ˜‰ what happens when u do that to a string what u get in result?
Gamer1234556
Gamer1234556β€’9mo ago
Yeah it's supposed to be an array
leowest
leowestβ€’9mo ago
yep which are multiple items πŸ˜‰ and if u add 1 by 1 to the list box what happens?
Gamer1234556
Gamer1234556β€’9mo ago
Hold on, I am overhauling the code to get the functionality to work
leowest
leowestβ€’9mo ago
tyt
Gamer1234556
Gamer1234556β€’9mo ago
Ok, so here is what I have now, but apparently it just skips over whatever items whenever it tries to parse through the text.
c#
//***********************************************************************************
//Program: DisplayFileContents()
//Description: Catches an error if the file doesn't have valid tokens.
//Date: Feb. 07/2024
//Author: Allen Keettikkal
//Course: CMPE1666
//Class: A02
//***********************************************************************************
private void DisplayFileContents(string filePath)
{
try
{
// Read all lines from the file
string[] fileContent = File.ReadAllLines(filePath);

// Convert lines to integer
// Update the listbox with file contents
GenerateValueDisplay.Items.Clear();

// Extract integers from lines and store in generatedArray
List<int> values = new List<int>();
foreach (string line in fileContent)
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
if (int.TryParse(part.Trim(), out int value))
{
values.Add(value);
}
else
{
MessageBox.Show($"Invalid value found: {part.Trim()}. Skipping...");
}
}
}
generatedArray = values.ToArray();
}
catch (Exception ex)
{
MessageBox.Show($"Error reading file: {ex.Message}");
}
}
c#
//***********************************************************************************
//Program: DisplayFileContents()
//Description: Catches an error if the file doesn't have valid tokens.
//Date: Feb. 07/2024
//Author: Allen Keettikkal
//Course: CMPE1666
//Class: A02
//***********************************************************************************
private void DisplayFileContents(string filePath)
{
try
{
// Read all lines from the file
string[] fileContent = File.ReadAllLines(filePath);

// Convert lines to integer
// Update the listbox with file contents
GenerateValueDisplay.Items.Clear();

// Extract integers from lines and store in generatedArray
List<int> values = new List<int>();
foreach (string line in fileContent)
{
string[] parts = line.Split(',');
foreach (string part in parts)
{
if (int.TryParse(part.Trim(), out int value))
{
values.Add(value);
}
else
{
MessageBox.Show($"Invalid value found: {part.Trim()}. Skipping...");
}
}
}
generatedArray = values.ToArray();
}
catch (Exception ex)
{
MessageBox.Show($"Error reading file: {ex.Message}");
}
}
leowest
leowestβ€’9mo ago
before using line I recommend u to check if its not null or empty on the Split method might also be worth adding to it the parameter to exclude empty elements after ',' if u put a , it will give u the option to add a StringSplitOptions enum aside from that you're not adding anyting to the listbox a few things on the file u linked:
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
MessageBox.Show("Invalid file path.");
return;
}

// Read all lines from the file
string[] fileContent = File.ReadAllLines(filePath);

// Check if file content is empty
if (fileContent == null || fileContent.Length == 0)
{
MessageBox.Show("File is empty.");
return;
}
if (string.IsNullOrEmpty(filePath) || !File.Exists(filePath))
{
MessageBox.Show("Invalid file path.");
return;
}

// Read all lines from the file
string[] fileContent = File.ReadAllLines(filePath);

// Check if file content is empty
if (fileContent == null || fileContent.Length == 0)
{
MessageBox.Show("File is empty.");
return;
}
you dont need to check if filepath is null File.Exists will fail for both so u can just use File.Exists there fileContent you dont need to check either if its empty your loop will be skiped
// Update the listbox with file contents
GenerateValueDisplay.Items.Clear();
GenerateValueDisplay.Items.AddRange(fileContent);
// Update the listbox with file contents
GenerateValueDisplay.Items.Clear();
GenerateValueDisplay.Items.AddRange(fileContent);
U dont want to call GenerateValueDisplay.Items.AddRange(fileContent); u want to do that after you processed the files on your resulting array
foreach (string line in fileContent)
{
// Split line by commas, excluding empty entries
string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string line in fileContent)
{
// Split line by commas, excluding empty entries
string[] parts = line.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
what happens if line is empty or null there? it would crash on the line.split so you want to check line before u use it on the split to ensure its not empty, if its empty just continue to the next line then
generatedArray = values.ToArray();
generatedArray = values.ToArray();
this is where u were suppose to fill your list but instead you're filling something else
Gamer1234556
Gamer1234556β€’9mo ago
Ok, I managed to get the DisplayFileContents() working. I needed to make a complete overhaul of the function since textboxes and listboxes are very different and now it is working. The problem now is the sorting algorithms aren't working since the array doesn't seem to exist. You want me to link the code?
leowest
leowestβ€’9mo ago
sure
Gamer1234556
Gamer1234556β€’9mo ago
BlazeBin - vroztmsfisel
A tool for sharing your source code with the world!
leowest
leowestβ€’9mo ago
doesn't GenerateValueDisplay.Items gives u a string array u can use to sort? u would take a copy of it work with it then update the listbox again ah I see it have its own object so you would have to create a field to store your array aside from feeding your listbox and if u modify your field u would update your list box with it alternatively you could cast the listbox item to your type into an array but if this is a homework it probably wont be accepted
listBox1.Items.Cast<string>().ToArray()
listBox1.Items.Cast<string>().ToArray()
this assumes your time was originally of type string in your listbox if it was not it would fail you also have to fix all your methods using .Text
Gamer1234556
Gamer1234556β€’9mo ago
Well I changed DisplayFileContents() to return a list But now I might have to change some of the code to accomodate this
Gamer1234556
Gamer1234556β€’9mo ago
BlazeBin - fnsphmbcrfiu
A tool for sharing your source code with the world!
Gamer1234556
Gamer1234556β€’9mo ago
What are some changes to be made here?
leowest
leowestβ€’9mo ago
SortedValueDisplay.Text I suppose but I guess now u should try and see what is not working and if u can fix your self without me telling u and if u can't then ask us ;P
Gamer1234556
Gamer1234556β€’9mo ago
Ok Yeah I fixed up code and it’s working properly Thanks for the help This place has been a ton of help for me
Want results from more Discord servers?
Add your server