将列表框项目放入多个文本框中

时间:2015-11-21 21:18:41

标签: c# listbox

我试图让列表框中的项目(或字符串)出现在3个不同的文本框中。该字符串将从用户输入添加到列表框中。到目前为止,我设法让三个文本框在每个文本框中显示整个字符串。虽然我希望他们展示不同的东西。因此,我将用户输入字符串拆分为具有三个值的数组,但我似乎无法在三个文本框中显示相关信息。我还要提一下,只要选择了列表框项,文本框就应该只显示它​​们的值。

 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; }
    }

2 个答案:

答案 0 :(得分:0)

我认为你需要处理字符串方法。下面的代码可以帮助您获得所需的每个文本框。

        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);

答案 1 :(得分:0)

因此,假设不同的东西被空格分隔(例如“firstname lastname 12345678”),那么这应该有效。

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];
    }

在取消选择项目时将TextBoxes设置为空白。

private void firstContacts_Unselected(object sender, EventArgs e)
{
    txtFirstName.Text = "";
    txtLastName.Text = "";
    txtPhoneNumber.Text = "";
}