Hi & thanks in advance for any help given.
I'm AN ABSOLUTE beginner at programming and am finding it hard to find info on how to make a ComboBox list only display the first part of an externally located, comma delimited text file.
The external text file is used as a sort of "phone-book" and the program uses the entries to create the items in the ComboBox list.
For example, the external file contains:
Andy,[email protected]
Brian,[email protected]
The program needs the email address from each entry but the person using the program doesn't need to know it, just who they're selecting.
The current code displays the above info as it looks above, but I want to find a way to not display anything from the comma onwards, but still hold the phone number as part of the list, as the program needs it to work.
Anyway, here's what's currently doing:
{
StreamReader reader = new StreamReader(System.Environment.CurrentDirectory + \\Email.txt);
do
{
cmbRecipient.Items.Add(reader.ReadLine());
}
while (reader.Peek() != -1);
cmbRecipient.SelectedIndex = 0;
//Trying to stop email from displaying in Drop Down List
//cmbRecipient.DisplayMember = "";
}
Currently, the drop-down list displays everything, eg:
Andy,[email protected]
Brian,[email protected]
But, all I want to display in the drop-down list is the first names, eg:
Andy
Brian
But I need a way to have all the information still available to the ComboBox list, as it's used by this code to pass on to the messenger service:
string[] splitRecipient = cmbRecipient.Text.Split(',');
string EMAIL = splitRecipient[splitRecipient.Length - 1].Trim();
Sorry for the lenghty post, just wanted to give all the relevant info I had, to try and make sense.
Thanks again
Loading
AnkurPosted Sep 6, 2010, 1:43 AM
i think following might help:
DataTable table = new DataTable();
DataColumn colName = new DataColumn("Name");
DataColumn colEmailID = new DataColumn("Email");
table.Columns.Add(colName);
table.Columns.Add(colEmailID);
StreamReader reader = new StreamReader(System.Environment.CurrentDirectory + \\Email.txt);
do
{
table.Rows.Add(reader.ReadLine().Split(','));
}
while (reader.Peek() != -1);
cmbRecipient.DataSource = table;
cmbRecipient.DisplayMember = "Name";
cmbRecipient.ValueMember = "Email";
cmbRecipient.SelectedIndex = 0;
Posted Sep 6, 2010, 9:37 AM
thanks for all your help, if you're a newby like me, you'll probably be interested to know the answer to this question.
I posed a separate question in the C# forum (as it appears I posted this one in the wrong place, SORRY ABOUT THAT), please see:
http://www.c-sharpcorner.com/Forums/UploadFile/94039/
But quickly, the answer was:
string EmailAdd = Convert.ToString(cmbRecipient.SelectedValue);
This worked a treat. Many thanks to Manikavelu V. for that answer, I REALLY appreciated it (and all the help I got from everyone).
Hope this helps someone else as well.
Cheers
Andy
Posted Sep 6, 2010, 4:50 AM
that works very well at hiding the unwanted email & written in such a way that I'm pretty sure I even understand it as well.
However, I cannot find a way of utilising the string of "cmbRecipient.ValueMember" to send the message.
The only thing it seems to care about is what is displayed in the drop-down list box. I've changed the columns around to check this and it's true.
If I change the columns around to display the email, it works fine, but when I change it back to display the Recipient name, it trys to send an email to "Andy", not [email protected].
I've tried heaps and heaps of different ways to force it to pass the "cmbRecipient.ValueMember" but it just keeps taking the "cmbRecipient.DisplayMember" instead.
Thanks again, you've been a GREAT help.
Andy