Hola tengo un proyecto de windows form con C# que contiene un listbox en el que con un boton add te permite añadir un maximo de 3 objetos y cuando seleccionas ese objeto te muestra las propiedades y te habilita el boton de remover cuando le da a remover causa un crasheo porque la variable string SLUI = listbox.items.selecteditem.tostring(); es null como convierto ese null a otro valor para que no crashee sin añadir un objeto al listbox
variable del selecteditem
public string SLUI;
Codigo del listbox
private void UI_Inspector_SelectedIndexChanged(object sender, EventArgs e)
{
SLUI = UI_Inspector.SelectedItem.ToString();
string T1C = "Text-1";
string T2C = "Text-2";
string T3C = "Text-3";
RMVB.Visible = true;
if (SLUI == null)
{
SLUI = "NONE";
}
if (string.Compare(SLUI, T1C) == 0)
{
TX1P.Visible = true;
}
else
{
TX1P.Visible = false;
}
if (string.Compare(SLUI, T2C) == 0)
{
TX2P.Visible = true;
}
else
{
TX2P.Visible = false;
}
if (string.Compare(SLUI, T3C) == 0)
{
TX3P.Visible = true;
}
else
{
TX3P.Visible = false;
}
}
RMVB es el nombre del boton TX"numero"P es el panel de informacion del objeto UI_inspector es el listbox codigo del boton remover
private void RMVB_Click(object sender, EventArgs e)
{
SLUI = UI_Inspector.SelectedItem.ToString();
string T1C = "Text-1";
string T2C = "Text-2";
string T3C = "Text-3";
if (string.Compare(SLUI, T1C) == 0)
{
Text1B.Visible = true;
TX1P.Visible = false;
UI_Inspector.Items.Remove(T1C);
}
if (string.Compare(SLUI, T2C) == 0)
{
Text2B.Visible = true;
TX2P.Visible = false;
UI_Inspector.Items.Remove(T2C);
}
if (string.Compare(SLUI, T3C) == 0)
{
Text3B.Visible = true;
TX3P.Visible = false;
UI_Inspector.Items.Remove(T3C);
}
}
??
, quedando de esta maneraSLUI = UI_Inspector.SelectedItem.ToString() ?? "otro valor"