【问题标题】:How can I access an object attribute in a form in c#?如何在 c# 中访问表单中的对象属性?
【发布时间】:2017-06-18 20:24:57
【问题描述】:

我使用 Items 属性向表单中的组合框添加了 3 个项目。这些项目是:项目 1、项目 2、项目 3。

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

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

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

那么,我怎样才能将组合框项连接到这些对象之一,尤其是只有该对象的一个​​属性。谢谢。

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

}

【问题讨论】:

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

标签: c# winforms object


【解决方案1】:

假设您的代码在所选索引更改时编译并显示 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);
}

【讨论】:

    【解决方案2】:

    您可以使用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);
        }
    }
    

    【讨论】:

      【解决方案3】:

      在您的 AbonatTelefonic 类中添加 public override string ToString() 并添加代码 return {first attrib variable};

      给你的参考:https://msdn.microsoft.com/en-us/library/ms173154(v=vs.80).aspx

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-11-22
        • 1970-01-01
        • 2016-12-04
        • 1970-01-01
        • 2018-07-07
        相关资源
        最近更新 更多