How to save the last element of a dynamic combobox in c #?

0

Hi, I'm doing a program in which I save all my elements in a combobox since no sé muy bien hacerlo con listas o diccionarios .

The thing is that the combobox will be adding elements and increasing in size. The problem is that I need to select the last element every time a certain event occurs.

Well I add the elements in the following way and at the same time I quote that the combobox will increase in size as the program is working:

private void EntityLoad(DataInterceptedEventArgs obj){
    var entities = HEntity.Parse(obj.Packet);
    foreach(var entity in entities){
        Index = entity.Index;
        Id = entity.Id;
        Profile = entity.FigureId;
        playerList.Items.Add(entity.Index + "," + entity.Id + "," + entity.FigureId);
        //MessageBox.Show("figure id: "+ entity.FigureId +"el id del buny" + entity.Id + "buny index:" + entity.Index+ ", position: "+ entity.Tile.X + "," + entity.Tile.Y + "," + entity.Tile.Z);

    }
}

What I want to happen every time which for this is not necessary for you to help me as I know how to do it. What I want is that every time that event happens, the last element of my combobox. So my question is:

If I have un combobox que cambia de tamaño I can sacar el ultimo elemento each time my event is launched.

Second it is more advisable to use un diccionario o lista para guardar mis elementos .

El combobox será dinamico but I will tell you the moment only that I will need to know how to get the last element sabiendo que variará de tamaño .

Here is a capture of the combobox that will increase as my program runs:

Correction: My goal is to take the last element of my combobox, save it and if possible make a split as a practical example that would help me a lot

Example.

Combobox:

"hola,dos,tres"
"1,dos,tres"
"2,dos,tres"

I'll get:

Obtener el ultimo elemento.
lo guardaré.
y haré un split para guardar los elementos por separado: 
int i=2
string dos =  "dos"
string tres =  "tr"
    
asked by jeronimo urtado 05.04.2017 в 19:38
source

1 answer

2

Regardless of whether it is dynamic or not:

var ultimo = combobox.items.count - 1;

To select it

combobox.selectedindex = ultimo;

combobox.items.count < - always bring the number of elements, -1 to get the last index.

And every time you re dimensions your combo you would have to take that value that will be changing.

To take the value

  • Selecting it and taking the selected

     var ultimo = combobox.items.count - 1;
     combobox.selectedindex = ultimo;  //<-- con esto lo dejas seleccionado
     var valor = comboBox1.SelectedValue;
    
  • Direct with the index

     var ultimo = combobox.items.count - 1;
     var valor = combobox.items[ultimo].Value;  //No estoy segura de la sintaxis
    

Once you have the value:

var datos = valor.Split(',');
if(datos.length > 2){
   int i=datos[0];
   string dos =  datos[1];
   string tres =  datos[2];
}

Something like that, I hope my examples can help you, Greetings ... and it's not a nuisance by the way.

    
answered by 05.04.2017 / 19:46
source