I can not assign a value within my Label from a List

1

Good morning, first of all I would like to say that I am a beginner and I am doubtful about assigning a value to the Text property of a Label from a list created with information obtained from a txt .

Follow the code:

namespace BetaTeste
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();
            List<Planta> plantas = new List<Planta>();

            using (StreamReader arquivo = File.OpenText(@"C:\Estados\Banco de Estados.txt"))
            {
                string linha;
                while ((linha = arquivo.ReadLine()) != null)
                {
                    var espaçoArquivo = linha.Split(';');

                    var planta = new Planta();
                    planta.Local = espaçoArquivo[0];
                    planta.Conexao = espaçoArquivo[1];

                    plantas.Add(planta);
                }

            }

            foreach (Planta result in plantas)
            {
                comboBox1.Items.Add(result.Local);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            FrmBase formb = new FrmBase();

            switch (comboBox1.SelectedItem.ToString().Trim())
            {
                case "CT":
                    formb.Show(); 

                    // aqui está a dúvida
                    // formb.lblLocal.Text = ;
                    break;
            }
        }
    }
}

--------------------------------------------------------------------------------

class Planta
{
    public string Local { get; set; }
    public string Conexao { get; set; }

    public void Pesquisa()
    {

    }
}

--------------------------------------------------------------------------------

public partial class FrmBase : Form
{
    public FrmBase()
    {
        InitializeComponent();
    }
}

The goal is to fill Label with the value of a property of class Planta , but I'm not sure how I can get information from classes listed in items of ComboBox .

The% w_that I want to assign the value is label (line in comment).

    
asked by anonymous 30.07.2018 / 15:27

1 answer

0

Here is your answer correctly structured and optimized:)

namespace BetaTeste
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        public void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.Items.Clear();

            using (StreamReader arquivo = File.OpenText(@"C:\Estados\Banco de Estados.txt"))
            {
                string linha;

                while ((linha = arquivo.ReadLine()) != null)
                {
                    var espaçoArquivo = linha.Split(';');

                    comboBox1.Items.Add(new Planta()
                    {
                        Local = espaçoArquivo[0]
                        Conexao = espaçoArquivo[1]
                    });                     
                }
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            FrmBase formb = new FrmBase();

            if(comboBox1.SelectedItem != null)
            {
                Planta planta = (Planta)comboBox1.SelectedItem;

                switch (planta.Local)
                {
                    case "CT":
                        formb.Show(); 
                        formb.lblLocal.Text = planta.Local;
                        break;
                }
            }
        }
    }
}

--------------------------------------------------------------------------------

class Planta
{
    public string Local { get; set; }
    public string Conexao { get; set; }

    public void Pesquisa() {}
}

--------------------------------------------------------------------------------

public partial class FrmBase : Form
{
    public FrmBase()
    {
        InitializeComponent();
    }
}
    
31.07.2018 / 10:44