简体   繁体   English

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

[英]How can I access an object attribute in a form in c#?

I added 3 items to the comboBox in the form, using the Items property. 我使用Items属性将3个项目添加到窗体的comboBox中。 These items are: Item1, Item2, Item3. 这些项目是:Item1,Item2,Item3。

When I select any of these 3 items in the comboBox, I want it to show a messagebox that contains the value of the first attribute of the corresponding object. 当我在comboBox中选择这3个项目中的任何一个时,我希望它显示一个消息框,其中包含相应对象的第一个属性的值。 For example when i click Item1, I want it to show me attribute "CNP1" from object a1, when I click Item2, to show me attribute CNP2 from object a2 and so on. 例如,当我单击Item1时,我希望它向我显示对象a1的属性“ CNP1”,当我单击Item2时,我向我显示对象a2的属性CNP2,依此类推。

I think that I might connect each item in the comboBox with one of the 3 object created, not just write down these names(Item1,Item2,Item3) but I don't know how. 我想我可能会用创建的3个对象之一连接comboBox中的每个项目,而不仅仅是写下这些名称(Item1,Item2,Item3),但我不知道如何。

Also, these 3 items are created due to a class I created in the same project. 另外,由于我在同一项目中创建的类而创建了这3个项目。 I only have a class, a form and the main Program in this project. 在这个项目中,我只有一个类,一个窗体和主程序。

So, how can I connect a comboBox Item to one of these objects, especially with only one attribute of that object. 因此,如何将comboBox项连接到这些对象之一,尤其是仅使用该对象的一个​​属性。 Thank you. 谢谢。

   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());
    }
}

} }

Assuming that your code compiles and shows the o.ToString() when the selected index changes you would want to switch from using an ArrayList to a generic List, in your case a List which will allow you to access the properties of your entities without the need to casting in the event handler. 假设您的代码在选定索引更改时编译并显示o.ToString(),则您希望从使用ArrayList切换为通用List,在这种情况下,您可以使用List来访问实体的属性,而无需使用需要强制转换事件处理程序。 Here's the relevant portion of your code: 这是代码的相关部分:

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

You can use SelectedIndex to get AbonatTelefonic . 您可以使用SelectedIndex获取AbonatTelefonic I hope it will help you. 希望对您有帮助。

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

Add public override string ToString() on your AbonatTelefonic class and add the code return {first attrib variable}; 在您的AbonatTelefonic类上添加public override string ToString()并添加代码return {first attrib variable};

Reference for you: https://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx 适用于您的参考: https : //msdn.microsoft.com/zh-cn/library/ms173154(v=vs.80).aspx

Hope this helps. 希望这可以帮助。

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

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