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...
– João Martins
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
– Gabriel Miranda
What text does it intend to put on Label? It is one of the reasons of the class Plant?
– João Martins
Exactly John, I want to put the Local property in lblLocal and the connection property in lblConnect depending on the switch case
– Gabriel Miranda
I think that with the edition made, the issue is already clear enough to be removed the mark of "pending".
– João Martins
Thank you very much John I am new in these things, thank you for your understanding in editing my question
– Gabriel Miranda
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.
– SUR1C4T3
Philip could demonstrate in a line of code?
– Gabriel Miranda
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;
– João Martins
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'.'
– Gabriel Miranda
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.
– João Martins
The code until debug, but when I choose Combox items it does not call the other form
– Gabriel Miranda
Instead of putting switch (comboBox1.SelectedItem.Tostring(). Trim()) place: switch ((Plant)comboBox1.Selecteditem). Local).
– João Martins
Thanks John the code is debugging and working.
– Gabriel Miranda