Click here to Skip to main content
15,885,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi,

I am trying to take a string, seperate it into 3 parts and parse one of them into a combobox only if it has that value in it.

So...

The values are stored in an Enum and are displayed in the combobox. on selected index changed the first combobox's text is taken into 3 parts. FirstName, LastName and Position

FirstName and LastName are put into textboxes and I want the position to be selected automatically from a Combo Box

This is the code I am using to split the string:

C#
private void cboSelectEmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboSelectEmp.SelectedIndex >= 0)
            {
                SelectEmployeeInfo();
            }
            
        }


        private void SelectEmployeeInfo()
        {
            
            if (!string.IsNullOrWhiteSpace(cboSelectEmp.Text.Trim()))
            {
               string input = cboSelectEmp.Text.Trim();
                if (input.Split(' ').Length > 1)
                {
                    string formFirstNameValue = input.Split(' ')[0];
                    txtFirstName.Text = formFirstNameValue;
                    string formLastNameValue = input.Split(' ')[1].Replace(",", "");
                    txtLastName.Text = formLastNameValue;
                    string formPositionValue = input.Split('(')[0].Replace(")", "");
                   // cboPosition.SelectedIndex 
                }
                
            }
               
        }

How would I do this?
Any help would be greatly appreciated.
Posted
Comments
Maciej Los 16-Dec-13 16:19pm    
Please post sample data stored in combobox.
BillWoodruff 16-Dec-13 16:39pm    
Show the Enum and a sample of the Text in one of the ComboBox Items.

Here is full code. I added some comments, please read them and try to inderstand what's happening in the code:

C#
private void SelectEmployeeInfo()
{

    if (!string.IsNullOrEmpty(cboSelectEmp.Text.Trim()))
    {
        string input = cboSelectEmp.Text.Trim();

        //Assuming your combo box contains:
        // John Smith, (7)
        string[] splittedText = input.Split(',');
        //spilttedTex[0] contains "John Smith"
        //spilttedTex[1] contains "(1)"
        if (splittedText.Length > 1)
        {
            string[] firstAndLastName = splittedText[0].Split(' ');
            //firstAndLastName[0] contains "John"
            //firstAndLastName[1] contains "Smith"

            txtFirstName.Text = firstAndLastName[0];
            txtLastName.Text = firstAndLastName[1];


            //Now you have to deal with position in spilttedTex[1]
            //First remove ()
            string formPositionValue = splittedText[1].Substring(1, splittedText[1].Length - 2);

            //next, convert formPositionValue to int
            int position;
            if (int.TryParse(formPositionValue, out position))
                cboPosition.SelectedIndex = position;

        }

    }
 
Share this answer
 
Comments
Maciej Los 16-Dec-13 16:38pm    
Please, do not repost!

[EDIT]Thank you[/EDIT]
The enum with the values:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EmployeeTracker.DataTypes
{
    public enum EmployeePosition
    {
        Owner,
        Manager,
        SalesClerk,
        Stocker
    }
}


The input string will always be in the format of FirstName LastName, (Position)

EG - John Citizen, (Manager)

In the position comboBox the values will be one individual value per line

Owner
Manager
SalesClerk
Stocker
 
Share this answer
 
v2

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