Converting list box Items to a String C# - c#

I'm writing a WPF application. How to Convert ListBox items to single string Value?
I have 1 Text Box, an ADD Button to add items to a List Box, and a SAVE Button to SAVE items in Console Window.
How can I do this?

//Concatenation of all items of list box
string allItems = string.Join(", ",listBox1.Items.OfType<object>());
//Concatenation of selected tiems of list box
string selectedItems= string.Join(", ",listBox1.SelectedItems.OfType<object>());

var listboxitems = listbox1.Items.Cast<ListBoxItem>().Select(p => p.Content as string);
string result= String.Join("|", listboxitems .ToArray());
This will place
value1|value2|value3

On which ever Button Click you want to Convert that Items to single String value Add this piece of code :
Stringbuilder sb =new StringBuilder();
private void btn1_Click(object sender, RoutedEventArgs e)
{
//Suppose that ListBox name is listBox1
for(int i=0;listBox1.Items.Count; i++)
{
sb.Append(listBox1.Items[i].ToString());
}
}
use this Stringbuilder sb.ToString() to Your Text box
txtname.text =sb.ToString();

ListBox listBox1 = new ListBox();
for (int x = 1; x <= 5; x++)
{
listBox1.Items.Add("Item " + x.ToString());
}
StringBuilder sb = new StringBuilder();
foreach (var item in listBox1.Items)
{
sb.Append(item.ToString());
}
Console.WriteLine(sb.ToString());

You can simply try:
string[] listAsArray = listBoxPart.Items.OfType<string>().ToArray();

Add a new string content from your TextBox:
Assume that you have a TextBox name textBox,
You can get string content of the textBox by Text property.
You have to create 2 Button (s).
Button ADD: To add the string from your TextBox to your ListBox
Button SAVE: To save the ListBox items to a Console Window
See the code:
ListBoxItem item1 = new ListBoxItem();
item1.Content = textBox.Text;
listBox.Items.Add(item1);
// Do the same with item2, item 3,... item n
Show in a Console Window:
foreach (var item in listBox.Items)
{
Console.WriteLine(item);
}

Related

Why when adding items to combobox it's adding the same item many times?

In the top of form1:
public class ComboboxItem
{
public string Text { get; set; }
public object Value { get; set; }
public override string ToString()
{
return Text;
}
}
List<string> results = new List<string>();
Then:
ComboboxItem item = new ComboboxItem();
var result = videoCatagories.Execute();
for (int i = 0; i < result.Items.Count; i++)
{
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
comboBox1.Items.Add(item);
}
And in the end:
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
MessageBox.Show((comboBox1.SelectedItem as ComboboxItem).Value.ToString());
}
What i wanted to do in general is to add to the combobox the titles and then when i select a title to get the title id.
For example i run the program and select the title Weather now i want to see in a messageBow.Show the id 1
There are 31 items.
When i use a breakpoint and look on result i see 31 items when i click on the first item in index 0 i see Id = "1" and then i click on snippet and see title "weather"
Then i do the same for item in index 1 and i see Id = "19" and in the snippet the title is "animals".
But for some reason it's adding each itertion the same item many times.
Create a new instance of ComboboxItem each time you want to add a new item to the combo box:
for (int i = 0; i < result.Items.Count; i++)
{
ComboboxItem item = new ComboboxItem();
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
comboBox1.Items.Add(item);
}
Your code changes the properties of the same item instance for each entry in results, then adds it to the comboBox1.Items collection. Add inserts its argument to the Items collection, it doesn't copy its contents. As a result, when the combobox is rendered, all combobox items point to the same item. To avoid this, create a new item instance for each entry in results:
for (int i = 0; i < result.Items.Count; i++)
{
var item=new ComboboxItem
{
Text = result.Items[i].Snippet.Title,
Value = result.Items[i].Id
};
comboBox1.Items.Add(item);
}
or
var items=from item in result
select new ComboboxItem
{
Text = item.Snippet.Title,
Value = item.Id
};
comboBox1.Items.AddRange(items);
You could do a simple check to make sure that the combobox doesn't already contain it before doing the insert.
ComboboxItem item = new ComboboxItem();
var result = videoCatagories.Execute();
for (int i = 0; i < result.Items.Count - 1; i++)
{
if(!comboBox1.Items.Contains(item))
{
item.Text = result.Items[i].Snippet.Title;
item.Value = result.Items[i].Id;
comboBox1.Items.Add(item);
}
}
Or you can do like this article suggests and remove every item that is the same before adding the new item to remove conflicts, No Duplicate in a Listbox or using the same way to stop duplicates from a combobox too

Change text in multiple textboxes?

How would I go about making a for loop that changes the text in more than 1 textbox?
for (int i; i < 5; i++)
{
textbox(i).text = "something"
}
But I don't know how to get the I to represent the number after the textbox, does anyone know how to?
Store the textboxes in an Array and then loop over the array
for (int i; i < 5; i++)
{
textboxArray[i].text = "something"
}
You could use Controls.Find:
var txts = this.Controls.Find("textbox" + i, true); // true for recursive search
foreach(TextBox txt in txts)
txt.Text = "something";
or - if the TextBoxes are in the same container control(like the Form or a Panel)- with LINQ:
var txts = this.Controls.OfType<TextBox>().Where(txt => txt.Name == "textbox" + i);
foreach(TextBox txt in txts)
txt.Text = "something";
Actually you don't need the loop variable, you could also use String.StartsWith to get all:
var txts = this.Controls.OfType<TextBox>().Where(txt => txt.Name.StartsWith("textbox"));
foreach(TextBox txt in txts)
txt.Text = "something";
if you dont want to alter every textbox on your form just simply add them to a List:
List<TextBox> TextBoxes = new List<TextBox>();
TextBoxes.Add(This.TextBox1);
TextBoxes.Add(This.TextBox3):
then as others have suggested you could either linq or regular foreach the textboxes in the list
TextBoxes.Foreach(Textbox => TextBox.Text = "something");
or
foreach (TextBox r in TextBoxes)
{
r.Text = "something;
}

From Listbox item to a textbox with duplicate items

I have 1 listbox and 2 buttons and 2 textboxes. When i press a button my listbox shows + all my data.
In the listbox i have around 30 names. With different names in it. Some names are the same.
And when i press the other button so shall in one textbox (that i already done). Show a name that i selected in the list. But! Here comes the tricky part.
I want to have in the last textbox the name from a listbox that can calculate all names that are in the same name.
If i selected "Peter" in the list and Peter is 3-4 times in that list. How can i write it out so it displays 3 times? I mean 3 (int) times.
And here is the Code:
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
_items.Add("Per Lindmark, 2012-05-01");
// add more items
_items.Add("Elin Ivarsson, 2012-05-13");
listBox1.DataSource = _items;
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = listBox1.SelectedItem.ToString();
}
}
}
Here is one solution.
var name = txtBox1.Text.Split(' ')[0];
if (string.IsNullOrEmpty(name))
{
return;
}
var items = listBox1.DataSource as List<string>;
var count = items.Count(x => x.Split(' ')[0] == name);
textBox2.Text = count.ToString();
I hope I understood your question :)
You could iterate over all items in the list and compare the names:
int counter = 0;
foreach (string item in listBox1.Items)
{
if (item == name)
counter++;
}
Of course, you'd have to extract the name from the item in the list properly.

Get text of selected items in a ListBox

I'm trying to show the selected items of listBox1 in a Message Box here's the code:
int index;
string item;
foreach (int i in listBox1 .SelectedIndices )
{
index = listBox1.SelectedIndex;
item = listBox1.Items[index].ToString ();
groupids = item;
MessageBox.Show(groupids);
}
The problem is that when I select more than one item
the message box shows the frist one I've selected and repeats the message
EX: if I selected 3 items the message will appear 3 times with the first item
You can iterate through your items like so:
foreach (var item in listBox1.SelectedItems)
{
MessageBox.Show(item.ToString());
}
The i in the foreach loop has the index you need. You're using listBox1.SelectedIndex which only has the first one. So item should be:
item = listBox1.Items[i].ToString ();
How about 1 message box with all the selected items?
List<string> selectedList = new List<string>();
foreach (var item in listBox1.SelectedItems) {
selectedList.Add(item.ToString());
}
if (selectedList.Count() == 0) { return; }
MessageBox.Show("Selected Items: " + Environment.NewLine +
string.Join(Environment.NewLine, selectedList));
If any are selected, this should give you a line for each selected item in your message box. There's probably a prettier way to do this with linq but you didn't specify .NET version.
Try this solution:
string item = "";
foreach (int i in listBox1.SelectedIndices )
{
item += listBox1.Items[i] + Environment.NewLine;
}
MessageBox.Show(item);

Retrieve items from a combo box

Currently I have a comboBox that reads information from outlook, and stores a list as values that can be selected for the comboBox.
I would like to be able to set the text of a button to the values stored in this comboBox.
I have an array of buttons that stores the buttons to be changed. Below is the code, so that on a click, the values from the comboBox will be displayed as the text labels in the buttons, the ?? is where I am stuck.
private void Mmaptsks_Click(object sender, EventArgs e)
{
int count = cmb.Items.Count;
for (int i = 0; i < count; i++)
{
buttonArray[i].Visible = true;
buttonArray[i].Text = ??;
}
}
Thanks,
Tom
You can try:
buttonArray[i].Text = cmb.Items[i].ToString();
Or if your combo items are not string then you can:
buttonArray[i].Text = (cmb.Items[i] as YourType).SomeProperty;

Categories

Resources