使用Text解析ComboBox以获取其选定的索引C# [英] Parse ComboBox with Text to get its selected index C#

查看:94
本文介绍了使用Text解析ComboBox以获取其选定的索引C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我正在尝试取一个字符串,将其分成3个部分并将其中一个解析为组合框,只要它具有该值。



所以...



值存储在枚举中并显示在组合框中。在选定的索引上改变了第一个组合框的文本被分为3个部分。 FirstName,LastName和Position



FirstName和LastName被放入文本框中,我希望从组合框中自动选择位置



这是我用来拆分字符串的代码:



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:

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.

推荐答案

这是完整的代码。我添加了一些评论,请阅读它们并尝试理解代码中发生的事情:



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

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;

        }

    }


包含值的枚举:



The enum with the values:

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





输入字符串将始终采用FirstName LastName格式,(位置)



EG - John Citizen,(经理)



在位置组合框中,每行的值为一个单独的值



所有者

经理

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


这篇关于使用Text解析ComboBox以获取其选定的索引C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆