0

How can I pass a ListBox content to a string (array of strings?). I need to get a similar format of data:

Value1
Value2
Value3

Where each Value is a separate ListBox item.

EDITED: I wasn't too accurate with my question. I want to send desired list of values as an e-mail through my WPF application, unfortunately values are sent in one line. Even use of /n is not helping. What can I do to change appearance to list instead of string?

4 Answers 4

1

Simple one line solution:

List<string> items = ListBox1.Items.Cast<ListBoxItem>().Select(p=> p.Content as string).ToList();
Sign up to request clarification or add additional context in comments.

Comments

1

You can use a single string value instead of an array to store the result as follows:

string emailData=string.Empty;
//get each item's text & append emailData.
foreach(var item in listBox1.Items)
 {
     string itemData=((ContentControl)item).Content.ToString();
     emailData = emailData +itemData+"\n";
 }
//Call the method that writes the data to email, remember to use Trim() while using emailData.
WriteToEmail(emailData.Trim());

Comments

0
public string[] get()
{
    string[] arr = new string[listBox1.Items.Count];
    for (int i = 0; i < listBox1.Items.Count; i++)
    {
        arr[i] = listBox1.Items[i].ToString();
    }
    return arr;
}

Comments

0
//lb is the Listbox; array is a string[]
lb.ItemsSource= array;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.