Click here to Skip to main content
15,891,513 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all

I have a listbox named listbox1 in which emails are collected.

now i want listbox1 item in textbox1 by selecting that item.

That i can do by
C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        textBox1.Text = listBox1.SelectedItem.ToString();
    }



Emails are in format of a@gmail.com b@gmail.com

I want the selected item in textbox without @gmail.com.

How to do that.

eg: the items collected in listbox are

ab@gmail.com bc@gmail.com gh@gmail.com

i want just ab or bc or gh by selecting.

thank you
Posted
Updated 1-May-14 5:44am
v3
Comments
[no name] 1-May-14 11:46am    
Okay so split the string on the @ and take the first element of the array.

you can do as below

C#
TextBox1.Text=listBox1.SelectedItem.ToString().Split('@')[0];
 
Share this answer
 
C#
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var email = listBox1.SelectedItem.ToString();
textBox4.Text =   email.Substring(0, email.IndexOf("@"));
        }


This worked for me.
 
Share this answer
 
This code for select item Fill in Textbox

C#
foreach (var item in listBox1.SelectedItems)
       {
           TextBox1.Text=item.Split('@');
       }
 
Share this answer
 
Comments
Member 10579673 1-May-14 12:07pm    
@MAHESH THIS GIVES THE FOLLOWING ERROR

'object' does not contain a definition for 'split' and no extension method 'Split' accepting a first argument of type 'object' could be found
CHill60 1-May-14 12:45pm    
Try this instead
foreach (string item in listBox1.SelectedItems)<br>
{<br>
this.textBox1.Text = item.Split('@')[0];<br>
}

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