简体   繁体   English

ComboBox2 获取 ComboBox1 函数中的字段

[英]ComboBox2 get fields in function of ComboBox1

I have to select one brand of moto.我必须选择一个品牌的moto。 If i select "KTM", i want to get Ktm's motos.如果我选择“KTM”,我想获得 Ktm 的 motos。 If i select "HVA", i want HVA's motos.如果我选择“HVA”,我想要 HVA 的 motos。 Etc ..等等 ..

I have a List of models with all models, and in function what i select, i want to add models by this brand and return this in my ComboBox2.我有一个包含所有模型的模型列表,在我选择的功能中,我想添加这个品牌的模型并在我的 ComboBox2 中返回它。

Modele.cs :模型.cs :

class Modele模型类

 {
        public string NomModele;

    public static List<Modele> lesModeles = new List<Modele>() {

        // Husqvarna
        new Modele() { NomModele = "TE"},
        new Modele() { NomModele = "FE"},

        // KTM
        new Modele() { NomModele = "EXC"},
        new Modele() { NomModele = "EXC-F"}
    };

    public Modele() { }

    public Modele(string NomModele)
    {
        this.NomModele = NomModele;
    }
}

Main.cs :主文件:

namespace SuiviEntretien
{
    public partial class SuiviEntretien : Form
    {
        public SuiviEntretien()
        {
            InitializeComponent();

        this.lesMarques.Items.AddRange(Marque.lesMarques.Select(x => x.NomMarque).ToArray());
        this.lesModeles.Items.AddRange(Modele.lesModeles.Select(x => x.NomModele).ToArray());
    }

    private void SuiviEntretien_Load(object sender, EventArgs e)
    {

    }

    private void SauvegarderMoto_Click(object sender, EventArgs e)
    {
        try
        {
            Moto maMoto = new Moto(
                maMarque.Text = lesMarques.SelectedItem.ToString(),
                monModele.Text = lesModeles.SelectedItem.ToString()
                );
            MessageBox.Show("Moto enregistrée avec succès !", "Information");
            tabControl1.SelectTab(MaMoto);
        }
        catch(Exception)
        {
            MessageBox.Show("Il manque des informations !", "Information");
        }
    }
}
}

Thanks for further help.感谢您的进一步帮助。

The following answer has been made with some assumptions, those being:以下答案是在一些假设下做出的,这些假设是:

-You have a ComboBox that contains values, when a value is selected another ComboBox needs to re-populate itself with a new list of data. -You have a ComboBox that contains values, when a value is selected another ComboBox needs to re-populate itself with a new list of data.

Depending on the scale of this problem I would recommend two solutions.根据这个问题的规模,我会推荐两种解决方案。 Move your data into a relational database and access it accordingly, then populate your first ComboBox as a list of all main keys.将您的数据移动到关系数据库中并相应地访问它,然后将您的第一个 ComboBox 填充为所有主键的列表。 (One to many methodology) then populate your second ComboBox according to the first ComboBox value. (一对多方法)然后根据第一个 ComboBox 值填充第二个 ComboBox。

Assuming you want to build your list dynamically and want to avoid a database then simply use functionality based on if the ComboBox changes.假设您想动态构建列表并希望避免使用数据库,那么只需使用基于 ComboBox 是否更改的功能即可。

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e) {
if (ComboBox1.Text == "KTM")
{
    // Populate ComboBox2 with KTM data.
} 
else
{
   // Populate ComboBox2 with some other data.    
}
}

This should help you out.这应该可以帮助你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM