Jump to content

Help with formatting Text before Displaying in ListBox (C#)

FuzzyYellow

Hi everyone, I'm working a project for my C# class, and I need to take a large text file, delete all puctuation and spaces, and display it in a List box with each word as a separate Item. so far I have the Text in a List, and the List into the ListBox, but I can't figure out how to display each word in it's own item in the ListBox, or remove the punctuation and social characters. here's my code so far.


            OpenFileDialog openfile1 = new OpenFileDialog();
           
           List<string> wordlist = new List<string>();
            
            if(openfile1.ShowDialog() == DialogResult.OK)
            {
                string filename = openfile1.FileName;
                
                List<string> words = File.ReadAllLines(filename).ToList();

                

                wordslb.DataSource = words;
                

            }

 

Recovering Apple addict

 

ASUS Zephyrus G14 2022

Spoiler

CPU: AMD Ryzen 9 6900HS GPU: AMD r680M / RX 6700S RAM: 16GB DDR5 

 

Link to comment
Share on other sites

Link to post
Share on other sites

2 hours ago, FuzzyYellow said:

social characters.

What's a social character?

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

54 minutes ago, Nuluvius said:

What's a social character?

I assume he means special chars. Maybe use string replace? https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx

 

 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

9 hours ago, vorticalbox said:

I assume he means special chars. Maybe use string replace? https://msdn.microsoft.com/en-us/library/fk49wtc1(v=vs.110).aspx

 

 

So does that mean I should put the entire text file into a string, then into the list? 

Recovering Apple addict

 

ASUS Zephyrus G14 2022

Spoiler

CPU: AMD Ryzen 9 6900HS GPU: AMD r680M / RX 6700S RAM: 16GB DDR5 

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, FuzzyYellow said:

So does that mean I should put the entire text file into a string, then into the list? 

once its in the list you can edit each one at a time. Just a quick question why aren't you removing the stuff you don't want before putting into a file? And why do you need them removed?

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

3 minutes ago, vorticalbox said:

once its in the list you can edit each one at a time. Just a quick question why aren't you removed the stuff you don't want before putting into a file? And why do you need them removed?

Because the goal of the project is to take a large text file (a book in this case) and separate each word and list them in a list box. I also have to create a function that searches the list for a user input and list info about that word. 

 

How exactly would I do that?

Recovering Apple addict

 

ASUS Zephyrus G14 2022

Spoiler

CPU: AMD Ryzen 9 6900HS GPU: AMD r680M / RX 6700S RAM: 16GB DDR5 

 

Link to comment
Share on other sites

Link to post
Share on other sites

3 hours ago, FuzzyYellow said:

Because the goal of the project is to take a large text file (a book in this case) and separate each word and list them in a list box. I also have to create a function that searches the list for a user input and list info about that word. 

 

How exactly would I do that?

If I were to approach such a project I'd begin by breaking the spec down into a series of concerns. To me it sounds as though you have the following:

  • You need to load a file
  • From the data that is loaded you need to parse the distinct words excluding all other characters
  • You need some metadata about each of the words
  • The words must be displayed in a list
  • There needs to be a facility to search the words
  • There also needs to be a facility to display the metadata that is associated with a selected or found word

Given those concerns I would think about a rough design:

  • A Word class to represent the word entity and its associated metadata
  • Some kind of file parser class that takes in a file and spits out a list of Words
  • A ViewModel for organising these Models so that the View can bind to them

It's perhaps not relevant for you but I'd drive the whole thing out using Test Driven Development (TDD). Skipping that then the end result would likely follow the Model View ViewModel (MVVM) design pattern. It would essentially consist of a View with a list box (very important) that would be bound to an ObservableCollection of Word ViewModels that wrapped Word Models organised in an overall ViewModel that contained most of the (trivial) business logic. I would have a progress bar that was notified via the Task-based Asynchronous Pattern (TAP) from another Model class that would be responsible for the parsing of the data. This would use the Task Parallel Library (TPL) and async await implementing TAP under the hood and supporting cancellation and progress. It would essentially care about taking in a file and spitting out a collection of Word Models. Finally I would have part of the View DataTemplate on the selection of a Word in the list box thus displaying the associated metadata in another little bit of View following the Master Detail design pattern.

 

As you go you could think about Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation and Dependency Inversion (SOLID). Dependency Injection (DI) and Inversion of Control (IoC) would also be great to get in there since you should use a ViewModelLocator and both of these concepts with your MVVM WPF application. MVVM Light is a great toolkit that includes all of this and much more and it happens to be my current favourite.

 

Hope that helps.

The single biggest problem in communication is the illusion that it has taken place.

Link to comment
Share on other sites

Link to post
Share on other sites

28 minutes ago, Nuluvius said:

If I were to approach such a project I'd begin by breaking the spec down into a series of concerns. To me it sounds as though you have the following:

  • You need to load a file
  • From the data that is loaded you need to parse the distinct words excluding all other characters
  • You need some metadata about each of the words
  • The words must be displayed in a list
  • There needs to be a facility to search the words
  • There also needs to be a facility to display the metadata that is associated with a selected or found word

Given those concerns I would think about a rough design:

  • A Word class to represent the word entity and its associated metadata
  • Some kind of file parser class that takes in a file and spits out a list of Words
  • A ViewModel for organising these Models so that the View can bind to them

It's perhaps not relevant for you but I'd drive the whole thing out using Test Driven Development (TDD). Skipping that then the end result would likely follow the Model View ViewModel (MVVM) design pattern. It would essentially consist of a View with a list box (very important) that would be bound to an ObservableCollection of Word ViewModels that wrapped Word Models organised in an overall ViewModel that contained most of the (trivial) business logic. I would have a progress bar that was notified via the Task-based Asynchronous Pattern (TAP) from another Model class that would be responsible for the parsing of the data. This would use the Task Parallel Library (TPL) and async await implementing TAP under the hood and supporting cancellation and progress. It would essentially care about taking in a file and spitting out a collection of Word Models. Finally I would have part of the View DataTemplate on the selection of a Word in the list box thus displaying the associated metadata in another little bit of View following the Master Detail design pattern.

 

As you go you could think about Single Responsibility, Open-Closed, Liskov Substitution, Interface Segregation and Dependency Inversion (SOLID). Dependency Injection (DI) and Inversion of Control (IoC) would also be great to get in there since you should use a ViewModelLocator and both of these concepts with your MVVM WPF application. MVVM Light is a great toolkit that includes all of this and much more and it happens to be my current favourite.

 

Hope that helps.

Thanks for this!

Recovering Apple addict

 

ASUS Zephyrus G14 2022

Spoiler

CPU: AMD Ryzen 9 6900HS GPU: AMD r680M / RX 6700S RAM: 16GB DDR5 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×