I am trying to make an item (or a string) in a listbox appear in 3 different textboxes. The string gets added to the listbox from user input. So far I've managed to get the three textboxes to display the entire string in every textbox. Although I want them to display different things. So I split the user input string into an array with three values but I can't seem to get it to display the relevant info in the three textboxes. I should also mention, the textboxes should only display their values whenever the listbox item is selected.
private void firstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
//firstContacts is the listbox
//txtFirstName, txtLastName and txtPhoneNumber are the 3 different textboxes
if (firstContacts.SelectedItem != null)
{
txtFirstName.Text = firstContacts.GetItemText(firstContacts.SelectedItem);
txtLastName.Text = firstContacts.SelectedItem.ToString();
txtPhoneNumber.Text = firstContacts.SelectedItem.ToString();
}
private void btnSaveContact_Click(object sender, EventArgs e)
{
if (this.txtNewContact.Text != "")
{
try
{
string[] delimiters = new string[] { "," };
string[] stringArray = txtNewContact.Text.Split(delimiters, StringSplitOptions.None);
Contact person = new Contact(stringArray[0], stringArray[1], stringArray[2]);
firstContacts.Items.Add(person);
class Contact
{
public Contact(string firstName, string lastName, string phoneNumber) //Constructor
{
this.FirstName = firstName;
this.LastName = lastName;
this.PhoneNumber = phoneNumber;
}
public override string ToString()
{
return LastName.TrimStart(' ') + " " + FirstName + PhoneNumber;
}
//Properties
public string FirstName { get; private set; }
public string LastName { get; private set; }
public string PhoneNumber { get; private set; }
}
I think you need to deal with string methods. The code below should help you to get each textbox the vlaue it need.
string myString = "FirstName$Surname$PhoneNumber"; // listBox selected string value
string myFirstName = myString.Substring(0, myString.IndexOf("$")); // YOU CAN REPLACE THE $ CHARECTER WITH WIGHT SPACE ONE.
myString = myString.Substring(myString.IndexOf("$") + 1);
string mySurname = myString.Substring(0, myString.IndexOf("$"));
myString = myString.Substring(myString.IndexOf("$") + 1);
string myPhoneNum = myString;
Console.WriteLine("myFirstName:" + myFirstName);
Console.WriteLine("mySurname:" + mySurname);
Console.WriteLine("myPhoneNum:" + myPhoneNum);
So, assuming that the different things are separated by a space (eg "firstname lastname 12345678") then this should work.
private void firstContacts_SelectedIndexChanged(object sender, EventArgs e)
{
if (firstContacts.Focused)
{
string[] words = firstContacts.SelectedItem.ToString().Split(" ".ToCharArray());
if (words.Length != 3)
{
throw new Exception("Wrong format");
}
txtFirstName.Text = words[0];
txtLastName.Text = words[1];
txtPhoneNumber.Text = words[2];
}
And to set the TextBoxes to blank when an item is deselected.
private void firstContacts_Unselected(object sender, EventArgs e)
{
txtFirstName.Text = "";
txtLastName.Text = "";
txtPhoneNumber.Text = "";
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.