访问列表中的元素<>结构

时间:2011-12-06 15:18:59

标签: c# list binding combobox struct

一个小背景,这开始于问题:Custom data type (Structures) vs arrays

我设法创建了struct State,并List<State>继续创建它。最初这是一个多维数组,其目的是将0索引作为组合框中的项目,然后在3个文本框中显示1-3。我现在的问题是,如何使用struct / List组合复制此功能。该结构包含与原始Array相同的4个字段,但是我在理解如何指定哪个字段进入哪个框时遇到了问题。从我在其他地方读到的内容来看,应该只是使用struct.field格式,但似乎没有用。像往常一样,这可能是我想念的简单事情,但我似乎无法绕过它。

另一方面,这个列表是完全不可变的,这些信息都不会改变/改变(如果是,它将在代码级别,我会自己重做,但用户进入不应该影响它。)

非常感谢任何帮助。

STRUCT:

struct State
{
    private String StateID;
    private String TimeZone;
    private String CureType;
    private int CureTimeframe;

    public State(string stateID, string timeZone, string cureType, int cureTimeframe)
    {
        StateID = stateID;
        TimeZone = timeZone;
        CureType = cureType;
        CureTimeframe = cureTimeframe;
    }

    public override string ToString()
    {
        string data = String.Format("{0} {1} {2} {3}", StateID, TimeZone, CureType, CureTimeframe);
        return data;
    }
}

列表&LT;&GT;初始化:

class StatesAndTerritories
{
    public List<State> stateData = new List<State>();
    public StatesAndTerritories ()
    {
        stateData.Add(new State("AL", "CST", "None", 0));
        stateData.Add(new State("AK", "AST", "COD", 10));
        stateData.Add(new State("AR", "CST", "COD", 10));
        stateData.Add(new State("AZ", "MST", "COD", 10));
        stateData.Add(new State("CA", "PST", "COD/Mandatory", 10));
        stateData.Add(new State("CO", "MST", "Mandatory", 20));
        stateData.Add(new State("CT", "EST", "Mandatory", 10));
        stateData.Add(new State("DC", "EST", "Mandatory", 10));
        stateData.Add(new State("DE", "EST", "None", 0));
        stateData.Add(new State("FL", "EST", "COD", 10));
        stateData.Add(new State("GA", "EST", "COD", 10));
        stateData.Add(new State("HI", "HST", "None", 0));
        stateData.Add(new State("IA", "CST", "Mandatory", 20));
        stateData.Add(new State("ID", "MST", "None", 0));
        stateData.Add(new State("IL", "CST", "COD", 10));
        stateData.Add(new State("IN", "EST", "None", 0)); 
        stateData.Add(new State("KS", "CST", "Mandatory", 20));
        stateData.Add(new State("KY", "EST", "None", 0));
        stateData.Add(new State("LA", "CST", "Replevin", 0));
        stateData.Add(new State("MA", "EST", "Mandatory", 21));
        stateData.Add(new State("MD", "EST", "Mandatory", 10));
        stateData.Add(new State("ME", "EST", "Mandatory", 19));
        stateData.Add(new State("MI", "EST", "COD/Mandatory", 30));
        stateData.Add(new State("MN", "CST", "COD", 10));
        stateData.Add(new State("MO", "CST", "Mandatory", 20));
        stateData.Add(new State("MS", "CST", "None", 0)); 
        stateData.Add(new State("MT", "MST", "COD", 10)); 
        stateData.Add(new State("NC", "EST", "None", 0));
        stateData.Add(new State("ND", "CST", "None", 0));
        stateData.Add(new State("NE", "CST", "Mandatory", 20));
        stateData.Add(new State("NH", "EST", "None", 0));
        stateData.Add(new State("NJ", "EST", "COD", 10)); 
        stateData.Add(new State("NM", "MST", "COD", 10));
        stateData.Add(new State("NV", "PST", "COD", 10));
        stateData.Add(new State("NY", "EST", "None", 0));
        stateData.Add(new State("OH", "EST", "COD", 10)); 
        stateData.Add(new State("OK", "CST", "COD", 10));
        stateData.Add(new State("OR", "PST", "COD", 10));
        stateData.Add(new State("PA", "EST", "None", 0));
        stateData.Add(new State("RI", "EST", "Mandatory", 24));
        stateData.Add(new State("SC", "EST", "Mandatory", 20));
        stateData.Add(new State("SD", "CST", "COD", 10));
        stateData.Add(new State("TN", "CST", "COD", 10));
        stateData.Add(new State("TX", "CST", "Mandatory",10));
        stateData.Add(new State("UT", "MST", "None", 0));
        stateData.Add(new State("VA", "EST", "None", 0));
        stateData.Add(new State("VT", "EST", "None", 0));
        stateData.Add(new State("WA", "PST", "COD", 10)); 
        stateData.Add(new State("WI", "CST", "Mandatory", 15));
        stateData.Add(new State("WV", "EST", "Mandatory", 10));
        stateData.Add(new State("WY", "MST", "None", 0));
    }

    public IEnumerable<State> GetStates (){
        return stateData;
    }
    public IEnumerable<State> GetStatesInTimeZone(string timezone)
    {
        return stateData;
    }
    public IEnumerable<State> GetStatesCureType(string curetype){
        return stateData;
    }
    public IEnumerable<State> GetStatesCureTimeframe(int curetimeframe){
        return stateData;
    }        
}

Combobox人口:

    public void PopulateStateInfo()
    {
        StatesAndTerritories states = new StatesAndTerritories();

        foreach (State state in states.stateData)
        {
            //string s = stateData[i, 0];
            stateInfoBox.Items.Add(states.stateData);
        }
    }

ComboBox选择:

    private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        StatesAndTerritories states = new StatesAndTerritories();
        try
        {
            stateTimeZone.Text = states.stateData.ToString();
            stateCureType.Text = states.stateData.ToString();
            stateCureTimeframe.Text = states.stateData.ToString();
            if (stateCureTimeframe.Text != 0.ToString())
            {
                stateCureAtDPD.Text = ((55 - int.Parse(stateCureTimeframe.Text)).ToString());
            }
        }
        catch (IndexOutOfRangeException)
        {
            stateTimeZone.Text = "";
            stateCureType.Text = "";
            stateCureTimeframe.Text = "";
            stateCureAtDPD.Text = "";
        }
    }

3 个答案:

答案 0 :(得分:2)

您已将struct字段定义为private,因此您无法在另一个类中引用它们。您需要做的就是定义一个公共只读属性来公开每个字段。

答案 1 :(得分:1)

首先,你无法达到结构的内容,

private String cvStateID;
private String cvTimeZone;
private String cvCureType;
private int cvCureTimeframe;

这些是私有的,因此无法从结构外部访问。 您需要为它们定义公共只读属性:

public readonly string StateID { get { return cvStateID; } }
public readonly string TimeZone { get { return cvTimeZone; } }
public readonly string CureType { get { return cvCureType; } }
public readonly int CureTimeframe { get { return cvCureTimeframe; } }

接下来,我认为您只想在组合框中显示StateID。如果是这样,您需要相应地编写ToString():

public override string ToString()
{
    return StateID.ToString();
}

PopulateStateInfo方法中,您不是要添加列表中的每个项目,而是添加不寻常的整个集合!正确的实施方式如下:

public void PopulateStateInfo()
{
    StatesAndTerritories states = new StatesAndTerritories();

    foreach (State state in states.GetStates())
    {
        stateInfoBox.Items.Add(state);
    }
}

最后,处理SelectedIndexChanged事件的正确方法如下:

private void stateInfoBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    State selectedState = (State)stateInfoBox.SelectedItem;
    stateTimeZone.Text = selectedState.TimeZone;
    StateCureType.Text = selectedState.CureType;
    stateCureTimeframe.Text = selectedState.CureTimeframe.ToString();
}

我想要的最后一点是,请不要将StatesAndTerritories类的stateData元素设为public。如果它只能从课堂内部变化,你将从中受益。

答案 2 :(得分:0)

在表单上放置一个新的BindingSource。然后,您可以将List<State>绑定到绑定源。使用DataSource成员将数据绑定到控件。这可以在设计器中完成,并替换循环以填充下拉控件。文本框自动显示单个项目(绑定源的Current),而组合框可以绑定到整个列表。