OutOfBoundsException when clicking enabled button.

How can I disable a button’s state? It is set by default to not enabled in the inspector.
It will delete a row from a list box when clicked, but when the listbox is empty or when nothing is selected in the listbox, the button is still enabled. Clicking it gives the OutOfBoundsException error.

I tried this in the list box as CellAction and Gotfocus and LostFocus even handlers, but it doesn’t work as expected:
If Me.ListIndex >= 0 Then
EditStoryButton.Enabled = true
DeleteStoryButton.Enabled = true
AddCharacterB.Enabled = true

end if

Please locate the line raising the exception.

This is the button pressed that gives the error. From what I understand from the code, it says that if there is a line in the listbox selected, execute the code. Otherwise do nothing. But then why do I get the error when I click it with nothing selected?

If StoriesList.ListIndex >= 0 Then
Dim id As Integer
id = StoriesList.RowTag(StoriesList.ListIndex).IntegerValue

Dim sql As String
sql = “DELETE FROM Stories WHERE ID = ?”

Dim ps As SQLitePreparedStatement
ps = DB.Prepare(sql)

ps.BindType(0, SQLitePreparedStatement.SQLITE_INTEGER)

ps.SQLExecute(id)
If DBError Then Return

LoadStories
LoadCharacters
End If

this is the full error message:
An exception of class OutOfBoundsException was not handled. The application must shut down.

Can you create a sample that shows this problem?
I can’t make a simple code with .Listindex >= 0 execute if nothing is selected, like your code should work.

[quote=442457:@AndrvanHaren]How can I disable a button’s state? It is set by default to not enabled in the inspector.
It will delete a row from a list box when clicked, but when the listbox is empty or when nothing is selected in the listbox, the button is still enabled. Clicking it gives the OutOfBoundsException error.

I tried this in the list box as CellAction and Gotfocus and LostFocus even handlers, but it doesn’t work as expected:
If Me.ListIndex >= 0 Then
EditStoryButton.Enabled = true
DeleteStoryButton.Enabled = true
AddCharacterB.Enabled = true

end if[/quote]
Change that to

if me.listcount <= 0 then

It looks you might not have Project->Break on Exceptions enabled? Do that so Xojo will jump to the Debugger to show you the line of code that causes the exception.

https://documentation.xojo.com/getting_started/debugging/debugger_usage.html

I’d also put your button disable code in the ListBox Change event.

[quote=442467:@Greg O’Lone]Change that to

if me.listcount <= 0 then

and change

EditStoryButton.Enabled = true DeleteStoryButton.Enabled = true AddCharacterB.Enabled = true
to

EditStoryButton.Enabled = false DeleteStoryButton.Enabled = false AddCharacterB.Enabled = false
Enable the buttons when you have something on the list me.listcount >= 1

I added the suggestions, but still am able to press the Edit Story button when nothing is selected in the List window. This is the error I get now after set Project->Break on Exceptions enabled:

Sub Action()
StoriesList.EditCell(StoriesList. ListIndex, 0)
End Sub

Also, I noticed that simply clicking in the Listbox (not on a row) will enable the buttons.

The suggestions are for enable/disable the buttons if there is something/nothing on SotriesList. If you want to enable/disable the buttons if there is something selected, then the code is different.

Also, depending where you put the code, it will be executed or not. Maybe you want to use the ListBox Change event.

btw. I studied the Example project named BaseballLeage and the video named “Using SQL in Xojo apps” for this and checked it just now. That project shows the same error. Clicking the edit button when nothing selected gives the same error.

[quote=442486:@Alberto De Poo]The suggestions are for enable/disable the buttons if there is something/nothing on SotriesList. If you want to enable/disable the buttons if there is something selected, then the code is different.

Also, depending where you put the code, it will be executed or not. Maybe you want to use the ListBox Change event.[/quote]

Ah yes, I didn’t know the count method yet. I thought it was a variation on the method to see if something was selected in the listbox. Thanks for telling me.

I got it to work. I placed this code in the Change event Handler in the List box:

If Me.Listindex >= 0 Then
EditStoryButton.Enabled = true
DeleteStoryButton.Enabled = true
AddCharacterB.Enabled = true

end if

If Me.Listindex < 0 Then
EditStoryButton.Enabled = false
DeleteStoryButton.Enabled = false
AddCharacterB.Enabled = false

end if