I cannot assign a value inside my Label from a List

Asked

Viewed 56 times

1

Good morning, first I would like to say that I am a beginner and I am in doubt when it comes to assigning a value to the property Text of a Label from a list created with information obtained from a file txt.

Follows 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 the Label with the value of a class property Planta, but I don’t know how I can get information from the classes listed in items of ComboBox.

To label that I want to assign the value is to formb.lblLocal (line in comment).

  • What is your question exactly? To assign the value to Label just put the value on Text, as it is already in your code...

  • Good morning João, my doubt would be to put an item of my list there depending on the choice of the user of the Combox

  • What text does it intend to put on Label? It is one of the reasons of the class Plant?

  • Exactly John, I want to put the Local property in lblLocal and the connection property in lblConnect depending on the switch case

  • 1

    I think that with the edition made, the issue is already clear enough to be removed the mark of "pending".

  • Thank you very much John I am new in these things, thank you for your understanding in editing my question

  • I think I understand where you’re going. You already populate your combobox with the "Local" property of each Plant. To get the string of the selected item just get the selected comboboxitem (Comboboxitem item = combobox1.selectedItem as Comboboxitem). Then pick up your content (item.Content.Tostring()). Finally, to set this string on your label just do: label.Content = stringAqui.

  • Philip could demonstrate in a line of code?

  • I asked to reopen because it is easier to put code, but the solution is to do the following (without any validations): formb.lblLocal.Text = ((Plant)comboBox1.Selecteditem). Local;

  • Using this solution João appears here when debugging the code: System.Invalidcastexception: 'It is not possible to convert an object of type 'System.String' in type 'Betateste.Plan'.'

  • Ha, of course, is not being assigned the class instance Plant to items of Combobox. Do the following, no foreach that you use to assign the value of item put as follows: comboBox1.Items.Add(result);, then out of the foreach place: comboBox1.Displaymember = "Local";. I think that way the rest of the code will work, and that way you can get any information from the class Plant.

  • The code until debug, but when I choose Combox items it does not call the other form

  • Instead of putting switch (comboBox1.SelectedItem.Tostring(). Trim()) place: switch ((Plant)comboBox1.Selecteditem). Local).

  • Thanks John the code is debugging and working.

Show 9 more comments

1 answer

0


Here is your properly structured and optimized response :)

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.