All items unselected in a listbox

Started by The creature, Wed 31/05/2023 16:50:39

Previous topic - Next topic

The creature

As the post title says: is there a way to have no items selected when a listbox is displayed. By default, the first item is always selected.

I'm sure there is a way as it was asked in the forum a while a go, but I've made a forum search and found nothing and I can't find anything in the manual about having all items deselected.

If I set the selectedIndex to -1 (like it says in the manual) it works. But, in a repeatedly executed I'm selecting items via string names and when I do this I get a compile error

Any help would be great.

Chris.

Crimson Wizard

Quote from: The creature on Wed 31/05/2023 16:50:39If I set the selectedIndex to -1 (like it says in the manual) it works. But, in a repeatedly executed I'm selecting items via string names and when I do this I get a compile error

Please post your code and tell what the error is?

The creature

#2
Code: ags
Function game_start()
{
Listbox_Contacts.AddItem("Contact1");
Listbox_Contacts.SelectedIndex=-1;
}


Function repeatedly_execute()
{
 String CurrentContact = Listbox_Contacts.Items[Listbox_Contacts.SelectedIndex];

If(CurrentContact=="Contact1")
{
gContactGUI.Visible=true;
}
}


The error is: Invalid index specified.

Crimson Wizard

#3
Well, if you set index to -1, then you cannot use it on array. So the solution is to check whether something is selected before trying to get CurrentContact.

Code: ags
String CurrentContact;
if (Listbox_Contacts.SelectedIndex >= 0)
{
    CurrentContact = Listbox_Contacts.Items[Listbox_Contacts.SelectedIndex];
}

if(CurrentContact != null && CurrentContact=="Contact1")
{
    gContactGUI.Visible=true;
}

SMF spam blocked by CleanTalk