Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: The creature on Wed 31/05/2023 16:50:39

Title: All items unselected in a listbox
Post by: The creature on Wed 31/05/2023 16:50:39
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.
Title: Re: All items unselected in a listbox
Post by: Crimson Wizard on Wed 31/05/2023 17:14:30
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?
Title: Re: All items unselected in a listbox
Post by: The creature on Wed 31/05/2023 17:26:21
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.
Title: Re: All items unselected in a listbox
Post by: Crimson Wizard on Wed 31/05/2023 17:41:11
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) Select
String CurrentContact;
if (Listbox_Contacts.SelectedIndex >= 0)
{
    CurrentContact = Listbox_Contacts.Items[Listbox_Contacts.SelectedIndex];
}

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