简体   繁体   中英

Need to use a file to fill an array, then a listbox. I managed to get it filled with something but I can't get it to display properly. advice?

Basically, the assignment I have is with one program I need to write some info into a file, then in another use that file to fill a listbox with names and a lookup function. My current problem is that I am have a lot of trouble getting the file to be read in properly. Relevant Info: the info the other file writes included name, phone number, cell number, pager number, voicemail, and email address. All is written into the file via textbox inputs.

The main method I was trying was by simply adding the .txt file to the project as an asset to make accessing easier, but it's simply not doing that. the new method is using a picker to pick the file.

    public sealed partial class MainPage : Page
    {
        internal struct Information
        {
            internal string Name;
            internal string Phone;
            internal string Pager;
            internal string Cell;
            internal string Voicemail;
            internal string Email;
        }

        private Information[] Info = new Information[4];

        public MainPage()
        {
            this.InitializeComponent();

            Prepare_Array();
        }
        private async void Prepare_Array()
        { 
            try
            {
                                FileOpenPicker openPicker = new FileOpenPicker();

                openPicker.ViewMode = PickerViewMode.List;

                openPicker.SuggestedStartLocation = PickerLocationId.ComputerFolder;

                openPicker.FileTypeFilter.Add(".txt");

                StorageFile file = await openPicker.PickSingleFileAsync();

                if (file != null)
                {
                    Info_Listbox.Items.Add(Info[1].Name);
                }
            }

            catch { }
        }

Text File I'm trying to use.

Name: Joker Phone Number: 888-888-8888 Pager Number: 90009 Cellphone Number: 555-555-5555 Voicemail Number: 333-333-3333 Email Address: Joke@Joker.Net

Name: Johnny Phone Number 123-456-7890 Pager Number: 19991 Cellphone Number: 098-765-4321 Voicemail Number: 567-843-1209 Email Address: Johnny@Joker.Net

I expected the listbox to have something (even an entire entry was acceptable, I could work from there). The actual result was Windows.Foundation.Collections.IVector'1

try this

some declarations (for context) :

struct Information
{
    string Name;
    string Phone;
    string Pager;
    string Cell;
    string Voicemail;
    string Email;
}

public Information[] info = new Information[4];

some other declarations (inside that structure loading method) :

OpenFileDialog openDialog = new OpenFileDialog();
string path;
string line;
string[] attributes = new string[6];

and now the code

openDialog.Title = "Select A File";
openDialog.Filter = "Text Files (*.txt)|*.txt" + "|" + 
                    "All Files (*.*)|*.*";

if (openDialog.ShowDialog() == DialogResult.OK)
{
    for(int i = 0; i < info.Length; i++)
    {
        path = openDialog.FileName;

        line = File.ReadLine(path);
        attributes = line.Remove("Name: ").Remove("Phone Number ").Remove("Pager Number: ")
            .Remove("Cellphone Number: ").Remove("Voicemail Number: ").Remove("Email Address: ").Split(' ');

        info[i].Name = attributes[0];
        info[i].Phone = attributes[1];
        info[i].Pager = attributes[2];
        info[i].Cell = attributes[3];
        info[i].Voicemail = attributes[4];
        info[i].Email = attributes[5];
    }
    foreach(Information inf in info)
    {
        Info_Listbox.Items.Add(inf.Name);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM