Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在c中访问表单中的对象属性?_C#_Winforms_Object - Fatal编程技术网

C# 如何在c中访问表单中的对象属性?

C# 如何在c中访问表单中的对象属性?,c#,winforms,object,C#,Winforms,Object,我使用items属性向表单中的comboBox添加了3项。这些项目是:项目1、项目2、项目3 当我在组合框中选择这3项中的任何一项时,我希望它显示一个messagebox,其中包含相应对象的第一个属性的值。 例如,当我单击Item1时,我希望它显示来自对象a1的属性CNP1,当我单击Item2时,显示来自对象a2的属性CNP2,依此类推 我想我可能会将组合框中的每个项目与创建的3个对象中的一个连接起来,而不仅仅是写下这些名称Item1、Item2、Item3,但我不知道如何写 此外,这3个项目是

我使用items属性向表单中的comboBox添加了3项。这些项目是:项目1、项目2、项目3

当我在组合框中选择这3项中的任何一项时,我希望它显示一个messagebox,其中包含相应对象的第一个属性的值。 例如,当我单击Item1时,我希望它显示来自对象a1的属性CNP1,当我单击Item2时,显示来自对象a2的属性CNP2,依此类推

我想我可能会将组合框中的每个项目与创建的3个对象中的一个连接起来,而不仅仅是写下这些名称Item1、Item2、Item3,但我不知道如何写

此外,这3个项目是由于在同一项目中创建的类I而创建的。 我只有一个班,一个表格和这个项目的主程序

那么,如何将comboBox项连接到其中一个对象,尤其是仅使用该对象的一个属性。多谢各位

   using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IncercareEX2015
{
    public partial class PreluareDate : Form
    {
        ArrayList listaAbonati;
        AbonatTelefonic ab;

        public PreluareDate()
        {
            InitializeComponent();

            double[] vectMin = new double[4] { 12, 15, 50, 20 };
            AbonatTelefonic a1 = new AbonatTelefonic("CNP1", "Nume1", "Adresa1", "tel1", "tip1", vectMin);

            double[] vectMin3 = new double[2] { 100, 130 };
            AbonatTelefonic a3 = new AbonatTelefonic("CNP3", "Nume3", "Adresa3", "Tel3", "Tip3", vectMin3);

            double[] vectMin2 = new double[3] { 200, 80, 150 };
            AbonatTelefonic a2 = new AbonatTelefonic("CNP2", "Nume2", "Adresa2", "Tel2", "Tip2", vectMin2);

            ///GENERARE COLECTIE DE OBIECTE
            ArrayList listaAbonati = new ArrayList();
            listaAbonati.Add(a1);
            listaAbonati.Add(a3);
            listaAbonati.Add(a2);
            listaAbonati.Sort();

        }

    private void comboBox1_nume_SelectedIndexChanged(object sender, EventArgs e)
    {
        foreach (object o in listaAbonati)
            MessageBox.Show(o.ToString());
    }
}

}

假设您的代码在所选索引更改时编译并显示o.ToString,您可能希望从使用ArrayList切换到通用列表,在您的情况下,该列表将允许您访问实体的属性,而无需在事件处理程序中强制转换。以下是代码的相关部分:

List<AbonatTelefonic> listaAbonati;

public PreluareDate()
{
    ///GENERARE COLECTIE DE OBIECTE
    listaAbonati = new List<AbonatTelefonic>();
    listaAbonati.Add(a1);
    listaAbonati.Add(a3);
    listaAbonati.Add(a2);
    listaAbonati.Sort();

}

private void comboBox1_nume_SelectedIndexChanged(object sender, EventArgs e)
{
    foreach (AbonatTelefonic at in listaAbonati)
        MessageBox.Show(at.YourDesiredPropertyNameGoesHere);
}
您可以使用SelectedIndex获取AbonatTelefonic。我希望它能帮助你

private void comboBox1_nume_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1_nume.SelectedIndex != -1)
    {
        AbonatTelefonic at = (AbonatTelefonic)listaAbonati[comboBox1_nume.SelectedIndex];
        MessageBox.Show(at.YourAttribute);
    }
}
在AbonatTelefonic类上添加public override字符串ToString,并添加代码返回{first attrib variable}

你的参考资料:


希望这能有所帮助。

是否要将项目添加到组合框中?或者显示组合框中已有项目的属性?我使用组合框的items属性添加了3个项目,如下所示:Item1、Item2、Item3。我希望在单击Item1时显示对象的第一个属性a1@IsacBut如果通过设计器添加它们,则添加的是字符串而不是对象。还是在代码中添加了它们?