Click here to Skip to main content
15,891,907 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a textbox in which text is separated by #

I want to transfer every splitted text in listbox with a button click

how to do that?
Posted

1 solution

Add this in your button click method:
C#
string[] parts = yourTextBox.Text.Split('#');
yourListBox.Items.AddRange(parts);

If you don't want that there can be empty elements in your listbox, try this:
C#
string[] parts = yourTextBox.Text.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
yourListBox.Items.AddRange(parts);

If you want to add 5 items on every button click (and only want 5 items in the listbox), try this:
C#
int i = 0;
void yourButton_Click(object sender, EventArgs e)
{
    string[] parts = yourTextBox.Text.Split(new char[] { '#' }, StringSplitOptions.RemoveEmptyEntries);
    string[] partsToAdd = parts.Skip(5 * i).Take(5).ToArray();
    yourListBox.Items.Clear();
    yourListBox.Items.AddRange(partsToAdd);
    i++;
}
 
Share this answer
 
v4
Comments
Member 10579673 6-Jul-14 9:41am    
this adds one empty item in listbox.any suggestions?
Thomas Daniels 6-Jul-14 9:46am    
I edited my answer.
Member 10579673 6-Jul-14 10:08am    
one more thing can you please tell me how to insert only 5 items in listbox same situation?when i click button only 5 items must be added to listbox and then when i click again next 5 items must be added.
Thomas Daniels 6-Jul-14 10:15am    
I added this to my answer.
Member 10579673 6-Jul-14 10:38am    
@ProgramFOX sir i want only 5 items in listbox.how to delete the previous 5 items when i click the button again?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900