How to send textfile string to textbox using listbox?

Manny

The Windows Form application project I'm working on requires me to fill 4 textboxes with values in a text file. In the textfile, every line contains a word for each textbox, separated by a space. (For example, the first line could say "cat fish dog horse" and the second line could say "a b c d")

The listbox contains the first word of every line. (Running with the same example, the list box will contain "cat" and "a".)

So I'll double click on a value in the listbox, and run a search in the textfile using streamreader, select the line that contains the selected item, place it in a string array, split it into 4 elements based on the spacing, and place them into the 4 respective textboxes.

It doesn't work right yet though, any suggestions?

       private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)//list double click
    {
        AccountBox.Clear();
        EmailBox.Clear();
        UserBox.Clear();
        PassBox.Clear(); //to reset boxes

        string accountName = listBox1.GetItemText(listBox1.SelectedItem);
        AccountBox.Text = accountName;

        System.IO.StreamReader account = new System.IO.StreamReader("record.txt");

        var lineCount = File.ReadLines("record.txt").Count(); 
        int lines = Convert.ToInt32(lineCount);
        for (int i = 0; i < lines; i++)

       {
        if (account.ReadLine().Contains(AccountBox.Text))
            {
                string[] words;

                words = account.ReadLine().Split(' ');

                AccountBox.Text = words[0];
                EmailBox.Text = words[1];
                UserBox.Text = words[2];
                PassBox.Text = words[3]; 
            }
            else
            {
                break;
            }
        }
Ashkan Mobayen Khiabani

As you are using file as accounting so I assume that there must not be much records in it, So you can easily read all records at once and compare them in memory which will be much faster and easier:

string accountName = listBox1.GetItemText(listBox1.SelectedItem);
AccountBox.Text = accountName;
string[] lines = File.ReadAllLines("record.txt");
string account = lines.Where(l=>l.Split(' ')[0]==accountName).FirstOrDefault();

if(account!=null)
{
    string[] words = account.Split(' ');
    AccountBox.Text = words[0];
    EmailBox.Text = words[1];
    UserBox.Text = words[2];
    PassBox.Text = words[3]; 
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to find items in listbox using Textbox( localdatabase )

From Dev

How to send date from a listbox to textbox of another form

From Dev

How to send date from a listbox to textbox of another form

From Dev

When using a button to choose a listbox item to show a string of text in a textbox

From Dev

how to set TextBox value as textfile name

From Dev

Send String from server to client - save it as textfile

From Dev

How to strip textfile using csv

From Dev

How to save multiple listbox value into single textfile C#

From Dev

How to save multiple listbox value into single textfile C#

From Dev

Filtering listbox using textbox, while the listbox is filled using File Names

From Dev

Extract a string from textfile using JS regex

From Dev

TextBox Content as a name for Textfile

From Dev

How to write/print a string to a new textfile in python?

From Dev

How do you add listbox data into a textBox on another webPage. Using Visual Studio 2015 C#

From Dev

How to dynamically position a listbox in relation to textbox in winforms

From Dev

how to use the listbox value to update textbox

From Dev

How to Split Textbox Text to Listbox C#

From Dev

How to Get Listbox item in textbox with mouse click?

From Dev

How can I automatically export csv and send it to a database or textfile

From Dev

How to send and receive string using MPI

From Dev

c# how to divide lines of a textfile in two equal parts, and show it in two different listbox

From Dev

Add the same string from textbox in two lines listbox?

From Dev

How to send email by selecting the name in ListBox

From Dev

How to get string from listbox

From Dev

How do I save a input String into a textfile with output messages?

From Dev

Using timer to send textbox lines to notepad

From Dev

apache spark textfile to a string

From Dev

Reading a textfile into a String

From Dev

using a textbox value as string in Access

Related Related

  1. 1

    How to find items in listbox using Textbox( localdatabase )

  2. 2

    How to send date from a listbox to textbox of another form

  3. 3

    How to send date from a listbox to textbox of another form

  4. 4

    When using a button to choose a listbox item to show a string of text in a textbox

  5. 5

    how to set TextBox value as textfile name

  6. 6

    Send String from server to client - save it as textfile

  7. 7

    How to strip textfile using csv

  8. 8

    How to save multiple listbox value into single textfile C#

  9. 9

    How to save multiple listbox value into single textfile C#

  10. 10

    Filtering listbox using textbox, while the listbox is filled using File Names

  11. 11

    Extract a string from textfile using JS regex

  12. 12

    TextBox Content as a name for Textfile

  13. 13

    How to write/print a string to a new textfile in python?

  14. 14

    How do you add listbox data into a textBox on another webPage. Using Visual Studio 2015 C#

  15. 15

    How to dynamically position a listbox in relation to textbox in winforms

  16. 16

    how to use the listbox value to update textbox

  17. 17

    How to Split Textbox Text to Listbox C#

  18. 18

    How to Get Listbox item in textbox with mouse click?

  19. 19

    How can I automatically export csv and send it to a database or textfile

  20. 20

    How to send and receive string using MPI

  21. 21

    c# how to divide lines of a textfile in two equal parts, and show it in two different listbox

  22. 22

    Add the same string from textbox in two lines listbox?

  23. 23

    How to send email by selecting the name in ListBox

  24. 24

    How to get string from listbox

  25. 25

    How do I save a input String into a textfile with output messages?

  26. 26

    Using timer to send textbox lines to notepad

  27. 27

    apache spark textfile to a string

  28. 28

    Reading a textfile into a String

  29. 29

    using a textbox value as string in Access

HotTag

Archive